ServiceResolver::resolve()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 8.8571
cc 6
eloc 8
nc 4
nop 1
crap 6
1
<?php
2
3
/*
4
 * This file is part of the LaravelYaml package.
5
 *
6
 * (c) Théo FIDRY <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Fidry\LaravelYaml\FileLoader\Parser\Resolver;
13
14
use Fidry\LaravelYaml\DependencyInjection\Builder\BuilderInterface;
15
use Fidry\LaravelYaml\DependencyInjection\Definition\Reference;
16
use Symfony\Component\ExpressionLanguage\Expression;
17
18
/**
19
 * @author Théo FIDRY <[email protected]>
20
 */
21
final class ServiceResolver implements ResolverInterface
22
{
23
    /**
24
     * Resolves the given value.
25
     *
26
     * @example
27
     *  ::resolve("@=something") => return an Expression
28
     *  ::resolve("@something") => will resolve the service reference to return a Reference object
29
     *  ::resolve($arrayValue) => will recursively resolve the values
30
     *  Other values are left unchanged
31
     *
32
     * @param mixed $value
33
     *
34
     * @return mixed|Expression|Reference
35
     */
36 24
    public function resolve($value)
37
    {
38 24
        if (is_array($value)) {
39 2
            return array_map([$this, 'resolve'], $value);
40
        }
41
42 24
        if (is_string($value) && 0 === strpos($value, '@=')) {
43 6
            return new Expression(substr($value, 2));
44
        }
45
46 20
        if (is_string($value) && 0 === strpos($value, '@')) {
47 10
            return $this->resolveServiceReferenceValue($value);
48
        }
49
50 12
        return $value;
51
    }
52
53
    /**
54
     * @param string $value Service reference
55
     *
56
     * @example
57
     *  "@@dummy"
58
     *  "@?dummy"
59
     *  "@<something>"
60
     *
61
     * @return Reference
62
     */
63 10
    private function resolveServiceReferenceValue($value)
64
    {
65 10
        $value = substr($value, 1);
66 10
        $invalidBehavior = BuilderInterface::EXCEPTION_ON_INVALID_REFERENCE;
67
68 10
        if (0 === strpos($value, '@')) {
69 2
            $invalidBehavior = null;
70 9
        } elseif (0 === strpos($value, '?')) {
71 6
            $value = substr($value, 1);
72 6
            $invalidBehavior = BuilderInterface::IGNORE_ON_INVALID_REFERENCE;
73 3
        }
74
75 10
        return new Reference($value, $invalidBehavior);
76
    }
77
}
78