Completed
Push — 1.x ( d1a99d...be31ef )
by Marko
02:10
created

ListReader::configureFields()   C

Complexity

Conditions 8
Paths 30

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
rs 5.3846
cc 8
eloc 16
nc 30
nop 2
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\ListAssociationField;
9
use KunicMarko\SonataAnnotationBundle\Annotation\ListField;
10
use Sonata\AdminBundle\Datagrid\ListMapper;
11
12
/**
13
 * @author Marko Kunic <[email protected]>
14
 */
15
class ListReader
16
{
17
    use AnnotationReaderTrait;
18
19
    public function configureFields(\ReflectionClass $class, ListMapper $listMapper): void
20
    {
21
        foreach ($class->getProperties() as $property) {
22
            foreach ($this->getPropertyAnnotations($property) as $annotation) {
23
                if ($annotation instanceof ListAssociationField) {
24
                    $this->addField(
25
                        $property->getName() . '.' . $annotation->field,
26
                        $annotation,
27
                        $listMapper
28
                    );
29
                    continue;
30
                }
31
32
                if ($annotation instanceof ListField) {
33
                    $this->addField($property->getName(), $annotation, $listMapper);
34
                }
35
            }
36
        }
37
38
        foreach ($class->getMethods() as $method) {
39
            if ($annotation = $this->getMethodAnnotation($method, ListField::class)) {
40
                $this->addField($property->getName(), $annotation, $listMapper);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $property seems to be defined by a foreach iteration on line 21. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
41
            }
42
        }
43
44
        if ($actions = $this->getListActions($this->getClassAnnotations($class))) {
45
            $listMapper->add('_action', null, [
46
                'actions' => $actions
47
            ]);
48
        }
49
    }
50
51
    private function addField(string $name, ListField $annotation, ListMapper $listMapper): void
52
    {
53
        if ($annotation->identifier) {
54
            $listMapper->addIdentifier($name, ...$annotation->getSettings());
0 ignored issues
show
Bug introduced by
$annotation->getSettings() is expanded, but the parameter $type of Sonata\AdminBundle\Datag...Mapper::addIdentifier() does not expect variable arguments. ( Ignorable by Annotation )

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

54
            $listMapper->addIdentifier($name, /** @scrutinizer ignore-type */ ...$annotation->getSettings());
Loading history...
55
            return;
56
        }
57
58
        $listMapper->add($name, ...$annotation->getSettings());
0 ignored issues
show
Bug introduced by
$annotation->getSettings() is expanded, but the parameter $type of Sonata\AdminBundle\Datagrid\ListMapper::add() does not expect variable arguments. ( Ignorable by Annotation )

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

58
        $listMapper->add($name, /** @scrutinizer ignore-type */ ...$annotation->getSettings());
Loading history...
59
    }
60
61
    private function getListActions(array $annotations): array
62
    {
63
        $actions = [];
64
65
        foreach ($annotations as $annotation) {
66
            if ($annotation instanceof ListAction) {
67
                $actions[$annotation->name] = $annotation->options;
68
            }
69
        }
70
71
        return $actions;
72
    }
73
}
74