Passed
Push — develop ( 3d1775...ebd477 )
by Mathieu
02:47
created

ListReader::addField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace KunicMarko\SonataAnnotationBundle\Reader;
6
7
use Doctrine\Common\Annotations\Reader;
8
use KunicMarko\SonataAnnotationBundle\Annotation\ListAction;
9
use KunicMarko\SonataAnnotationBundle\Annotation\ListField;
10
use KunicMarko\SonataAnnotationBundle\Exception\MissingAnnotationArgumentException;
11
use ReflectionClass;
12
use Sonata\AdminBundle\Datagrid\ListMapper;
13
use Sonata\AdminBundle\Mapper\MapperInterface;
14
15
/**
16
 * List configuration reader.
17
 *
18
 * @author Marko Kunic <[email protected]>
19
 * @author Mathieu Wambre <[email protected]>
20
 */
21
final class ListReader extends AbstractFieldConfigurationReader
22
{
23
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function __construct(Reader $annotationReader)
28
    {
29
        parent::__construct($annotationReader, ListField::class);
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     *
35
     * @param ListMapper $mapper Admin list mapper.
36
     */
37
    public function configureFields(
38
        ReflectionClass $class,
39
        MapperInterface $mapper
40
    ): void {
41
        parent::configureFields($class, $mapper);
42
43
        if ($actions = $this->getListActions($class)) {
44
            $mapper->add('_action', null, [
0 ignored issues
show
Bug introduced by
The method add() does not exist on Sonata\AdminBundle\Mapper\MapperInterface. 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

44
            $mapper->/** @scrutinizer ignore-call */ 
45
                     add('_action', null, [
Loading history...
45
                'actions' => $actions,
46
            ]);
47
        }
48
    }
49
50
    /**
51
     * Get list of actions.
52
     *
53
     * @param ReflectionClass $class Entity class.
54
     *
55
     * @return array
56
     */
57
    private function getListActions(ReflectionClass $class): array
58
    {
59
        $actions = [];
60
        /** @var array<object|ListAction> $annotations */
61
        $annotations = $this->annotationReader->getClassAnnotations($class);
62
63
        foreach ($annotations as $annotation) {
64
            if ($annotation instanceof ListAction) {
65
                if (!isset($annotation->name)) {
66
                    throw new MissingAnnotationArgumentException(
67
                        $annotation,
68
                        'name',
69
                        $class
70
                    );
71
                }
72
73
                $actions[$annotation->name] = $annotation->options;
74
            }
75
        }
76
77
        return $actions;
78
    }
79
80
}
81