Passed
Push — master ( 4dcde6...03a7f0 )
by SignpostMarv
03:33
created

Framework::__construct()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 4

Importance

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