Passed
Push — master ( eee611...426661 )
by Peter
05:12
created

FileCategory::addIsPublic()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 22
rs 9.7998
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Form\Factory;
6
7
use AbterPhp\Admin\Domain\Entities\UserGroup;
8
use AbterPhp\Admin\Form\Factory\Base;
9
use AbterPhp\Admin\Orm\UserGroupRepo;
10
use AbterPhp\Files\Domain\Entities\FileCategory as Entity;
11
use AbterPhp\Framework\Constant\Html5;
12
use AbterPhp\Framework\Form\Component\Option;
13
use AbterPhp\Framework\Form\Container\CheckboxGroup;
14
use AbterPhp\Framework\Form\Container\FormGroup;
15
use AbterPhp\Framework\Form\Element\Input;
16
use AbterPhp\Framework\Form\Element\MultiSelect;
17
use AbterPhp\Framework\Form\Element\Select;
18
use AbterPhp\Framework\Form\IForm;
19
use AbterPhp\Framework\Form\Label\Label;
20
use AbterPhp\Framework\Html\Node;
21
use AbterPhp\Framework\I18n\ITranslator;
22
use Opulence\Orm\IEntity;
23
use Opulence\Sessions\ISession;
24
25
class FileCategory extends Base
26
{
27
    /** @var UserGroupRepo */
28
    protected $userGroupRepo;
29
30
    /**
31
     * FileCategory constructor.
32
     *
33
     * @param ISession      $session
34
     * @param ITranslator   $translator
35
     * @param UserGroupRepo $userGroupRepo
36
     */
37
    public function __construct(ISession $session, ITranslator $translator, UserGroupRepo $userGroupRepo)
38
    {
39
        parent::__construct($session, $translator);
40
41
        $this->userGroupRepo = $userGroupRepo;
42
    }
43
44
    /**
45
     * @param string       $action
46
     * @param string       $method
47
     * @param string       $showUrl
48
     * @param IEntity|null $entity
49
     *
50
     * @return IForm
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
            ->addIsPublic($entity)
61
            ->addUserGroups($entity)
62
            ->addDefaultButtons($showUrl);
63
64
        $form = $this->form;
65
66
        $this->form = null;
67
68
        return $form;
69
    }
70
71
    /**
72
     * @param Entity $entity
73
     *
74
     * @return $this
75
     */
76
    protected function addName(Entity $entity): FileCategory
77
    {
78
        $input = new Input('name', 'name', $entity->getName());
79
        $label = new Label('name', 'files:fileCategoryName');
80
81
        $this->form[] = new FormGroup($input, $label);
82
83
        return $this;
84
    }
85
86
    /**
87
     * @param Entity $entity
88
     *
89
     * @return $this
90
     */
91
    protected function addIdentifier(Entity $entity): FileCategory
92
    {
93
        $input = new Input('identifier', 'identifier', $entity->getIdentifier());
94
        $label = new Label('body', 'files:fileCategoryIdentifier');
95
96
        $this->form[] = new FormGroup($input, $label);
97
98
        return $this;
99
    }
100
101
    /**
102
     * @param Entity $entity
103
     *
104
     * @return $this
105
     */
106
    protected function addIsPublic(Entity $entity): FileCategory
107
    {
108
        $attributes = [Html5::ATTR_TYPE => [Input::TYPE_CHECKBOX]];
109
        if ($entity->isPublic()) {
110
            $attributes[Html5::ATTR_CHECKED] = null;
111
        }
112
        $input = new Input(
113
            'is_public',
114
            'is_public',
115
            '1',
116
            [],
117
            $attributes
118
        );
119
        $label = new Label(
120
            'is_public',
121
            'files:fileCategoryIsPublic'
122
        );
123
        $help  = new Node('files:fileCategoryIsPublic');
124
125
        $this->form[] = new CheckboxGroup($input, $label, $help);
126
127
        return $this;
128
    }
129
130
    /**
131
     * @param Entity $entity
132
     *
133
     * @return $this
134
     */
135
    protected function addUserGroups(Entity $entity): FileCategory
136
    {
137
        $allUserGroups = $this->userGroupRepo->getAll();
138
        $userGroupIds  = $this->getUserGroupIds($entity);
139
140
        $options = $this->createUserGroupOptions($allUserGroups, $userGroupIds);
141
142
        $this->form[] = new FormGroup(
143
            $this->createUserGroupSelect($options),
144
            $this->createUserGroupLabel()
145
        );
146
147
        return $this;
148
    }
149
150
    /**
151
     * @param Entity $entity
152
     *
153
     * @return int[]
154
     */
155
    protected function getUserGroupIds(Entity $entity): array
156
    {
157
        $userGroupIds = [];
158
        foreach ($entity->getUserGroups() as $userGroup) {
159
            $userGroupIds[] = $userGroup->getId();
160
        }
161
162
        return $userGroupIds;
163
    }
164
165
    /**
166
     * @param UserGroup[] $allUserGroups
167
     * @param int[]       $userGroupIds
168
     *
169
     * @return array
170
     */
171
    protected function createUserGroupOptions(array $allUserGroups, array $userGroupIds): array
172
    {
173
        $options = [];
174
        foreach ($allUserGroups as $userGroup) {
175
            $isSelected = in_array($userGroup->getId(), $userGroupIds, true);
176
            $options[]  = new Option((string)$userGroup->getId(), $userGroup->getName(), $isSelected);
177
        }
178
179
        return $options;
180
    }
181
182
    /**
183
     * @param Option[] $options
184
     *
185
     * @return Select
186
     */
187
    protected function createUserGroupSelect(array $options): Select
188
    {
189
        $attributes = [
190
            Html5::ATTR_SIZE => $this->getMultiSelectSize(
191
                count($options),
192
                static::MULTISELECT_MIN_SIZE,
193
                static::MULTISELECT_MAX_SIZE
194
            ),
195
        ];
196
197
        $select = new MultiSelect('user_group_ids', 'user_group_ids[]', [], $attributes);
198
199
        foreach ($options as $option) {
200
            $select[] = $option;
201
        }
202
203
        return $select;
204
    }
205
206
    /**
207
     * @return Label
208
     */
209
    protected function createUserGroupLabel(): Label
210
    {
211
        return new Label('user_group_ids', 'files:fileCategoryUserGroups');
212
    }
213
214
    /**
215
     * @param int $optionCount
216
     * @param int $minSize
217
     * @param int $maxSize
218
     *
219
     * @return int
220
     */
221
    protected function getMultiSelectSize(int $optionCount, int $minSize, int $maxSize): int
222
    {
223
        return (int)max(min($optionCount, $maxSize), $minSize);
224
    }
225
}
226