Passed
Push — master ( 5b49ac...957a79 )
by SignpostMarv
02:45
created

Framework::ObtainConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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