Completed
Pull Request — master (#283)
by Phil
04:05 queued 40s
created

CachedRouter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 23
c 1
b 0
f 0
dl 0
loc 55
ccs 21
cts 23
cp 0.913
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A buildRouter() 0 25 6
A __construct() 0 5 1
A dispatch() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Route;
6
7
use InvalidArgumentException;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
11
use function Opis\Closure\{serialize as s, unserialize as u};
2 ignored issues
show
Bug introduced by
The type Opis\Closure\serialize was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type Opis\Closure\unserialize was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class CachedRouter
14
{
15
    /**
16
     * @var callable
17
     */
18
    protected $builder;
19
20
    /**
21
     * @var string
22
     */
23
    protected $cacheFile;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $cacheEnabled;
29
30 3
    public function __construct(callable $builder, string $cacheFile, bool $cacheEnabled = true)
31
    {
32 3
        $this->builder = $builder;
33 3
        $this->cacheFile = $cacheFile;
34 3
        $this->cacheEnabled = $cacheEnabled;
35 3
    }
36
37 3
    public function dispatch(ServerRequestInterface $request): ResponseInterface
38
    {
39 3
        $router = $this->buildRouter($request);
40 3
        return $router->dispatch($request);
41
    }
42
43 3
    protected function buildRouter(ServerRequestInterface $request): Router
44
    {
45 3
        if (true === $this->cacheEnabled && file_exists($this->cacheFile)) {
46 3
            $cache  = file_get_contents($this->cacheFile);
47 3
            $router = u($cache, ['allowed_classes' => true]);
48
49 3
            if ($router instanceof Router) {
50 3
                return $router;
51
            }
52
        }
53
54 3
        $builder = $this->builder;
55 3
        $router  = $builder(new Router());
56
57 3
        if (false === $this->cacheEnabled) {
58
            return $router;
59
        }
60
61 3
        if ($router instanceof Router) {
62 3
            $router->prepareRoutes($request);
63 3
            file_put_contents($this->cacheFile, s($router));
64 3
            return $router;
65
        }
66
67
        throw new InvalidArgumentException('Invalid Router builder provided to cached router');
68
    }
69
}
70