Completed
Push — master ( 07fc20...4efd3c )
by David de
34:38
created

Tag::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 15
loc 15
ccs 4
cts 4
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 FOS\HttpCacheBundle\Exception\InvalidTagException;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationAnnotation;
16
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
17
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
18
19
/**
20
 * @Annotation
21
 */
22
#[\Attribute(\Attribute::IS_REPEATABLE | \Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
23
class Tag extends ConfigurationAnnotation
24
{
25
    private $tags;
26
27
    private $expression;
28
29 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...
30
        $data = [],
31
        $expression = null
32
    ) {
33 11
        $values = [];
34
        if (is_string($data)) {
35 11
            $values['value'] = $data;
36 11
        } else {
37
            $values = $data;
38
        }
39
40
        $values['expression'] = $values['expression'] ?? $expression;
41 3
42
        parent::__construct($values);
43
    }
44
45
    /**
46
     * Handle tags given without explicit key.
47
     *
48 3
     * @param string|array $data
49 3
     */
50
    public function setValue($data)
51
    {
52
        $this->setTags(is_array($data) ? $data : [$data]);
53
    }
54 11
55
    /**
56 11
     * @param mixed $expression
57
     */
58
    public function setExpression($expression)
59 12
    {
60
        // @codeCoverageIgnoreStart
61 12
        if (!class_exists(ExpressionLanguage::class)) {
62 12
            throw new InvalidConfigurationException('@Tag param uses an expression but the ExpressionLanguage is not available.');
63 1
        }
64
        // @codeCoverageIgnoreEnd
65
        $this->expression = $expression;
66
    }
67 11
68 11
    /**
69
     * @return mixed
70 9
     */
71
    public function getExpression()
72 9
    {
73
        return $this->expression;
74
    }
75
76
    public function setTags(array $tags)
77
    {
78 4
        foreach ($tags as $tag) {
79
            if (false !== \strpos($tag, ',')) {
80 4
                throw new InvalidTagException($tag, ',');
81
            }
82
        }
83
84
        $this->tags = $tags;
85
    }
86 4
87
    public function getTags()
88 4
    {
89
        return $this->tags;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getAliasName()
96
    {
97
        return 'tag';
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function allowArray()
104
    {
105
        return true;
106
    }
107
}
108