Completed
Push — 8.x-1.x ( 4cc04a...006414 )
by
unknown
27:53
created

Title::modifyRouteClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Drupal\controller_annotations\Configuration;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationAnnotation;
6
use Symfony\Component\Routing\Route as RoutingRoute;
7
8
/**
9
 * @Annotation
10
 */
11
class Title extends ConfigurationAnnotation implements RouteModifierMethodInterface, RouteModifierClassInterface
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $value;
17
18
    /**
19
     * @var array
20
     */
21
    protected $arguments;
22
23
    /**
24
     * @var array
25
     */
26
    protected $context;
27
28
    /**
29
     * @var string
30
     */
31
    protected $callback;
32
33
    /**
34
     * @param $title
35
     */
36 7
    public function setValue($title)
37
    {
38 7
        $this->setTitle($title);
39 7
    }
40
41
    /**
42
     * @return bool
43
     */
44 8
    public function hasTitle()
45
    {
46 8
        return !empty($this->value);
47
    }
48
49
    /**
50
     * @return string
51
     */
52 7
    public function getTitle()
53
    {
54 7
        return $this->value;
55
    }
56
57
    /**
58
     * @param string $title
59
     */
60 7
    public function setTitle($title)
61
    {
62 7
        $this->value = $title;
63 7
    }
64
65
    /**
66
     * @return bool
67
     */
68 8
    public function hasArguments()
69
    {
70 8
        return !empty($this->arguments);
71
    }
72
73
    /**
74
     * @return array
75
     */
76 7
    public function getArguments()
77
    {
78 7
        return $this->arguments;
79
    }
80
81
    /**
82
     * @param array $arguments
83
     */
84 7
    public function setArguments(array $arguments)
85
    {
86 7
        $this->arguments = $arguments;
87 7
    }
88
89
    /**
90
     * @return bool
91
     */
92 8
    public function hasContext()
93
    {
94 8
        return !empty($this->context);
95
    }
96
97
    /**
98
     * @return array
99
     */
100 1
    public function getContext()
101
    {
102 1
        return $this->context;
103
    }
104
105
    /**
106
     * @param array $context
107
     */
108 1
    public function setContext(array $context)
109
    {
110 1
        $this->context = $context;
111 1
    }
112
113
    /**
114
     * @return bool
115
     */
116 8
    public function hasCallback()
117
    {
118 8
        return !empty($this->callback);
119
    }
120
121
    /**
122
     * @return string
123
     */
124 8
    public function getCallback()
125
    {
126 8
        return $this->callback;
127
    }
128
129
    /**
130
     * @param string $callback
131
     */
132 8
    public function setCallback($callback)
133
    {
134 8
        $this->callback = $callback;
135 8
    }
136
137
    /**
138
     * @return string
139
     */
140 1
    public function getAliasName()
141
    {
142 1
        return 'title';
143
    }
144
145
    /**
146
     * @return bool
147
     */
148 1
    public function allowArray()
149
    {
150 1
        return false;
151
    }
152
153
    /**
154
     * @param RoutingRoute $route
155
     * @param \ReflectionClass $class
156
     * @param \ReflectionMethod $method
157
     */
158 1
    public function modifyRouteClass(RoutingRoute $route, \ReflectionClass $class, \ReflectionMethod $method)
159
    {
160 1
        $this->modifyRoute($route, $class);
161 1
    }
162
163
    /**
164
     * @param RoutingRoute $route
165
     * @param \ReflectionClass $class
166
     * @param \ReflectionMethod $method
167
     */
168 7
    public function modifyRouteMethod(RoutingRoute $route, \ReflectionClass $class, \ReflectionMethod $method)
169
    {
170 7
        $this->modifyRoute($route, $class);
171 7
    }
172
173
    /**
174
     * @param RoutingRoute $route
175
     * @param \ReflectionClass $class
176
     */
177 8
    protected function modifyRoute(RoutingRoute $route, \ReflectionClass $class)
178
    {
179 8
        if ($this->hasTitle()) {
180 7
            $route->setDefault('_title', $this->getTitle());
181
        }
182 8
        if ($this->hasArguments()) {
183 7
            $route->setDefault('_title_arguments', $this->getArguments());
184
        }
185 8
        if ($this->hasContext()) {
186 1
            $route->setDefault('_title_context', $this->getContext());
187
        }
188
189 8
        $this->registerCallback($route, $class);
190 8
    }
191
192
    /**
193
     * @param RoutingRoute $route
194
     * @param \ReflectionClass $class
195
     */
196 8 View Code Duplication
    protected function registerCallback(RoutingRoute $route, \ReflectionClass $class)
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...
197
    {
198 8
        if ($this->hasCallback()) {
199 8
            if (strpos($this->getCallback(), '::') === false && $class->hasMethod($this->getCallback())) {
200 7
                $this->setCallback(sprintf('%s::%s', $class->getName(), $this->getCallback()));
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
201
            }
202 8
            $route->setDefault('_title_callback', $this->getCallback());
203
        }
204 8
    }
205
}
206