• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

daycry / auth / 21031260116

15 Jan 2026 11:02AM UTC coverage: 66.786% (+0.2%) from 66.555%
21031260116

push

github

daycry
Refactor Auth config for improved organization

Reorganized the Auth configuration file by grouping related settings into logical sections such as logging, rate limiting, authentication, user/registration, password/security, OAuth, views/URLs, and discovery/cron. This improves readability and maintainability without changing any functional behavior.

2246 of 3363 relevant lines covered (66.79%)

33.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Commands/DiscoverCommand.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Auth.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Auth\Commands;
15

16
use CodeIgniter\CLI\BaseCommand;
17
use CodeIgniter\CLI\CLI;
18
use CodeIgniter\Config\BaseConfig;
19
use CodeIgniter\I18n\Time;
20
use Daycry\Auth\Entities\Api;
21
use Daycry\Auth\Entities\Controller;
22
use Daycry\Auth\Entities\Endpoint;
23
use Daycry\Auth\Models\ApiModel;
24
use Daycry\Auth\Models\ControllerModel;
25
use Daycry\Auth\Models\EndpointModel;
26
use Daycry\ClassFinder\ClassFinder;
27
use Daycry\ClassFinder\Config\ClassFinder as ClassFinderConfig;
28
use ReflectionClass;
29
use ReflectionMethod;
30

31
class DiscoverCommand extends BaseCommand
32
{
33
    protected $group       = 'Auth';
34
    protected $name        = 'auth:discover';
35
    protected $description = 'Discover classes from namespace to import in database.';
36
    protected Time $timeStart;
37
    protected BaseConfig $config;
38
    protected array $allClasses = [];
39

40
    public function run(array $params): void
41
    {
42
        if (! service('settings')->get('Auth.enableDiscovery')) {
×
43
            CLI::write('**** DISCOVERY DISABLED. Enable it in the Auth config file. ****', 'white', 'red');
×
44

45
            return;
×
46
        }
47

48
        $this->timeStart = Time::now()->subSeconds(1);
×
49
        /** @var ClassFinderConfig $finderConfig */
50
        $finderConfig                  = config('ClassFinder');
×
51
        $finderConfig->finder['files'] = false;
×
52

53
        $api = $this->_checkApiModel();
×
54

55
        foreach (service('settings')->get('Auth.namespaceScope') as $namespace) {
×
56
            // remove "\" for search in class-finder
57
            $namespace = (mb_substr($namespace, 0, 1) === '\\') ? mb_substr($namespace, 1) : $namespace;
×
58

59
            $classes = (new ClassFinder($finderConfig))->getClassesInNamespace($namespace, ClassFinder::RECURSIVE_MODE | ClassFinder::ALLOW_CLASSES);
×
60
            if ($classes !== []) {
×
61
                foreach ($classes as $class) {
×
62
                    $this->allClasses[] = '\\' . $class;
×
63

64
                    $methods = $this->_getMethodsFromCLass($class);
×
65

66
                    $class = (mb_substr($class, 0, 1) !== '\\') ? '\\' . $class : $class;
×
67

68
                    $this->_checkClassController($api, $class, $methods);
×
69
                }
70

71
                unset($classes);
×
72
            }
73
        }
74

75
        $controllerModel = new ControllerModel();
×
76
        $allControllers  = $controllerModel->where('api_id', $api->id)->findColumn('controller');
×
77
        if ($allControllers) {
×
78
            $forRemove = array_diff($allControllers, $this->allClasses);
×
79

80
            if ($forRemove !== []) {
×
81
                $controllerModel->where('api_id', $api->id)
×
82
                    ->whereIn('controller', $forRemove)
×
83
                    ->delete();
×
84
            }
85
        }
86

87
        CLI::write('**** FINISHED. ****', 'white', 'green');
×
88
    }
89

90
    private function _checkApiModel(): Api
91
    {
92
        $apiModel = new ApiModel();
×
93
        /** @var ?Api $api */
94
        $api = $apiModel->where('url', site_url())->first();
×
95

96
        if (! $api) {
×
97
            $api = new Api();
×
98
            $api->fill(['url' => site_url()]);
×
99
            $apiModel->save($api);
×
100
            $api->id = $apiModel->getInsertID();
×
101
        } else {
102
            $api->fill(['checked_at' => Time::now()]);
×
103
            $apiModel->save($api);
×
104
        }
105

106
        return $api;
×
107
    }
108

109
    private function _getMethodsFromCLass($namespace): array
110
    {
111
        $f       = new ReflectionClass($namespace);
×
112
        $methods = [];
×
113

114
        $namespaceConverted = (mb_substr($namespace, 0, 1) !== '\\') ? '\\' . $namespace : $namespace;
×
115

116
        foreach ($f->getMethods(ReflectionMethod::IS_PUBLIC) as $m) {
×
117
            if (! str_starts_with($m->name, '__') && $m->class === $namespace && ! in_array($m->name, service('settings')->get('Auth.excludeMethods'), true)) {
×
118
                $methods[] = $namespaceConverted . '::' . $m->name;
×
119
            }
120
        }
121

122
        return $methods;
×
123
    }
124

125
    private function _checkClassController(Api $api, string $class, array $methods = []): void
126
    {
127
        $controllerModel = new ControllerModel();
×
128
        $controller      = $controllerModel->where('api_id', $api->id)->where('controller', $class)->first();
×
129

130
        if (! $controller) {
×
131
            $controller = new Controller();
×
132
            $controller->fill(['api_id' => $api->id, 'controller' => $class]);
×
133
            $controllerModel->save($controller);
×
134
            $controller->id = $controllerModel->getInsertID();
×
135
        }
136

137
        $endpointModel = new EndpointModel();
×
138

139
        $allMethods = (new EndpointModel())->where('controller_id', $controller->id)->findColumn('method');
×
140

141
        foreach ($methods as $method) {
×
142
            $endpoint = $endpointModel->where('controller_id', $controller->id)->where('method', $method)->first();
×
143

144
            if (! $endpoint) {
×
145
                $endpoint = new Endpoint();
×
146
                $endpoint->fill(['controller_id' => $controller->id, 'method' => $method]);
×
147
                $endpointModel->save($endpoint);
×
148
                $endpoint->id = $endpointModel->getInsertID();
×
149
            }
150

151
            $endpoint->checked_at = Time::now();
×
152
            $endpointModel->save($endpoint);
×
153
        }
154

155
        if ($allMethods) {
×
156
            $forRemove = array_diff($allMethods, $methods);
×
157

158
            if ($forRemove !== []) {
×
159
                $endpointModel->where('controller_id', $controller->id)
×
160
                    ->whereIn('method', $forRemove)
×
161
                    ->delete();
×
162
            }
163
        }
164

165
        $controller->checked_at = Time::now();
×
166
        $controllerModel->save($controller);
×
167
    }
168
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc