Passed
Push — master ( 021f1c...1478fc )
by Evgeniy
01:24 queued 11s
created

KeyValue   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 46
c 5
b 2
f 0
dl 0
loc 92
ccs 52
cts 52
cp 1
rs 10
wmc 27

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A compile() 0 13 6
A stringToType() 0 6 2
A canProvide() 0 3 1
A provide() 0 6 2
A stringToAlias() 0 10 3
A transform() 0 9 3
B transformString() 0 21 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI\Provider;
6
7
use Cekta\DI\Loader\Alias;
8
use Cekta\DI\Provider\Exception\NotFound;
9
use Cekta\DI\ProviderInterface;
10
use InvalidArgumentException;
11
12
class KeyValue implements ProviderInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    private $values;
18
19 24
    public function __construct(array $values)
20
    {
21 24
        $this->values = $values;
22 24
    }
23
24 3
    public static function stringToAlias(array $values): self
25
    {
26 3
        $result = [];
27 3
        foreach ($values as $key => $value) {
28 3
            if (is_string($value)) {
29 3
                $value = new Alias($value);
30
            }
31 3
            $result[$key] = $value;
32
        }
33 3
        return new static($result);
34
    }
35
36 3
    public static function stringToType(array $params): self
37
    {
38 3
        foreach ($params as $key => $value) {
39 3
            $params[$key] = self::transform($value);
40
        }
41 3
        return new static($params);
42
    }
43
44 9
    public static function compile(string $compiledFile, callable $callable): self
45
    {
46
        if (
47 9
            (!file_exists($compiledFile) && !is_writable(dirname($compiledFile)))
48 9
            || (file_exists($compiledFile) && !is_writable($compiledFile))
49
        ) {
50 3
            throw new InvalidArgumentException("`$compiledFile` must be writable");
51
        }
52 6
        if (!is_readable($compiledFile)) {
53 3
            file_put_contents($compiledFile, call_user_func($callable));
54
        }
55
        /** @noinspection PhpIncludeInspection */
56 6
        return new static(require $compiledFile);
57
    }
58
59 18
    public function provide(string $id)
60
    {
61 18
        if (!$this->canProvide($id)) {
62 3
            throw new NotFound($id);
63
        }
64 15
        return $this->values[$id];
65
    }
66
67 21
    public function canProvide(string $id): bool
68
    {
69 21
        return array_key_exists($id, $this->values);
70
    }
71
72 3
    private static function transform($value)
73
    {
74 3
        if (is_string($value)) {
75 3
            if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
76 3
                $value = $matches[2];
77
            }
78 3
            $value = self::transformString($value);
79
        }
80 3
        return $value;
81
    }
82
83 3
    private static function transformString(string $value)
84
    {
85 3
        switch (strtolower($value)) {
86 3
            case '(true)':
87 3
            case 'true':
88 3
                $value = true;
89 3
                break;
90 3
            case '(false)':
91 3
            case 'false':
92 3
                $value = false;
93 3
                break;
94 3
            case '(null)':
95 3
            case 'null':
96 3
                $value = null;
97 3
                break;
98 3
            case '(empty)':
99 3
            case 'empty':
100 3
                $value = '';
101 3
                break;
102
        }
103 3
        return $value;
104
    }
105
}
106