GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#154)
by joseph
34:48
created

Config::calculateProxyDir()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
ccs 0
cts 11
cp 0
cc 5
nc 4
nop 0
crap 30
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta;
4
5
use Composer\Autoload\ClassLoader;
6
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
7
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\TypeHelper;
8
use EdmondsCommerce\DoctrineStaticMeta\Exception\ConfigException;
9
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
10
11
/**
12
 * Class Config
13
 *
14
 * @package EdmondsCommerce\DoctrineStaticMeta
15
 * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
16
 */
17
class Config implements ConfigInterface
18
{
19
20
    private static $projectRootDirectory;
21
    private $config = [];
22
23
    /**
24
     * Config constructor.
25
     *
26
     * @param array|mixed[] $server
27
     *
28
     * @throws ConfigException
29
     * @throws DoctrineStaticMetaException
30
     */
31
    public function __construct(array $server)
32
    {
33
        foreach (static::REQUIRED_PARAMS as $key) {
34
            if (!\array_key_exists($key, $server)) {
35
                throw new ConfigException(
36
                    'required config param ' . $key . ' is not set in $server'
37
                );
38
            }
39
            $this->config[$key] = $server[$key];
40
        }
41
        foreach (self::PARAMS as $key) {
42
            if (\array_key_exists($key, $server)) {
43
                $this->config[$key] = $server[$key];
44
                continue;
45
            }
46
            $this->config[$key] = $this->get($key);
47
        }
48
49
        $this->validateConfig();
50
    }
51
52
    /**
53
     * @param string $key
54
     * @param mixed  $default
55
     *
56
     * @return mixed|string
57
     * @throws DoctrineStaticMetaException
58
     */
59
    public function get(string $key, $default = ConfigInterface::NO_DEFAULT_VALUE)
60
    {
61
        if (!isset(static::REQUIRED_PARAMS[$key])
62
            && !isset(static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key])
63
            && !isset(static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key])
64
        ) {
65
            throw new ConfigException(
66
                'Invalid config param '
67
                . $key
68
                . ', should be one of '
69
                . print_r(static::PARAMS, true)
70
            );
71
        }
72
        if (isset($this->config[$key])) {
73
            return $this->config[$key];
74
        }
75
        if (ConfigInterface::NO_DEFAULT_VALUE !== $default) {
76
            return $default;
77
        }
78
        if (isset(static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key])) {
79
            return static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key];
80
        }
81
        if (isset(static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key])) {
82
            $method = static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key];
83
84
            return $this->$method();
85
        }
86
        throw new ConfigException(
87
            'No config set for param ' . $key . ' and no default provided'
88
        );
89
    }
90
91
    /**
92
     * @throws ConfigException
93
     * @throws DoctrineStaticMetaException
94
     */
95
    private function validateConfig(): void
96
    {
97
        $errors     = [];
98
        $typeHelper = new TypeHelper();
99
        foreach (ConfigInterface::PARAM_TYPES as $param => $requiredType) {
100
            $value = $this->get($param);
101
            if (self::TYPE_BOOL === $requiredType
102
                && is_numeric($value)
103
                && \in_array((int)$value, [0, 1], true)
104
            ) {
105
                $this->config[$param] = ($value === 1);
106
                continue;
107
            }
108
            if (\is_object($value)) {
109
                if (!($value instanceof $requiredType)) {
110
                    $actualType = \get_class($value);
111
                    $errors[]   =
112
                        ' ERROR  ' . $param . ' is not an instance of the required object [' . $requiredType . ']'
113
                        . 'currently configured as an object of the class  [' . $actualType . ']';
114
                }
115
                continue;
116
            }
117
            $actualType = $typeHelper->getType($value);
118
            if ($actualType !== $requiredType) {
119
                $valueString = var_export($value, true);
120
                $errors[]    = ' ERROR  ' . $param . ' is not of the required type [' . $requiredType . ']'
121
                               . ' currently configured as type [' . $actualType . '] with value: ' . $valueString;
122
            }
123
        }
124
        if ([] !== $errors) {
125
            throw new ConfigException(implode("\n\n", $errors));
126
        }
127
    }
128
129
    /**
130
     * Default Entities path, calculated default
131
     *
132
     * @return string
133
     * @throws DoctrineStaticMetaException
134
     */
135
    private function calculateEntitiesPath(): string
136
    {
137
        try {
138
            return self::getProjectRootDirectory() . '/src/Entities';
139
        } catch (\Exception $e) {
140
            throw new DoctrineStaticMetaException(
141
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
142
                $e->getCode(),
143
                $e
144
            );
145
        }
146
    }
147
148
    /**
149
     * Get the absolute path to the root of the current project
150
     *
151
     * It does this by working from the Composer autoloader which we know will be in a certain place in `vendor`
152
     *
153
     * @return string
154
     * @throws DoctrineStaticMetaException
155
     */
156
    public static function getProjectRootDirectory(): string
157
    {
158
        try {
159
            if (null === self::$projectRootDirectory) {
160
                $reflection                 = new \ts\Reflection\ReflectionClass(ClassLoader::class);
161
                self::$projectRootDirectory = \dirname($reflection->getFileName(), 3);
162
            }
163
164
            return self::$projectRootDirectory;
165
        } catch (\Exception $e) {
166
            throw new DoctrineStaticMetaException(
167
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
168
                $e->getCode(),
169
                $e
170
            );
171
        }
172
    }
173
174
    /**
175
     * Default Entities path, calculated default
176
     *
177
     * @return string
178
     * @throws DoctrineStaticMetaException
179
     */
180
    private function calculateProxyDir(): string
181
    {
182
        try {
183
            $dir = self::getProjectRootDirectory() . '/cache/Proxies';
184
            if (!is_dir($dir) && !(mkdir($dir, 0777, true) && is_dir($dir))) {
185
                throw new \RuntimeException(
186
                    'Proxy directory ' . $dir . ' does not exist and failed trying to create it'
187
                );
188
            }
189
190
            return $dir;
191
        } catch (\Exception $e) {
192
            throw new DoctrineStaticMetaException(
193
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
194
                $e->getCode(),
195
                $e
196
            );
197
        }
198
    }
199
200
    /**
201
     * @return UnderscoreNamingStrategy
202
     */
203
    private function getUnderscoreNamingStrategy(): UnderscoreNamingStrategy
204
    {
205
        return new UnderscoreNamingStrategy();
206
    }
207
208
    /**
209
     * @return string
210
     * @throws DoctrineStaticMetaException
211
     */
212
    private function getFilesystemCachePath(): string
213
    {
214
        $path = self::getProjectRootDirectory() . '/cache/dsm';
215
        if (!is_dir($path) && !(mkdir($path, 0777, true) && is_dir($path))) {
216
            throw new \RuntimeException('Failed creating default cache path at ' . $path);
217
        }
218
219
        return $path;
220
    }
221
222
    /**
223
     * @return string
224
     * @throws DoctrineStaticMetaException
225
     */
226
    private function calculateMigrationsDirectory(): string
227
    {
228
        $path = self::getProjectRootDirectory() . '/migrations';
229
        if (!is_dir($path) && !(mkdir($path, 0777, true) && is_dir($path))) {
230
            throw new \RuntimeException('Failed creating default migrations directory at ' . $path);
231
        }
232
233
        return $path;
234
    }
235
}
236