Passed
Push — master ( c68a30...978f61 )
by SignpostMarv
04:06
created

Framework::ValidateConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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