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 (#77)
by joseph
15:21
created

Config::__construct()   B

Complexity

Conditions 7
Paths 19

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
ccs 12
cts 12
cp 1
cc 7
nc 19
nop 1
crap 7
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\Exception\ConfigException;
8
use EdmondsCommerce\DoctrineStaticMeta\Exception\DoctrineStaticMetaException;
9
10
class Config implements ConfigInterface
11
{
12
13
    private static $projectRootDirectory;
14
    private $config = [];
15
16
    /**
17
     * Config constructor.
18
     *
19
     * @param array $server
20
     *
21
     * @throws ConfigException
22
     */
23 145
    public function __construct(array $server)
24
    {
25 145
        foreach (static::REQUIRED_PARAMS as $key) {
26 145
            if (!array_key_exists($key, $server)) {
27 1
                throw new ConfigException(
28 1
                    'required config param ' . $key . ' is not set in $server'
29
                );
30
            }
31 144
            $this->config[$key] = $server[$key];
32
        }
33 144
        foreach (static::OPTIONAL_PARAMS_WITH_DEFAULTS as $key => $value) {
34 144
            if (array_key_exists($key, $server)) {
35 144
                $this->config[$key] = $server[$key];
36
            }
37
        }
38 144
        foreach (static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS as $key => $value) {
39 144
            if (array_key_exists($key, $server)) {
40 144
                $this->config[$key] = $server[$key];
41
            }
42
        }
43 144
    }
44
45
    /**
46
     * @param string $key
47
     * @param mixed  $default
48
     *
49
     * @return mixed|string
50
     * @throws DoctrineStaticMetaException
51
     */
52 74
    public function get(string $key, $default = ConfigInterface::NO_DEFAULT_VALUE)
53
    {
54 74
        if (!isset(static::REQUIRED_PARAMS[$key])
55 74
            && !isset(static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key])
56 74
            && !isset(static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key])
57
        ) {
58
            throw new ConfigException(
59
                'Invalid config param '
60
                . $key
61
                . ', should be one of '
62
                . print_r(static::PARAMS, true)
63
            );
64
        }
65 74
        if (isset($this->config[$key])) {
66 72
            return $this->config[$key];
67
        }
68 72
        if (ConfigInterface::NO_DEFAULT_VALUE !== $default) {
69
            return $default;
70
        }
71 72
        if (isset(static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key])) {
72 71
            return static::OPTIONAL_PARAMS_WITH_DEFAULTS[$key];
73
        }
74 71
        if (isset(static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key])) {
75 71
            $method = static::OPTIONAL_PARAMS_WITH_CALCULATED_DEFAULTS[$key];
76
77 71
            return $this->$method();
78
        }
79
        throw new ConfigException(
80
            'No config set for param ' . $key . ' and no default provided'
81
        );
82
    }
83
84
    /**
85
     * Default Entities path, calculated default
86
     *
87
     * @return string
88
     * @throws DoctrineStaticMetaException
89
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
90
     */
91 1
    private function calculateEntitiesPath(): string
92
    {
93
        try {
94 1
            return self::getProjectRootDirectory() . '/src/Entities';
95
        } catch (\Exception $e) {
96
            throw new DoctrineStaticMetaException(
97
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
98
                $e->getCode(),
99
                $e
100
            );
101
        }
102
    }
103
104
    /**
105
     * Get the absolute path to the root of the current project
106
     *
107
     * It does this by working from the Composer autoloader which we know will be in a certain place in `vendor`
108
     *
109
     * @return string
110
     * @throws DoctrineStaticMetaException
111
     */
112 146
    public static function getProjectRootDirectory(): string
113
    {
114
        try {
115 146
            if (null === self::$projectRootDirectory) {
116
                $reflection                 = new \ts\Reflection\ReflectionClass(ClassLoader::class);
117
                self::$projectRootDirectory = \dirname($reflection->getFileName(), 3);
118
            }
119
120 146
            return self::$projectRootDirectory;
121
        } catch (\Exception $e) {
122
            throw new DoctrineStaticMetaException(
123
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
124
                $e->getCode(),
125
                $e
126
            );
127
        }
128
    }
129
130
    /**
131
     * Default Entities path, calculated default
132
     *
133
     * @return string
134
     * @throws DoctrineStaticMetaException
135
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
136
     */
137 70
    private function calculateProxyDir(): string
138
    {
139
        try {
140 70
            return self::getProjectRootDirectory() . '/cache/Proxies';
141
        } catch (\Exception $e) {
142
            throw new DoctrineStaticMetaException(
143
                'Exception in ' . __METHOD__ . ': ' . $e->getMessage(),
144
                $e->getCode(),
145
                $e
146
            );
147
        }
148
    }
149
150
    /**
151
     * @return UnderscoreNamingStrategy
152
     * @SuppressWarnings(PHPMD.UnusedPrivateMethod)
153
     */
154 70
    private function getUnderscoreNamingStrategy(): UnderscoreNamingStrategy
155
    {
156 70
        return new UnderscoreNamingStrategy();
157
    }
158
}
159