Completed
Push — master ( ad7fd3...b10ab8 )
by Théo
03:40
created

ServiceResolver   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 9
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 57
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B resolve() 0 16 6
A resolveServiceReferenceValue() 0 14 3
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
    public function resolve($value)
37
    {
38
        if (is_array($value)) {
39
            return array_map([$this, 'resolve'], $value);
40
        }
41
42
        if (is_string($value) && 0 === strpos($value, '@=')) {
43
            return new Expression(substr($value, 2));
44
        }
45
46
        if (is_string($value) && 0 === strpos($value, '@')) {
47
            return $this->resolveServiceReferenceValue($value);
48
        }
49
50
        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
    private function resolveServiceReferenceValue($value)
64
    {
65
        $value = substr($value, 1);
66
        $invalidBehavior = BuilderInterface::EXCEPTION_ON_INVALID_REFERENCE;
67
68
        if (0 === strpos($value, '@')) {
69
            $invalidBehavior = null;
70
        } elseif (0 === strpos($value, '?')) {
71
            $value = substr($value, 1);
72
            $invalidBehavior = BuilderInterface::IGNORE_ON_INVALID_REFERENCE;
73
        }
74
75
        return new Reference($value, $invalidBehavior);
76
    }
77
}
78