Completed
Push — master ( 1fcafd...dc160c )
by Arnaud
18s queued 11s
created

AdminConfiguration::getPermissions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 0
cp 0
crap 6
rs 10
1
<?php
2
3
namespace LAG\AdminBundle\Configuration;
4
5
use JK\Configuration\Configuration;
6
use LAG\AdminBundle\Bridge\Doctrine\ORM\DataProvider\ORMDataProvider;
7
use LAG\AdminBundle\Configuration\Behavior\TranslationConfigurationTrait;
8
use LAG\AdminBundle\Controller\AdminAction;
9
use Symfony\Component\OptionsResolver\Options;
10
use Symfony\Component\OptionsResolver\OptionsResolver;
11
12
/**
13
 * Ease Admin configuration manipulation.
14
 */
15
class AdminConfiguration extends Configuration
16
{
17
    use TranslationConfigurationTrait;
18
19
    /**
20
     * @var ApplicationConfiguration
21
     */
22
    protected $application;
23
24
    /**
25
     * @var string
26
     */
27 2
    private $name;
28
29 2
    /**
30
     * AdminConfiguration constructor.
31 2
     */
32 2
    public function __construct(string $name, ApplicationConfiguration $application)
33
    {
34
        parent::__construct();
35
36
        $this->application = $application;
37
        $this->name = $name;
38
    }
39
40
    public function configureOptions(OptionsResolver $resolver)
41
    {
42
        $resolver
43
            ->setDefaults([
44
                'actions' => [
45
                    'list' => [],
46
                    'create' => [],
47
                    'edit' => [],
48
                    'delete' => [],
49
                ],
50
                'batch' => true,
51
                'class' => $this->application->getParameter('admin_class'),
52
                'routing_url_pattern' => $this->application->getParameter('routing_url_pattern'),
53
                'routing_name_pattern' => $this->application->getParameter('routing_name_pattern'),
54
                'controller' => AdminAction::class,
55
                'max_per_page' => $this->application->getParameter('max_per_page'),
56
                'form' => null,
57
                'form_options' => [],
58
                'pager' => $this->application->getParameter('pager'),
59
                'permissions' => $this->application->getParameter('permissions'),
60
                'string_length' => $this->application->getParameter('string_length'),
61
                'string_length_truncate' => $this->application->getParameter('string_length_truncate'),
62
                'date_format' => $this->application->getParameter('date_format'),
63
                'data_provider' => ORMDataProvider::class,
64
                'page_parameter' => $this->application->getParameter('page_parameter'),
65
                'list_template' => $this->application->get('list_template'),
66
                'edit_template' => $this->application->get('edit_template'),
67
                'create_template' => $this->application->get('create_template'),
68
                'delete_template' => $this->application->get('delete_template'),
69
                'menus' => $this->application->get('menus'),
70
            ])
71
            ->setRequired([
72
                'entity',
73
            ])
74
            ->setAllowedTypes('string_length', 'integer')
75
            ->setAllowedTypes('string_length_truncate', 'string')
76
            ->setAllowedTypes('page_parameter', 'string')
77
            ->setAllowedValues('pager', [
78
                null,
79
                'pagerfanta',
80
            ])
81
            ->setAllowedTypes('menus', ['array', 'null'])
82
            ->setNormalizer('actions', function (Options $options, $actions) {
83
                $normalizedActions = [];
84
                $addBatchAction = false;
85
86
                foreach ($actions as $name => $action) {
87
                    // action configuration is an array by default
88
                    if (null === $action) {
89
                        $action = [];
90
                    }
91
                    $normalizedActions[$name] = $action;
92
93
                    // in list action, if no batch was configured or disabled, we add a batch action
94
                    if ('list' == $name && (!array_key_exists('batch', $action) || null === $action['batch'])) {
95
                        $addBatchAction = true;
96
                    }
97
                }
98
99
                // add empty default batch action
100
                if ($addBatchAction) {
101
                    $normalizedActions['batch'] = [];
102
                }
103
104
                return $normalizedActions;
105
            })
106
        ;
107
108
        $this->configureTranslation(
109
            $resolver,
110
            $this->application->getTranslationPattern(),
111
            $this->application->getTranslationCatalog()
112
        );
113
    }
114
115
    public function getName(): string
116
    {
117
        return $this->name;
118
    }
119
120
    public function getPermissions(): array
121
    {
122
        $roles = explode(',', $this->get('permissions'));
123
124
        foreach ($roles as $index => $role) {
125
            $roles[$index] = trim($role);
126
        }
127
128
        return $roles;
129
    }
130
}
131