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 (#58)
by joseph
18:31
created

Config::getProjectRootDirectory()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 4.679

Importance

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