Passed
Push — master ( 26f9dc...b66765 )
by Marko
02:57
created

ListReader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
dl 0
loc 66
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A configureFields() 0 7 2
A getListActions() 0 13 3
A isSupported() 0 3 1
A getAnnotation() 0 3 1
A addPropertiesToMapper() 0 4 2
A addField() 0 8 2
A findProperties() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\Reader;
6
7
use KunicMarko\SonataAnnotationBundle\Annotation\ListAction;
8
use KunicMarko\SonataAnnotationBundle\Annotation\ListField;
9
use Sonata\AdminBundle\Mapper\BaseMapper;
10
11
/**
12
 * @author Marko Kunic <[email protected]>
13
 */
14
class ListReader extends AbstractReader
15
{
16
    public function configureFields(\ReflectionClass $class, BaseMapper $baseMapper): void
17
    {
18
        parent::configureFields($class, $baseMapper);
19
20
        if ($actions = $this->getListActions($this->getClassAnnotations($class))) {
21
            $baseMapper->add('_action', null, [
0 ignored issues
show
Bug introduced by
The method add() does not exist on Sonata\AdminBundle\Mapper\BaseMapper. It seems like you code against a sub-type of said class. However, the method does not exist in Sonata\AdminBundle\Mapper\BaseGroupedMapper. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
            $baseMapper->/** @scrutinizer ignore-call */ 
22
                         add('_action', null, [
Loading history...
22
                'actions' => $actions
23
            ]);
24
        }
25
    }
26
27
    private function getListActions(array $annotations): array
28
    {
29
        $actions = [];
30
31
        foreach($annotations as $annotation) {
32
            if (!$this->isSupported($annotation)) {
33
                continue;
34
            }
35
36
            $actions[$annotation->name] = $annotation->options;
37
        }
38
39
        return $actions;
40
    }
41
42
    private function isSupported($annotation): bool
43
    {
44
        return $annotation instanceof ListAction;
45
    }
46
47
    protected function findProperties(\ReflectionClass $class): array
48
    {
49
        $fields = parent::findProperties($class);
50
51
        foreach ($class->getMethods() as $method) {
52
            if ($annotation = $this->getMethodAnnotation($method, $this->getAnnotation())) {
53
                $fields[$method->getName()] = $annotation;
54
            }
55
        }
56
57
        return $fields;
58
    }
59
60
    protected function addPropertiesToMapper(array $properties, BaseMapper $baseMapper): void
61
    {
62
        foreach ($properties as $name => $annotation) {
63
            $this->addField($name, $annotation, $baseMapper);
64
        }
65
    }
66
67
    private function addField(string $name, ListField $annotation, BaseMapper $baseMapper): void
68
    {
69
        if ($annotation->identifier) {
70
            $baseMapper->addIdentifier($name, ...$annotation->getSettings());
0 ignored issues
show
Bug introduced by
The method addIdentifier() does not exist on Sonata\AdminBundle\Mapper\BaseMapper. It seems like you code against a sub-type of Sonata\AdminBundle\Mapper\BaseMapper such as Sonata\AdminBundle\Datagrid\ListMapper. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
            $baseMapper->/** @scrutinizer ignore-call */ 
71
                         addIdentifier($name, ...$annotation->getSettings());
Loading history...
71
            return;
72
        }
73
74
        $baseMapper->add($name, ...$annotation->getSettings());
75
    }
76
77
    protected function getAnnotation(): string
78
    {
79
        return ListField::class;
80
    }
81
}
82