AuthChecker::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 4
nop 0
1
<?php declare(strict_types = 1);
2
3
namespace Wolnosciowiec\WebProxy\Service;
4
5
/**
6
 * Validates if the API token matches
7
 *
8
 * @package Wolnosciowiec\WebProxy\Service
9
 */
10
class AuthChecker
11
{
12
    /**
13
     * @var string[] $apiKey
14
     */
15
    private $apiKeys;
16
17
    public function __construct()
18
    {
19
        if (!is_file(__DIR__ . '/../../config.php')) {               // @codeCoverageIgnore
20
            throw new \Exception('config.php file was not found');   // @codeCoverageIgnore
21
        }
22
23
        $settings = require __DIR__ . '/../../config.php';
24
25
        if (!isset($settings['apiKey'])) {                                  // @codeCoverageIgnore
26
            throw new \Exception('apiKey should be defined in config.php'); // @codeCoverageIgnore
27
        }
28
29
        if (!is_array($settings['apiKey'])) {
30
            $settings['apiKey'] = [$settings['apiKey']];
31
        }
32
33
        $this->apiKeys = $settings['apiKey'];
34
    }
35
36
    /**
37
     * @param string $key
38
     * @return bool
39
     */
40
    public function validate($key)
41
    {
42
        return in_array($key, $this->apiKeys);
43
    }
44
}
45