Passed
Push — master ( 2c2744...7da672 )
by Divine Niiquaye
11:42
created

AbstractRouter::getCache()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
c 0
b 0
f 0
nc 5
nop 2
dl 0
loc 26
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace App\BenchMark;
19
20
use App\BenchMark\Strategy\CaseInterface;
21
22
/**
23
 * An abstraction for benchmarking routers.
24
 *
25
 * @method bool testSubDomain()
26
 *
27
 * @author Divine Niiquaye Ibok <[email protected]>
28
 */
29
abstract class AbstractRouter
30
{
31
    public const PATH = '{world}';
32
33
    public const HOST = '{extension}';
34
35
    protected string $type;
36
37
    protected bool $cache;
38
39
    protected CaseInterface $strategy;
40
41
    protected RouteGenerator $generator;
42
43
    /**
44
     * @param null|CaseInterface $strategy
45
     * @param RouteGenerator     $generator
46
     * @param string             $type
47
     * @param bool               $cache
48
     */
49
    public function __construct(CaseInterface $strategy, RouteGenerator $generator, string $type, bool $cache)
50
    {
51
        $this->type      = $type;
52
        $this->cache     = $cache;
53
        $this->strategy  = $strategy;
54
        $this->generator = $generator;
55
    }
56
57
    /**
58
     * Test Router against caching support
59
     *
60
     * @return bool
61
     */
62
    public static function isCacheable(): bool
63
    {
64
        return false;
65
    }
66
67
    /**
68
     * Test Dynamic path eg: /{var}
69
     *
70
     * @return bool
71
     */
72
    abstract public function testPath(): bool;
73
74
    /**
75
     * Test static path eg: /hello
76
     *
77
     * @return bool
78
     */
79
    abstract public function testStatic(): bool;
80
81
    /**
82
     * Build routes to be used in testPath, testStatic and testSubDomain
83
     *
84
     * @param array $routes
85
     */
86
    abstract public function buildRoutes(array $routes): void;
87
88
    /**
89
     * Get the cache directory or file is router supports caching.
90
     *
91
     * @param string $name
92
     * @param string $file
93
     *
94
     * @return null|string
95
     */
96
    protected function getCache(string $name, string $file = null): ?string
97
    {
98
        if ($this->isCacheable() && $this->cache) {
99
            static $subName;
100
101
            switch ($this->type) {
102
                case 'SubDomain':
103
                    $subName = '/hosts';
104
105
                    break;
106
107
                case 'Path':
108
                    $subName = '/paths';
109
110
                    break;
111
112
                case 'Static':
113
                    $subName = '/static';
114
115
                    break;
116
            }
117
118
            return __DIR__ . '/caches/' . $name . $subName . $file;
119
        }
120
121
        return null;
122
    }
123
}
124