Completed
Pull Request — master (#556)
by
unknown
58:23 queued 23:21
created

InvalidateRoute::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 15
loc 15
ccs 5
cts 5
cp 1
rs 9.7666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the FOSHttpCacheBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
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 FOS\HttpCacheBundle\Configuration;
13
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationAnnotation;
15
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
16
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
17
18
/**
19
 * @Annotation
20
 */
21
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
22
class InvalidateRoute extends ConfigurationAnnotation
23
{
24
    /**
25
     * @var string
26
     */
27
    private $name;
28
29
    /**
30
     * @var array
31
     */
32
    private $params;
33
34 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
35
        $data = [],
36
        $params = []
37
    ) {
38 1
        $values = [];
39
        if (is_string($data)) {
40 1
            $values['value'] = $data;
41 1
        } else {
42
            $values = $data;
43
        }
44
45
        $values['params'] = $values['params'] ?? $params;
46 4
47
        parent::__construct($values);
48 4
    }
49 4
50
    /**
51
     * Handle route name given without explicit key.
52
     *
53
     * @param string $value The route name
54 2
     */
55
    public function setValue($value)
56 2
    {
57
        $this->setName($value);
58
    }
59
60
    /**
61
     * @param string $name
62 4
     */
63
    public function setName($name)
64 4
    {
65 1
        $this->name = $name;
66
    }
67 3
68 3
    /**
69 3
     * @return string
70 1
     */
71 1
    public function getName()
72
    {
73 1
        return $this->name;
74
    }
75
76
    /**
77
     * @param array $params
78
     */
79
    public function setParams($params)
80
    {
81
        if (!is_array($params)) {
82
            throw new \RuntimeException('InvalidateRoute params must be an array');
83
        }
84
        foreach ($params as $name => $value) {
85
            if (is_array($value)) {
86
                if (1 !== count($value) || !isset($value['expression'])) {
87 2
                    throw new \RuntimeException(sprintf(
88 2
                        '@InvalidateRoute param %s must be string or {"expression"="<expression>"}, %s given',
89
                        $name,
90
                        print_r($value, true)
91
                    ));
92
                }
93 2
                // @codeCoverageIgnoreStart
94
                if (!class_exists(ExpressionLanguage::class)) {
95 2
                    throw new InvalidConfigurationException(sprintf(
96
                        '@InvalidateRoute param %s uses an expression but the ExpressionLanguage is not available.',
97
                        $name
98
                    ));
99
                }
100
                // @codeCoverageIgnoreEnd
101 1
            }
102
        }
103 1
104
        $this->params = $params;
105
    }
106
107
    /**
108
     * @return array
109 1
     */
110
    public function getParams()
111 1
    {
112
        return $this->params;
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getAliasName()
119
    {
120
        return 'invalidate_route';
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function allowArray()
127
    {
128
        return true;
129
    }
130
}
131