UserGroup::addAdminResources()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Form\Factory;
6
7
use AbterPhp\Admin\Domain\Entities\AdminResource;
8
use AbterPhp\Admin\Domain\Entities\UserGroup as Entity;
9
use AbterPhp\Admin\Orm\AdminResourceRepo;
10
use AbterPhp\Framework\Constant\Html5;
11
use AbterPhp\Framework\Form\Component\Option;
12
use AbterPhp\Framework\Form\Container\FormGroup;
13
use AbterPhp\Framework\Form\Element\Input;
14
use AbterPhp\Framework\Form\Element\MultiSelect;
15
use AbterPhp\Framework\Form\Element\Select;
16
use AbterPhp\Framework\Form\Extra\Help;
17
use AbterPhp\Framework\Form\IForm;
18
use AbterPhp\Framework\Form\Label\Label;
19
use AbterPhp\Framework\Html\Helper\Attributes;
20
use AbterPhp\Framework\I18n\ITranslator;
21
use Opulence\Orm\IEntity;
22
use Opulence\Orm\OrmException;
23
use Opulence\Sessions\ISession;
24
25
class UserGroup extends Base
26
{
27
    protected AdminResourceRepo $adminResourceRepo;
28
29
    /**
30
     * UserGroup constructor.
31
     *
32
     * @param ISession          $session
33
     * @param ITranslator       $translator
34
     * @param AdminResourceRepo $adminResourceRepo
35
     */
36
    public function __construct(ISession $session, ITranslator $translator, AdminResourceRepo $adminResourceRepo)
37
    {
38
        parent::__construct($session, $translator);
39
40
        $this->adminResourceRepo = $adminResourceRepo;
41
    }
42
43
    /**
44
     * @param string       $action
45
     * @param string       $method
46
     * @param string       $showUrl
47
     * @param IEntity|null $entity
48
     *
49
     * @return IForm
50
     * @throws OrmException
51
     */
52
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
53
    {
54
        assert($entity instanceof Entity, new \InvalidArgumentException());
55
56
        $this->createForm($action, $method)
57
            ->addDefaultElements()
58
            ->addName($entity)
59
            ->addIdentifier($entity)
60
            ->addAdminResources($entity)
61
            ->addDefaultButtons($showUrl);
62
63
        $form = $this->form;
64
65
        $this->form = null;
66
67
        return $form;
68
    }
69
70
    /**
71
     * @param Entity $entity
72
     *
73
     * @return $this
74
     */
75
    protected function addName(Entity $entity): UserGroup
76
    {
77
        $input = new Input('name', 'name', $entity->getName());
78
        $label = new Label('body', 'admin:userGroupName');
79
80
        $attributes   = Attributes::fromArray([Html5::ATTR_CLASS => FormGroup::CLASS_REQUIRED]);
81
        $this->form[] = new FormGroup($input, $label, null, [], $attributes);
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param Entity $entity
88
     *
89
     * @return $this
90
     */
91
    protected function addIdentifier(Entity $entity): UserGroup
92
    {
93
        $input = new Input(
94
            'identifier',
95
            'identifier',
96
            $entity->getIdentifier(),
97
            [],
98
            Attributes::fromArray([Html5::ATTR_CLASS => 'semi-auto'])
99
        );
100
        $label = new Label('identifier', 'admin:userGroupIdentifier');
101
        $help  = new Help('admin:userGroupIdentifierHelp');
102
103
        $this->form[] = new FormGroup($input, $label, $help);
104
105
        return $this;
106
    }
107
108
    /**
109
     * @param Entity $entity
110
     *
111
     * @return $this
112
     * @throws OrmException
113
     */
114
    protected function addAdminResources(Entity $entity): UserGroup
115
    {
116
        $allAdminResources = $this->getAllAdminResources();
117
        $adminResourceIds  = $this->getAdminResourceIds($entity);
118
119
        $options = $this->createAdminResourceOptions($allAdminResources, $adminResourceIds);
120
121
        $this->form[] = new FormGroup(
122
            $this->createAdminResourceSelect($options),
123
            $this->createAdminResourceLabel()
124
        );
125
126
        return $this;
127
    }
128
129
    /**
130
     * @return AdminResource[]
131
     * @throws OrmException
132
     */
133
    protected function getAllAdminResources(): array
134
    {
135
        return $this->adminResourceRepo->getAll();
136
    }
137
138
    /**
139
     * @param Entity $entity
140
     *
141
     * @return int[]
142
     */
143
    protected function getAdminResourceIds(Entity $entity): array
144
    {
145
        $adminResourceIds = [];
146
        foreach ($entity->getAdminResources() as $adminResource) {
147
            $adminResourceIds[] = $adminResource->getId();
148
        }
149
150
        return $adminResourceIds;
151
    }
152
153
    /**
154
     * @param AdminResource[] $allAdminResources
155
     * @param int[]           $adminResourceIds
156
     *
157
     * @return array
158
     */
159
    protected function createAdminResourceOptions(array $allAdminResources, array $adminResourceIds): array
160
    {
161
        $options = [];
162
        foreach ($allAdminResources as $adminResource) {
163
            $options[] = new Option(
164
                (string)$adminResource->getId(),
165
                $adminResource->getIdentifier(),
166
                in_array($adminResource->getId(), $adminResourceIds, true)
167
            );
168
        }
169
170
        return $options;
171
    }
172
173
    /**
174
     * @param Option[] $options
175
     *
176
     * @return Select
177
     */
178
    protected function createAdminResourceSelect(array $options): Select
179
    {
180
        $size   = $this->getMultiSelectSize(
181
            count($options),
182
            static::MULTISELECT_MIN_SIZE,
183
            static::MULTISELECT_MAX_SIZE
184
        );
185
        $select = new MultiSelect(
186
            'admin_resource_ids',
187
            'admin_resource_ids[]',
188
            [],
189
            Attributes::fromArray([Html5::ATTR_SIZE => [(string)$size]])
190
        );
191
192
        foreach ($options as $option) {
193
            $select[] = $option;
194
        }
195
196
        return $select;
197
    }
198
199
    /**
200
     * @return Label
201
     */
202
    protected function createAdminResourceLabel(): Label
203
    {
204
        return new Label('admin_resource_ids', 'admin:adminResources');
205
    }
206
207
    /**
208
     * @param int $optionCount
209
     * @param int $minSize
210
     * @param int $maxSize
211
     *
212
     * @return int
213
     */
214
    protected function getMultiSelectSize(int $optionCount, int $minSize, int $maxSize): int
215
    {
216
        return (int)max(min($optionCount, $maxSize), $minSize);
217
    }
218
}
219