Framework   A
last analyzed

Complexity

Total Complexity 28

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 0 Features 0
Metric Value
eloc 55
c 7
b 0
f 0
dl 0
loc 175
ccs 64
cts 64
cp 1
rs 10
wmc 28

13 Methods

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