File::getMultiSelectSize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Form\Factory;
6
7
use AbterPhp\Admin\Form\Factory\Base;
8
use AbterPhp\Files\Domain\Entities\File as Entity;
9
use AbterPhp\Files\Domain\Entities\FileCategory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, AbterPhp\Files\Form\Factory\FileCategory. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
use AbterPhp\Files\Orm\FileCategoryRepo;
11
use AbterPhp\Framework\Constant\Html5;
12
use AbterPhp\Framework\Form\Component\Option;
13
use AbterPhp\Framework\Form\Container\FormGroup;
14
use AbterPhp\Framework\Form\Element\Input;
15
use AbterPhp\Framework\Form\Element\Select;
16
use AbterPhp\Framework\Form\Element\Textarea;
17
use AbterPhp\Framework\Form\IForm;
18
use AbterPhp\Framework\Form\Label\Label;
19
use AbterPhp\Framework\I18n\ITranslator;
20
use Opulence\Orm\IEntity;
21
use Opulence\Sessions\ISession;
22
23
class File extends Base
24
{
25
    /** @var FileCategoryRepo */
26
    protected $fileCategoryRepo;
27
28
    /**
29
     * File constructor.
30
     *
31
     * @param ISession         $session
32
     * @param ITranslator      $translator
33
     * @param FileCategoryRepo $fileCategoryRepo
34
     */
35
    public function __construct(ISession $session, ITranslator $translator, FileCategoryRepo $fileCategoryRepo)
36
    {
37
        parent::__construct($session, $translator);
38
39
        $this->fileCategoryRepo = $fileCategoryRepo;
40
    }
41
42
    /**
43
     * @param string       $action
44
     * @param string       $method
45
     * @param string       $showUrl
46
     * @param IEntity|null $entity
47
     *
48
     * @return IForm
49
     */
50
    public function create(string $action, string $method, string $showUrl, ?IEntity $entity = null): IForm
51
    {
52
        assert($entity instanceof Entity, new \InvalidArgumentException());
53
54
        $this->createForm($action, $method, true)
55
            ->addDefaultElements()
56
            ->addFile()
57
            ->addDescription($entity)
58
            ->addFileCategory($entity)
59
            ->addDefaultButtons($showUrl);
60
61
        $form = $this->form;
62
63
        $this->form = null;
64
65
        return $form;
66
    }
67
68
    /**
69
     * @return $this
70
     */
71
    protected function addFile(): File
72
    {
73
        $input = new Input('file', 'file', '', [], [Html5::ATTR_TYPE => Input::TYPE_FILE]);
74
        $label = new Label('file', 'files:file');
75
76
        $this->form[] = new FormGroup($input, $label);
77
78
        return $this;
79
    }
80
81
    /**
82
     * @return $this
83
     */
84
    protected function addDescription(Entity $entity): File
85
    {
86
        $input = new Textarea('description', 'description', $entity->getDescription());
87
        $label = new Label('description', 'files:fileDescription');
88
89
        $this->form[] = new FormGroup($input, $label);
90
91
        return $this;
92
    }
93
94
    /**
95
     * @param Entity $entity
96
     *
97
     * @return File
98
     * @throws \Opulence\Orm\OrmException
99
     */
100
    protected function addFileCategory(Entity $entity): File
101
    {
102
        $allFileCategories = $this->fileCategoryRepo->getAll();
103
        $fileCategoryId    = $entity->getCategory()->getId();
104
105
        $options = $this->createFileCategoryOptions($allFileCategories, $fileCategoryId);
106
107
        $this->form[] = new FormGroup(
108
            $this->createFileCategorySelect($options),
109
            $this->createFileCategoryLabel()
110
        );
111
112
        return $this;
113
    }
114
115
    /**
116
     * @param FileCategory[] $allFileCategories
117
     * @param string         $fileCategoryId
118
     *
119
     * @return array
120
     */
121
    protected function createFileCategoryOptions(array $allFileCategories, string $fileCategoryId): array
122
    {
123
        $options = [];
124
        foreach ($allFileCategories as $fileCategory) {
125
            $isSelected = $fileCategory->getId() === $fileCategoryId;
126
            $options[]  = new Option($fileCategory->getId(), $fileCategory->getName(), $isSelected);
127
        }
128
129
        return $options;
130
    }
131
132
    /**
133
     * @param Option[] $options
134
     *
135
     * @return Select
136
     */
137
    protected function createFileCategorySelect(array $options): Select
138
    {
139
        $attributes = [
140
            Html5::ATTR_SIZE => $this->getMultiSelectSize(
141
                count($options),
142
                static::MULTISELECT_MIN_SIZE,
143
                static::MULTISELECT_MAX_SIZE
144
            ),
145
        ];
146
147
        $select = new Select('category_id', 'category_id', [], $attributes);
148
149
        foreach ($options as $option) {
150
            $select[] = $option;
151
        }
152
153
        return $select;
154
    }
155
156
    /**
157
     * @return Label
158
     */
159
    protected function createFileCategoryLabel(): Label
160
    {
161
        return new Label('file_category_id', 'files:fileCategory');
162
    }
163
164
    /**
165
     * @param int $optionCount
166
     * @param int $minSize
167
     * @param int $maxSize
168
     *
169
     * @return int
170
     */
171
    protected function getMultiSelectSize(int $optionCount, int $minSize, int $maxSize): int
172
    {
173
        return (int)max(min($optionCount, $maxSize), $minSize);
174
    }
175
}
176