Completed
Pull Request — 8.x-1.x (#7)
by
unknown
65:17
created

RouteTitleMethod::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Drupal\controller_annotations\RouteModifier\Annotated;
4
5
use Symfony\Component\Routing\Route;
6
7
/**
8
 * @Annotation
9
 */
10
class RouteTitleMethod implements AnnotatedRouteModifierInterface
11
{
12
    /**
13
     * @var string
14
     */
15
    private $titleMethodName;
16
17
    /**
18
     * @param array $values
19
     *
20
     * @throws \RuntimeException
21
     */
22
    public function __construct(array $values)
23
    {
24
        if (!isset($values['value'])) {
25
            throw new \RuntimeException('Title callback must be specified.');
26
        }
27
28
        $titleMethodName = $values['value'];
29
30
        if (!is_string($titleMethodName)) {
31
            throw new \RuntimeException('Title method name must be a string.');
32
        }
33
34
        $this->titleMethodName = $titleMethodName;
35
    }
36
37
    /**
38
     * @param \Symfony\Component\Routing\Route $route
39
     * @param \ReflectionClass $class
40
     * @param \ReflectionMethod $method
41
     * @param mixed $annot
42
     *
43
     * @throws \RuntimeException
44
     */
45
    public function modifyAnnotatedRoute(Route $route, \ReflectionClass $class, \ReflectionMethod $method, $annot)
46
    {
47
        if (!$class->hasMethod($this->titleMethodName)) {
48
            throw new \RuntimeException(
49
                sprintf(
50
                    'Title method %s does not exist on class %s.',
51
                    $this->titleMethodName,
52
                    $class->getName()));
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
53
        }
54
55
        $route->setDefault('_title_callback', $class->getName() . '::' . $this->titleMethodName);
0 ignored issues
show
Bug introduced by
Consider using $class->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
56
    }
57
}
58