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 (#132)
by joseph
47:28
created

Config::getProjectRootDirectory()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.087

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
ccs 3
cts 10
cp 0.3
rs 9.9332
c 0
b 0
f 0
nc 4
nop 0
cc 3
crap 6.087
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 1
    public function __construct(array $server)
32
    {
33 1
        foreach (static::REQUIRED_PARAMS as $key) {
34 1
            if (!\array_key_exists($key, $server)) {
35 1
                throw new ConfigException(
36 1
                    '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 4
    public function get(string $key, $default = ConfigInterface::NO_DEFAULT_VALUE)
60
    {
61 4
        if (!isset(static::REQUIRED_PARAMS[$key])
62 4
            && !isset(static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key])
63 4
            && !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 4
        if (isset($this->config[$key])) {
73 4
            return $this->config[$key];
74
        }
75 4
        if (ConfigInterface::NO_DEFAULT_VALUE !== $default) {
76
            return $default;
77
        }
78 4
        if (isset(static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key])) {
79 4
            return static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key];
80
        }
81 4
        if (isset(static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key])) {
82 4
            $method = static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key];
83
84 4
            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 1
    private function validateConfig(): void
96
    {
97 1
        $errors     = [];
98 1
        $typeHelper = new TypeHelper();
99 1
        foreach (ConfigInterface::PARAM_TYPES as $param => $requiredType) {
100 1
            $value = $this->get($param);
101 1
            if (self::TYPE_BOOL === $requiredType
102 1
                && is_numeric($value)
103 1
                && \in_array((int)$value, [0, 1], true)
104
            ) {
105 1
                $this->config[$param] = ($value === 1);
106 1
                continue;
107
            }
108 1
            if (\is_object($value)) {
109 1
                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 1
                continue;
116
            }
117 1
            $actualType = $typeHelper->getType($value);
118 1
            if ($actualType !== $requiredType) {
119
                $valueString = var_export($value, true);
120
                $errors[]    = ' ERROR  ' . $param . ' is not of the required type [' . $requiredType . ']'
121 1
                               . ' currently configured as type [' . $actualType . '] with value: ' . $valueString;
122
            }
123
        }
124 1
        if ([] !== $errors) {
125
            throw new ConfigException(implode("\n\n", $errors));
126
        }
127 1
    }
128
129
    /**
130
     * Default Entities path, calculated default
131
     *
132
     * @return string
133
     * @throws DoctrineStaticMetaException
134
     */
135 1
    private function calculateEntitiesPath(): string
136
    {
137
        try {
138 1
            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 1
    public static function getProjectRootDirectory(): string
157
    {
158
        try {
159 1
            if (null === self::$projectRootDirectory) {
160
                $reflection                 = new \ts\Reflection\ReflectionClass(ClassLoader::class);
161
                self::$projectRootDirectory = \dirname($reflection->getFileName(), 3);
162
            }
163
164 1
            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