Passed
Push — master ( ce1ad4...878910 )
by Korotkov
03:35 queued 01:47
created

Route   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
eloc 35
c 1
b 0
f 0
dl 0
loc 74
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 18 5
A collect() 0 8 3
B getRoutes() 0 36 6
1
<?php
2
3
namespace App\Ship;
4
5
use Rudra\Container\Facades\Rudra;
6
use Rudra\Exceptions\RouterException;
7
use Rudra\Router\RouterFacade as Router;
8
9
class Route
10
{
11
    /**
12
     * @throws RouterException
13
     */
14
    public function run()
15
    {
16
        if (config('environment') === 'development') {
17
            $timeCollector = Rudra::get("debugbar")['time'];
18
19
            if ($timeCollector->hasStartedMeasure('index')) {
20
                $timeCollector->stopMeasure('index');
21
            }
22
23
            if (!$timeCollector->hasStartedMeasure('routing')) {
24
                $timeCollector->startMeasure('routing');
25
            }
26
        }
27
28
        $this->collect(config('containers'));
29
30
        if (config('environment') !== 'test') {
31
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
32
        }
33
    }
34
35
    protected function collect(array $namespaces): void
36
    {
37
        foreach ($namespaces as $container => $item) {
38
            $routes     = $this->getRoutes($container);
39
            $flatRoutes = array_merge(...$routes);
40
41
            foreach ($flatRoutes as $route) {
42
                Router::set($route);
43
            }
44
        }
45
    }
46
47
    protected function getRoutes(string $container): array
48
    {
49
        $cacheDir  = "../app/cache";
50
        $cacheTime = config('cache.time', 'routes');
51
        $routesDir = $cacheDir . "/routes";
52
        $cacheFile = $routesDir . "/routes_" . $container . ".php";
53
        $cacheLifetime = strtotime($cacheTime) - time();
54
55
        if (!is_dir($cacheDir)) {
56
            mkdir($cacheDir, 0755, true);
57
        }
58
59
        if (!is_dir($routesDir)) {
60
            mkdir($routesDir, 0755, true);
61
        }
62
63
        if (file_exists($cacheFile)) {
64
            $cacheTime   = filemtime($cacheFile);
65
            $currentTime = time();
66
67
            if ($currentTime - $cacheTime < $cacheLifetime) {
68
                return include $cacheFile;
69
            }
70
        }
71
72
        $path = "../app/Containers/" . ucfirst($container) . "/routes";
73
74
        if (!file_exists($path . ".php")) {
75
            throw new \RuntimeException("Routes file not found for container: " . $container);
76
        }
77
78
        // Генерация маршрутов и сохранение их в кеш
79
        $routes = Router::annotationCollector(require_once $path . ".php", true, Rudra::config()->get("attributes"));
0 ignored issues
show
Unused Code introduced by
The call to Rudra\Router\RouterFacade::annotationCollector() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        /** @scrutinizer ignore-call */ 
80
        $routes = Router::annotationCollector(require_once $path . ".php", true, Rudra::config()->get("attributes"));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
80
        file_put_contents($cacheFile, "<?php\nreturn " . var_export($routes, true) . ";");
81
82
        return $routes;
83
    }
84
}
85