Passed
Push — master ( 62b6d2...494a24 )
by SignpostMarv
02:50
created

Framework   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 137
ccs 53
cts 53
cp 1
rs 10
c 0
b 0
f 0
wmc 23

12 Methods

Rating   Name   Duplication   Size   Complexity  
A ConfigureDatabaseConnection() 0 11 2
A ObtainConfig() 0 3 1
A ObtainFrameworkForRequest() 0 11 2
A PairWithRequest() 0 3 1
A FileIsUnderBasePath() 0 5 2
A ValidateConfig() 0 3 1
A ObtainBaseUrl() 0 3 1
A __construct() 0 20 4
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
    /**
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
        $this->baseUrl = $baseUrl . str_replace('//', '/', $parsed['path']);
59 42
        $this->basePath = $basePath;
60
61 42
        $this->config = $this->ValidateConfig($config);
62 34
    }
63
64 8
    public function ObtainDatabaseConnection() : EasyDB
65
    {
66 8
        if ( ! ($this->db instanceof EasyDB)) {
67 4
            throw new BadMethodCallException('Database Connection not available!');
68
        }
69
70 4
        return $this->db;
71
    }
72
73 16
    public function ConfigureDatabaseConnection(
74
        string $dsn,
75
        string $username = null,
76
        string $password = null,
77
        array $options = []
78
    ) : void {
79 16
        if ($this->db instanceof EasyDB) {
80 4
            throw new BadMethodCallException('Database Connection already made!');
81
        }
82
83 16
        $this->db = Factory::create($dsn, $username, $password, $options);
84 16
    }
85
86 18
    public function ObtainBaseUrl() : string
87
    {
88 18
        return $this->baseUrl;
89
    }
90
91 10
    public function ObtainBasePath() : string
92
    {
93 10
        return $this->basePath;
94
    }
95
96 10
    public function ObtainConfig() : array
97
    {
98 10
        return $this->config;
99
    }
100
101 2
    public function FileIsUnderBasePath(string $filename) : bool
102
    {
103
        return
104 2
            is_file($filename) &&
105 2
            0 === mb_strpos($this->basePath, realpath($filename));
106
    }
107
108 24
    public static function PairWithRequest(self $framework, Request $request) : void
109
    {
110 24
        self::$requestpair[spl_object_hash($request)] = $framework;
111 24
    }
112
113 24
    public static function ObtainFrameworkForRequest(Request $request) : self
114
    {
115 24
        $framework = self::$requestpair[spl_object_hash($request)] ?? null;
116
117 24
        if ( ! ($framework instanceof self)) {
118 24
            throw new InvalidArgumentException(
119 24
                'No framework instance has been paired with the provided request!'
120
            );
121
        }
122
123 16
        return $framework;
124
    }
125
126 16
    public static function DisposeOfFrameworkReferences(self ...$frameworks) : void
127
    {
128 16
        foreach (array_keys(self::$requestpair) as $hash) {
129 16
            if (in_array(self::$requestpair[$hash], $frameworks, true)) {
130 16
                unset(self::$requestpair[$hash]);
131
            }
132
        }
133 16
    }
134
135 8
    public static function DisposeOfRequestReferences(Request ...$requests) : void
136
    {
137 8
        foreach ($requests as $request) {
138 8
            $hash = spl_object_hash($request);
139
140 8
            if (isset(self::$requestpair[$hash])) {
141 8
                unset(self::$requestpair[$hash]);
142
            }
143
        }
144 8
    }
145
146
    /**
147
    * @throws InvalidArgumentException if $config contains something not valid
148
    */
149 34
    protected function ValidateConfig(array $config) : array
150
    {
151 34
        return $config;
152
    }
153
}
154