Passed
Push — master ( 78a20d...d042de )
by SignpostMarv
03:14
created

Framework::NormaliseUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 17
ccs 7
cts 9
cp 0.7778
crap 3.0987
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
* @author SignpostMarv
4
*/
5
declare(strict_types=1);
6
7
namespace SignpostMarv\DaftFramework;
8
9
use BadMethodCallException;
10
use InvalidArgumentException;
11
use ParagonIE\EasyDB\EasyDB;
12
use ParagonIE\EasyDB\Factory;
13
use Symfony\Component\HttpFoundation\Request;
14
15
class Framework
16
{
17
    /**
18
    * @var string
19
    */
20
    private $baseUrl;
21
22
    /**
23
    * @var string
24
    */
25
    private $basePath;
26
27
    /**
28
    * @var EasyDB|null
29
    */
30
    private $db;
31
32
    /**
33
    * @var array
34
    */
35
    private $config;
36
37
    /**
38
    * @var array<string, self>
39
    */
40
    private static $requestpair = [];
41
42 96
    public function __construct(string $baseUrl, string $basePath, array $config = [])
43
    {
44 96
        if ( ! is_dir($basePath)) {
45 2
            throw new InvalidArgumentException('Base path must be a directory!');
46 94
        } elseif (realpath($basePath) !== $basePath) {
47 2
            throw new InvalidArgumentException('Path should be explicitly set to via realpath!');
48
        }
49
50 92
        $this->baseUrl = static::NormaliseUrl($baseUrl);
51 92
        $this->basePath = $basePath;
52
53 92
        $this->config = $this->ValidateConfig($config);
54 68
    }
55
56 92
    public static function NormaliseUrl(string $baseUrl) : string
57
    {
58 92
        $parsed = (array) parse_url($baseUrl);
59
60 92
        if ( ! isset($parsed['scheme'], $parsed['host'], $parsed['path'])) {
61
            throw new InvalidArgumentException(
62
                'Base URL must have at least a scheme, host & path in order to be normalised!'
63
            );
64
        }
65
66 92
        $baseUrl = (string) $parsed['scheme'] . '://' . (string) $parsed['host'];
67
68 92
        if (isset($parsed['port'])) {
69 8
            $baseUrl .= ':' . (string) $parsed['port'];
70
        }
71
72 92
        return $baseUrl . str_replace('//', '/', (string) $parsed['path']);
73
    }
74
75 12
    public function ObtainDatabaseConnection() : EasyDB
76
    {
77 12
        if ( ! ($this->db instanceof EasyDB)) {
78 4
            throw new BadMethodCallException('Database Connection not available!');
79
        }
80
81 8
        return $this->db;
82
    }
83
84 32
    public function ConfigureDatabaseConnection(
85
        string $dsn,
86
        string $username = null,
87
        string $password = null,
88
        array $options = []
89
    ) : void {
90 32
        if ($this->db instanceof EasyDB) {
91 8
            throw new BadMethodCallException('Database Connection already made!');
92
        }
93
94 32
        $this->db = Factory::create($dsn, $username, $password, $options);
95 32
    }
96
97 48
    public function ObtainBaseUrl() : string
98
    {
99 48
        return $this->baseUrl;
100
    }
101
102 14
    public function ObtainBasePath() : string
103
    {
104 14
        return $this->basePath;
105
    }
106
107 32
    public function ObtainConfig() : array
108
    {
109 32
        return $this->config;
110
    }
111
112 36
    public function FileIsUnderBasePath(string $filename, bool $requireFileExists = true) : bool
113
    {
114 36
        $realpath = realpath($filename);
115
116
        return
117 36
            ( ! $requireFileExists && ! file_exists($filename)) ||
118
            (
119 18
                is_file($filename) &&
120 18
                is_string($realpath) &&
121 36
                0 === mb_strpos($realpath, $this->basePath)
122
            );
123
    }
124
125 58
    public static function PairWithRequest(self $framework, Request $request) : void
126
    {
127 58
        self::$requestpair[spl_object_hash($request)] = $framework;
128 58
    }
129
130 54
    public static function ObtainFrameworkForRequest(Request $request) : self
131
    {
132 54
        $framework = self::$requestpair[spl_object_hash($request)] ?? null;
133
134 54
        if ( ! ($framework instanceof self)) {
135 36
            throw new InvalidArgumentException(
136 36
                'No framework instance has been paired with the provided request!'
137
            );
138
        }
139
140 42
        return $framework;
141
    }
142
143 24
    public static function DisposeOfFrameworkReferences(self ...$frameworks) : void
144
    {
145 24
        foreach (array_keys(self::$requestpair) as $hash) {
146 24
            if (in_array(self::$requestpair[$hash], $frameworks, true)) {
147 24
                unset(self::$requestpair[$hash]);
148
            }
149
        }
150 24
    }
151
152 12
    public static function DisposeOfRequestReferences(Request ...$requests) : void
153
    {
154 12
        foreach ($requests as $request) {
155 12
            $hash = spl_object_hash($request);
156
157 12
            if (isset(self::$requestpair[$hash])) {
158 12
                unset(self::$requestpair[$hash]);
159
            }
160
        }
161 12
    }
162
163
    /**
164
    * @throws InvalidArgumentException if $config contains something not valid
165
    */
166 68
    protected function ValidateConfig(array $config) : array
167
    {
168 68
        return $config;
169
    }
170
}
171