AnnotationProvider::getFromString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Pgs\HashIdBundle\AnnotationProvider;
6
7
use Doctrine\Common\Annotations\Reader;
8
use Pgs\HashIdBundle\Exception\InvalidControllerException;
9
use Pgs\HashIdBundle\Exception\MissingClassOrMethodException;
10
use Pgs\HashIdBundle\Reflection\ReflectionProvider;
11
12
class AnnotationProvider implements AnnotationProviderInterface
13
{
14
    /**
15
     * @var Reader
16
     */
17
    protected $reader;
18
19
    /**
20
     * @var ReflectionProvider
21
     */
22
    protected $reflectionProvider;
23
24 5
    public function __construct(Reader $reader, ReflectionProvider $reflectionProvider)
25
    {
26 5
        $this->reader = $reader;
27 5
        $this->reflectionProvider = $reflectionProvider;
28 5
    }
29
30
    /**
31
     * @return object|null
32
     */
33 3
    public function getFromString(string $controller, string $annotationClassName)
34
    {
35 3
        $explodedControllerString = explode('::', $controller);
36 3
        if (2 !== \count($explodedControllerString)) {
37 1
            $message = sprintf('The "%s" controller is not a valid "class::method" string.', $controller);
38 1
            throw new InvalidControllerException($message);
39
        }
40 2
        $reflection = $this->reflectionProvider->getMethodReflectionFromClassString(...$explodedControllerString);
41
42 2
        return $this->reader->getMethodAnnotation($reflection, $annotationClassName);
43
    }
44
45
    /**
46
     * @param object $controller
47
     *
48
     * @throws InvalidControllerException
49
     * @throws MissingClassOrMethodException
50
     *
51
     * @return object|null
52
     */
53 2
    public function getFromObject($controller, string $method, string $annotationClassName)
54
    {
55 2
        if (!\is_object($controller)) {
56 1
            throw new InvalidControllerException('Provided controller is not an object');
57
        }
58 1
        $reflection = $this->reflectionProvider->getMethodReflectionFromObject($controller, $method);
59
60 1
        return $this->reader->getMethodAnnotation($reflection, $annotationClassName);
61
    }
62
}
63