Passed
Push — master ( fad3cd...8c9440 )
by Michael
02:31
created

AbstractProcessor::resolveGetter()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 8
Ratio 47.06 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 8
loc 17
ccs 0
cts 8
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 3
nop 2
crap 20
1
<?php
2
declare(strict_types = 1);
3
4
namespace Mikemirten\Component\JsonApi\Mapper\Definition\ConfigurationProcessor;
5
6
use Mikemirten\Component\JsonApi\Mapper\Definition\Link;
7
8
/**
9
 * Abstract processor
10
 * Contains shared methods
11
 *
12
 * @package Mikemirten\Component\JsonApi\Mapper\Definition\ConfigurationProcessor
13
 */
14
abstract class AbstractProcessor implements ConfigurationProcessorInterface
15
{
16
    /**
17
     * Pattern of "resource" parameter of link annotation
18
     */
19
    const RESOURCE_PATTERN = '~^(?<repository>[a-z_][a-z0-9_]*)\.(?<link>[a-z_][a-z0-9_]*)$~i';
20
21
    /**
22
     * Create link
23
     *
24
     * @param  string $name
25
     * @param  array  $data
26
     * @return Link
27
     */
28 2
    public function createLink(string $name, array $data): Link
29
    {
30 2
        if (! preg_match(self::RESOURCE_PATTERN, $data['resource'], $matches)) {
31
            throw new \LogicException(sprintf('Invalid resource definition: "%s"', $data['resource']));
32
        }
33
34 2
        $link = new Link(
35 2
            $name,
36 2
            $matches['repository'],
37 2
            $matches['link']
38
        );
39
40 2
        $link->setParameters($data['parameters']);
41 2
        $link->setMetadata($data['metadata']);
42
43 2
        return $link;
44
    }
45
46
    /**
47
     * Resolve getter of related object
48
     *
49
     * @param  \ReflectionClass $reflection
50
     * @param  string           $name
51
     * @return string
52
     */
53
    protected function resolveGetter(\ReflectionClass $reflection, string $name)
54
    {
55 View Code Duplication
        foreach (['get', 'is'] as $prefix)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
        {
57
            $getter = $prefix . ucfirst($name);
58
59
            if ($reflection->hasMethod($getter) && $reflection->getMethod($getter)->isPublic()) {
60
                return $getter;
61
            }
62
        }
63
64
        throw new \LogicException(sprintf(
65
            'Getter-method for "%s" cannot be resolved automatically. ' .
66
            'Probably there is no get%2$s() or is%2$s() method or it is not public.',
67
            $name, ucfirst($name)
68
        ));
69
    }
70
}