Passed
Pull Request — master (#1)
by Peter
05:48 queued 02:44
created

Item   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 234
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 85
c 1
b 0
f 0
dl 0
loc 234
rs 10
wmc 27

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addImgHref() 0 13 4
A addBody() 0 9 2
A __construct() 0 5 1
A addName() 0 9 2
A addImgSrc() 0 13 3
A addBodyHref() 0 13 3
A addImgAlt() 0 13 3
A create() 0 21 3
A addNameHref() 0 19 3
A addIsDeleted() 0 15 1
A addId() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Form\Factory\ContentList;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Form\Container\CheckboxGroup;
9
use AbterPhp\Framework\Form\Container\FormGroup;
10
use AbterPhp\Framework\Form\Element\Input;
11
use AbterPhp\Framework\Form\Element\Textarea;
12
use AbterPhp\Framework\Form\Extra\Help;
13
use AbterPhp\Framework\Form\Label\Label;
14
use AbterPhp\Framework\Html\Component;
15
use AbterPhp\Framework\Html\INode;
16
use AbterPhp\Website\Domain\Entities\ContentListItem as Entity;
17
18
class Item
19
{
20
    /** @var int */
21
    protected static $existingCount = 1;
22
23
    /** @var bool */
24
    protected $protected;
25
26
    /** @var bool */
27
    protected $withImage;
28
29
    /** @var bool */
30
    protected $withLinks;
31
32
    /** @var string */
33
    protected $id;
34
35
    /** @var array */
36
    protected $hiddenAttribs = [Html5::ATTR_TYPE => Input::TYPE_HIDDEN];
37
38
    /**
39
     * Item constructor.
40
     *
41
     * @param bool $protected
42
     * @param bool $withImage
43
     * @param bool $withLinks
44
     */
45
    public function __construct(bool $protected, bool $withImage, bool $withLinks)
46
    {
47
        $this->protected = $protected;
48
        $this->withImage = $withImage;
49
        $this->withLinks = $withLinks;
50
    }
51
52
    /**
53
     * @param Entity|null $entity
54
     *
55
     * @return INode[]
56
     */
57
    public function create(?Entity $entity = null): array
58
    {
59
        $count    = static::$existingCount++;
60
        $this->id = $entity ? "existing{$count}" : 'new';
61
62
        $components = [];
63
64
        if ($entity) {
65
            $components[] = $this->addId($entity);
66
        }
67
68
        $components[] = $this->addName($entity);
69
        $components[] = $this->addNameHref($entity);
70
        $components[] = $this->addBody($entity);
71
        $components[] = $this->addBodyHref($entity);
72
        $components[] = $this->addImgSrc($entity);
73
        $components[] = $this->addImgAlt($entity);
74
        $components[] = $this->addImgHref($entity);
75
        $components[] = $this->addIsDeleted($entity);
76
77
        return $components;
78
    }
79
80
    /**
81
     * @param Entity $entity
82
     *
83
     * @return INode
84
     */
85
    protected function addId(Entity $entity): INode
86
    {
87
        $name = $entity ? $entity->getName() : '';
0 ignored issues
show
introduced by
$entity is of type AbterPhp\Website\Domain\Entities\ContentListItem, thus it always evaluated to true.
Loading history...
Unused Code introduced by
The assignment to $name is dead and can be removed.
Loading history...
88
89
        return new Input("item_id_{$this->id}", "{$this->id}[id]", $entity->getId(), [], $this->hiddenAttribs);
90
91
        return new FormGroup($input, $label, $help);
0 ignored issues
show
Unused Code introduced by
return new AbterPhp\Fram...($input, $label, $help) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
92
    }
93
94
    /**
95
     * @param Entity|null $entity
96
     *
97
     * @return INode
98
     */
99
    protected function addName(?Entity $entity): INode
100
    {
101
        $name = $entity ? $entity->getName() : '';
102
103
        $input = new Input("item_name_{$this->id}", "{$this->id}[name]", $name);
104
        $label = new Label("item_name_{$this->id}", 'website:contentListItemName');
105
        $help  = new Help('website:contentListItemNameHelp');
106
107
        return new FormGroup($input, $label, $help);
108
    }
109
110
    /**
111
     * @param Entity|null $entity
112
     *
113
     * @return INode
114
     */
115
    protected function addNameHref(?Entity $entity): INode
116
    {
117
        if (!$this->withLinks) {
118
            return new Input(
119
                "item_name_href_{$this->id}",
120
                "item_name_href[{$this->id}]",
121
                '',
122
                [],
123
                $this->hiddenAttribs
124
            );
125
        }
126
127
        $nameHref = $entity ? $entity->getNameHref() : '';
128
129
        $input = new Input("item_name_href_{$this->id}", "{$this->id}[name_href]", $nameHref);
130
        $label = new Label("item_name_href_{$this->id}", 'website:contentListItemNameHref');
131
        $help  = new Help('website:contentListItemNameHrefHelp');
132
133
        return new FormGroup($input, $label, $help);
134
    }
135
136
    /**
137
     * @param Entity|null $entity
138
     *
139
     * @return INode
140
     */
141
    protected function addBody(?Entity $entity): INode
142
    {
143
        $body = $entity ? $entity->getBody() : '';
144
145
        $input = new Textarea("item_body_{$this->id}", "{$this->id}[body]", $body);
146
        $label = new Label("item_body_{$this->id}", 'website:contentListItemBody');
147
        $help  = new Help('website:contentListItemBodyHelp');
148
149
        return new FormGroup($input, $label, $help);
150
    }
151
152
    /**
153
     * @param Entity|null $entity
154
     *
155
     * @return INode
156
     */
157
    protected function addBodyHref(?Entity $entity): INode
158
    {
159
        if (!$this->withLinks) {
160
            return new Input("item_body_href_{$this->id}", "{$this->id}[body_href]", '', [], $this->hiddenAttribs);
161
        }
162
163
        $bodyHref = $entity ? $entity->getBodyHref() : '';
164
165
        $input = new Input("item_body_href_{$this->id}", "{$this->id}[body_href]", $bodyHref);
166
        $label = new Label("item_body_href_{$this->id}", 'website:contentListItemBodyHref');
167
        $help  = new Help('website:contentListItemBodyHrefHelp');
168
169
        return new FormGroup($input, $label, $help);
170
    }
171
172
    /**
173
     * @param Entity|null $entity
174
     *
175
     * @return INode
176
     */
177
    protected function addImgSrc(?Entity $entity): INode
178
    {
179
        if (!$this->withImage) {
180
            return new Input("item_img_src_{$this->id}", "{$this->id}[img_src]", '', [], $this->hiddenAttribs);
181
        }
182
183
        $imgSrc = $entity ? $entity->getImgSrc() : '';
184
185
        $input = new Input("item_img_src_{$this->id}", "{$this->id}[img_src]", $imgSrc);
186
        $label = new Label("item_img_src_{$this->id}", 'website:contentListItemImgSrc');
187
        $help  = new Help('website:contentListItemImgSrcHelp');
188
189
        return new FormGroup($input, $label, $help);
190
    }
191
192
    /**
193
     * @param Entity|null $entity
194
     *
195
     * @return INode
196
     */
197
    protected function addImgAlt(?Entity $entity): INode
198
    {
199
        if (!$this->withImage) {
200
            return new Input("item_img_alt_{$this->id}", "{$this->id}[img_alt]", '', [], $this->hiddenAttribs);
201
        }
202
203
        $imgAlt = $entity ? $entity->getImgAlt() : '';
204
205
        $input = new Input("item_img_alt_{$this->id}", "{$this->id}[img_alt]", $imgAlt);
206
        $label = new Label("item_img_alt_{$this->id}", 'website:contentListItemImgAlt');
207
        $help  = new Help('website:contentListItemImgAltHelp');
208
209
        return new FormGroup($input, $label, $help);
210
    }
211
212
    /**
213
     * @param Entity|null $entity
214
     *
215
     * @return INode
216
     */
217
    protected function addImgHref(?Entity $entity): INode
218
    {
219
        if (!$this->withImage || !$this->withLinks) {
220
            return new Input("item_img_href_{$this->id}", "{$this->id}[img_href]", '', [], $this->hiddenAttribs);
221
        }
222
223
        $imgHref = $entity ? $entity->getImgHref() : '';
224
225
        $input = new Input("item_img_href_{$this->id}", "{$this->id}[img_href]", $imgHref);
226
        $label = new Label("item_img_href_{$this->id}", 'website:contentListItemImgHref');
227
        $help  = new Help('website:contentListItemImgHrefHelp');
228
229
        return new FormGroup($input, $label, $help);
230
    }
231
232
    /**
233
     * @param Entity|null $entity
234
     *
235
     * @return INode
236
     */
237
    protected function addIsDeleted(?Entity $entity): INode
0 ignored issues
show
Unused Code introduced by
The parameter $entity is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

237
    protected function addIsDeleted(/** @scrutinizer ignore-unused */ ?Entity $entity): INode

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
238
    {
239
        $attributes = [Html5::ATTR_TYPE => Input::TYPE_CHECKBOX, Html5::ATTR_WRAP => ['item_is_deleted']];
240
241
        $input = new Input(
242
            "item_is_deleted_{$this->id}",
243
            "{$this->id}[is_deleted]",
244
            '1',
245
            [],
246
            $attributes
247
        );
248
        $label = new Label("item_is_deleted_{$this->id}", 'website:contentListItemIsDeleted');
249
        $help  = new Component('website:contentListItemIsDeletedHelp');
250
251
        return new CheckboxGroup($input, $label, $help, [], [Html5::ATTR_CLASS => 'is-deleted-container']);
252
    }
253
}
254