Passed
Push — develop ( b8535a...b19a66 )
by Mathieu
11:29
created

ListReader   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 24
dl 0
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getAdminAnnotationFields() 0 5 1
A getListActions() 0 21 5
A configureFields() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Neimheadh\SonataAnnotationBundle\Reader;
6
7
use Doctrine\Common\Annotations\Reader;
8
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\Admin;
9
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ListAction;
10
use Neimheadh\SonataAnnotationBundle\Annotation\Sonata\ListField;
11
use Neimheadh\SonataAnnotationBundle\Exception\MissingAnnotationArgumentException;
12
use ReflectionClass;
13
use Sonata\AdminBundle\Datagrid\ListMapper;
14
use Sonata\AdminBundle\Mapper\MapperInterface;
15
16
/**
17
 * List configuration reader.
18
 *
19
 * @author Marko Kunic <[email protected]>
20
 * @author Mathieu Wambre <[email protected]>
21
 */
22
final class ListReader extends AbstractFieldConfigurationReader
23
{
24
25
    /**
26
     * List default actions.
27
     */
28
    public const DEFAULT_ACTIONS = [
29
        'show' => [],
30
        'edit' => [],
31
        'delete' => [],
32
    ];
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    public function __construct(Reader $annotationReader)
38
    {
39
        parent::__construct($annotationReader, ListField::class);
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     *
45
     * @param ListMapper $mapper Admin list mapper.
46
     */
47
    public function configureFields(
48
        ReflectionClass $class,
49
        MapperInterface $mapper
50
    ): void {
51
        parent::configureFields($class, $mapper);
52
53
        if ($mapper instanceof ListMapper) {
54
            if (!empty($actions = $this->getListActions($class))) {
55
                $mapper->remove(ListMapper::NAME_ACTIONS);
56
                $mapper->add(ListMapper::NAME_ACTIONS, null, [
57
                    'actions' => $actions,
58
                ]);
59
            }
60
        }
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66
    protected function getAdminAnnotationFields(
67
        Admin $annotation,
68
        ?string $action
69
    ): array {
70
        return $annotation->getListFields();
71
    }
72
73
    /**
74
     * Get list of actions.
75
     *
76
     * @param ReflectionClass $class Entity class.
77
     *
78
     * @return array
79
     */
80
    private function getListActions(ReflectionClass $class): array
81
    {
82
        $actions = [];
83
        /** @var array<object|ListAction> $annotations */
84
        $annotations = $this->annotationReader->getClassAnnotations($class);
85
86
        foreach ($annotations as $annotation) {
87
            if ($annotation instanceof ListAction) {
88
                if (!isset($annotation->name)) {
89
                    throw new MissingAnnotationArgumentException(
90
                        $annotation,
91
                        'name',
92
                        $class
93
                    );
94
                }
95
96
                $actions[$annotation->name] = $annotation->options;
97
            }
98
        }
99
100
        return empty($actions) ? self::DEFAULT_ACTIONS : $actions;
101
    }
102
103
}
104