Passed
Pull Request — 1.x (#9)
by Marko
03:23
created

ListReader::addField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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
final 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->getField(),
26
                        $annotation,
27
                        $listMapper
28
                    );
29
30
                    continue;
31
                }
32
33
                if ($annotation instanceof ListField) {
34
                    $this->addField($property->getName(), $annotation, $listMapper);
35
                }
36
            }
37
        }
38
39
        foreach ($class->getMethods() as $method) {
40
            if ($annotation = $this->getMethodAnnotation($method, ListField::class)) {
41
                $this->addField($method->getName(), $annotation, $listMapper);
42
            }
43
        }
44
45
        if ($actions = $this->getListActions($this->getClassAnnotations($class))) {
46
            $listMapper->add('_action', null, [
47
                'actions' => $actions
48
            ]);
49
        }
50
    }
51
52
    private function addField(string $name, ListField $annotation, ListMapper $listMapper): void
53
    {
54
        if ($annotation->identifier) {
55
            $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

55
            $listMapper->addIdentifier($name, /** @scrutinizer ignore-type */ ...$annotation->getSettings());
Loading history...
56
            return;
57
        }
58
59
        $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

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