Failed Conditions
Pull Request — master (#23)
by Emanuele
20:34
created

FeatureAdmin::getObjectCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Ae\FeatureBundle\Admin;
4
5
use Ae\FeatureBundle\Entity\FeatureManager;
6
use Sonata\AdminBundle\Admin\Admin;
7
use Sonata\AdminBundle\Datagrid\DatagridMapper;
8
use Sonata\AdminBundle\Datagrid\ListMapper;
9
use Sonata\AdminBundle\Form\FormMapper;
10
11
/**
12
 * @author Carlo Forghieri <[email protected]>
13
 */
14
class FeatureAdmin extends Admin
15
{
16
    /**
17
     * {@inheritdoc}
18
     */
19
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
20
    {
21
        $datagridMapper
22
            ->add('name')
23
            ->add('enabled');
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    protected function configureListFields(ListMapper $listMapper)
30
    {
31
        $listMapper
32
            ->add('id')
33
            ->add('name')
34
            ->add('role')
35
            ->add('enabled')
36
            ->add('_action', 'actions', [
37
                'actions' => [
38
                    'edit' => [],
39
                    'delete' => [],
40
                ],
41
            ]);
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected function configureFormFields(FormMapper $formMapper)
48
    {
49
        $roles = $this->getRoles();
50
51
        $formMapper
52
            ->add('name', 'text', [
53
                'required' => true,
54
            ])
55
            ->add('enabled', 'checkbox', [
56
                'required' => false,
57
            ])
58
            ->add('role', 'choice', [
59
                'choices' => array_combine($roles, $roles),
60
                'multiple' => false,
61
                'required' => false,
62
            ]);
63
64
        if (!$this->getSubject()->getParent()) {
65
            $formMapper->add(
66
                'children',
67
                'sonata_type_collection',
68
                [
69
                    'required' => false,
70
                ],
71
                [
72
                    'edit' => 'inline',
73
                    'inline' => 'table',
74
                ]
75
            );
76
        }
77
    }
78
79
    protected function getRoles()
80
    {
81
        $roleHierarchy = $this
82
            ->getConfigurationPool()
83
            ->getContainer()
84
            ->getParameter('security.role_hierarchy.roles');
85
86
        $roles = array_keys($roleHierarchy);
87
        $roles[] = 'ROLE_PREVIOUS_ADMIN';
88
89
        return $roles;
90
    }
91
92
    public function createQuery($context = 'list')
93
    {
94
        $query = parent::createQuery($context);
95
        if ($context === 'list') {
96
            $alias = current($query->getDQLPart('from'))->getAlias();
97
            $query->andWhere($alias.'.parent IS NULL');
98
        }
99
100
        return $query;
101
    }
102
103
    public function postUpdate($object)
104
    {
105
        $cache = $this->modelManager
0 ignored issues
show
Bug introduced by
The method getEntityManager() does not seem to exist on object<Sonata\AdminBundl...\ModelManagerInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
            ->getEntityManager($object)
107
            ->getConfiguration()
108
            ->getResultCacheImpl();
109
110
        foreach ($object->getChildren() as $child) {
111
            $cache->delete($this->getObjectCacheKey($child));
112
        }
113
    }
114
115
    protected function getObjectCacheKey($object)
116
    {
117
        return FeatureManager::generateCacheKey(
118
            $object->getParent()->getName(),
119
            $object->getName()
120
        );
121
    }
122
}
123