Passed
Push — master ( cd1392...a15adc )
by SignpostMarv
07:33
created

Framework::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 3
rs 9.8666
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
        /**
59
        * @var array<string, string> $parsed
60
        */
61 92
        $parsed = parse_url($baseUrl);
62
63 92
        $baseUrl = $parsed['scheme'] . '://' . $parsed['host'];
64
65 92
        if (isset($parsed['port'])) {
66 8
            $baseUrl .= ':' . $parsed['port'];
67
        }
68
69 92
        return $baseUrl . str_replace('//', '/', $parsed['path']);
70
    }
71
72 4
    public function ObtainDatabaseConnection() : EasyDB
73
    {
74 4
        if ( ! ($this->db instanceof EasyDB)) {
75 4
            throw new BadMethodCallException('Database Connection not available!');
76
        }
77
78
        return $this->db;
79
    }
80
81 32
    public function ConfigureDatabaseConnection(
82
        string $dsn,
83
        string $username = null,
84
        string $password = null,
85
        array $options = []
86
    ) : void {
87 32
        if ($this->db instanceof EasyDB) {
88 8
            throw new BadMethodCallException('Database Connection already made!');
89
        }
90
91 32
        $this->db = Factory::create($dsn, $username, $password, $options);
92 32
    }
93
94 40
    public function ObtainBaseUrl() : string
95
    {
96 40
        return $this->baseUrl;
97
    }
98
99 14
    public function ObtainBasePath() : string
100
    {
101 14
        return $this->basePath;
102
    }
103
104 32
    public function ObtainConfig() : array
105
    {
106 32
        return $this->config;
107
    }
108
109 36
    public function FileIsUnderBasePath(string $filename, bool $requireFileExists = true) : bool
110
    {
111 36
        $realpath = realpath($filename);
112
113
        return
114 36
            ( ! $requireFileExists && ! file_exists($filename)) ||
115
            (
116 18
                is_file($filename) &&
117 18
                is_string($realpath) &&
118 36
                0 === mb_strpos($realpath, $this->basePath)
119
            );
120
    }
121
122 50
    public static function PairWithRequest(self $framework, Request $request) : void
123
    {
124 50
        self::$requestpair[spl_object_hash($request)] = $framework;
125 50
    }
126
127 54
    public static function ObtainFrameworkForRequest(Request $request) : self
128
    {
129 54
        $framework = self::$requestpair[spl_object_hash($request)] ?? null;
130
131 54
        if ( ! ($framework instanceof self)) {
132 36
            throw new InvalidArgumentException(
133 36
                'No framework instance has been paired with the provided request!'
134
            );
135
        }
136
137 42
        return $framework;
138
    }
139
140 24
    public static function DisposeOfFrameworkReferences(self ...$frameworks) : void
141
    {
142 24
        foreach (array_keys(self::$requestpair) as $hash) {
143 24
            if (in_array(self::$requestpair[$hash], $frameworks, true)) {
144 24
                unset(self::$requestpair[$hash]);
145
            }
146
        }
147 24
    }
148
149 12
    public static function DisposeOfRequestReferences(Request ...$requests) : void
150
    {
151 12
        foreach ($requests as $request) {
152 12
            $hash = spl_object_hash($request);
153
154 12
            if (isset(self::$requestpair[$hash])) {
155 12
                unset(self::$requestpair[$hash]);
156
            }
157
        }
158 12
    }
159
160
    /**
161
    * @throws InvalidArgumentException if $config contains something not valid
162
    */
163 68
    protected function ValidateConfig(array $config) : array
164
    {
165 68
        return $config;
166
    }
167
}
168