Passed
Push — master ( a11e3f...8bca10 )
by Peter
02:52
created

ContentList::fillEntity()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 34
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 6
eloc 23
c 3
b 1
f 0
nc 32
nop 3
dl 0
loc 34
rs 8.9297
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Service\Execute;
6
7
use AbterPhp\Admin\Service\Execute\RepoServiceAbstract;
8
use AbterPhp\Framework\Constant\Session;
9
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
10
use AbterPhp\Website\Constant\Authorization;
11
use AbterPhp\Website\Domain\Entities\ContentList as Entity;
12
use AbterPhp\Website\Domain\Entities\ContentListItem as Item;
13
use AbterPhp\Website\Domain\Entities\ContentListType as Type;
14
use AbterPhp\Website\Orm\ContentListRepo as GridRepo;
15
use AbterPhp\Website\Validation\Factory\ContentList as ValidatorFactory;
16
use Casbin\Enforcer;
17
use Cocur\Slugify\Slugify;
18
use Opulence\Events\Dispatchers\IEventDispatcher;
19
use Opulence\Http\Requests\UploadedFile;
20
use Opulence\Orm\IUnitOfWork;
21
use Opulence\Sessions\ISession;
22
23
class ContentList extends RepoServiceAbstract
24
{
25
    /** @var Slugify */
26
    protected $slugify;
27
28
    /** @var GridRepo */
29
    protected $repo;
30
31
    /** @var ISession */
32
    protected $session;
33
34
    /** @var Enforcer */
35
    protected $enforcer;
36
37
    /**
38
     * ContentList constructor.
39
     *
40
     * @param GridRepo         $repo
41
     * @param ValidatorFactory $validatorFactory
42
     * @param IUnitOfWork      $unitOfWork
43
     * @param IEventDispatcher $eventDispatcher
44
     * @param Slugify          $slugify
45
     * @param ISession         $session
46
     * @param Enforcer         $enforcer
47
     */
48
    public function __construct(
49
        GridRepo $repo,
50
        ValidatorFactory $validatorFactory,
51
        IUnitOfWork $unitOfWork,
52
        IEventDispatcher $eventDispatcher,
53
        Slugify $slugify,
54
        ISession $session,
55
        Enforcer $enforcer
56
    ) {
57
        parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher);
58
59
        $this->slugify  = $slugify;
60
        $this->session  = $session;
61
        $this->enforcer = $enforcer;
62
    }
63
64
    /**
65
     * @param string $entityId
66
     *
67
     * @return Entity
68
     */
69
    public function createEntity(string $entityId): IStringerEntity
70
    {
71
        return new Entity($entityId, '', '', '', false, false, false, false, false);
72
    }
73
74
    /**
75
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
76
     *
77
     * @param IStringerEntity $entity
78
     * @param array           $postData
79
     * @param UploadedFile[]  $fileData
80
     *
81
     * @return Entity
82
     */
83
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
84
    {
85
        assert($entity instanceof Entity, new \InvalidArgumentException());
86
87
        $postData = $this->protectPostData($entity, $postData);
88
89
        $type = new Type((string)$postData['type_id'], '', '');
90
91
        $name = (string)$postData['name'];
92
93
        $identifier = empty($postData['identifier']) ? $name : (string)$postData['identifier'];
94
        $identifier = $this->slugify->slugify($identifier);
95
96
        $classes = $postData['classes'];
97
98
        $protected = empty($postData['protected']) ? false : (bool)$postData['protected'];
99
        $withImage = empty($postData['with_image']) ? false : (bool)$postData['with_image'];
100
        $withLinks = empty($postData['with_links']) ? false : (bool)$postData['with_links'];
101
        $withHtml  = empty($postData['with_html']) ? false : (bool)$postData['with_html'];
102
103
        $items = $this->createItems($postData, $entity->getId());
104
105
        $entity
106
            ->setType($type)
107
            ->setIdentifier($identifier)
108
            ->setClasses($classes)
109
            ->setName($name)
110
            ->setProtected($protected)
111
            ->setWithImage($withImage)
112
            ->setWithLinks($withLinks)
113
            ->setWithHtml($withHtml)
114
            ->setItems($items);
115
116
        return $entity;
117
    }
118
119
    /**
120
     * @param Entity $entity
121
     * @param array  $postData
122
     *
123
     * @return array
124
     * @throws \Casbin\Exceptions\CasbinException
125
     */
126
    protected function protectPostData(Entity $entity, array $postData): array
127
    {
128
        if (!$entity->isProtected()) {
129
            return $postData;
130
        }
131
132
        $username        = $this->session->get(Session::USERNAME);
133
        $advancedAllowed = $this->enforcer->enforce(
134
            $username,
135
            Authorization::RESOURCE_LISTS,
136
            Authorization::ROLE_ADVANCED_WRITE
137
        );
138
139
        if ($advancedAllowed) {
140
            return $postData;
141
        }
142
143
        $postData['type_id']    = $entity->getType()->getId();
144
        $postData['identifier'] = $entity->getIdentifier();
145
        $postData['protected']  = $entity->isProtected();
146
        $postData['with_image'] = $entity->isWithImage();
147
        $postData['with_links'] = $entity->isWithLinks();
148
        $postData['with_html']  = $entity->isWithHtml();
149
150
        return $postData;
151
    }
152
153
    /**
154
     * @param array  $postData
155
     * @param string $listId
156
     *
157
     * @return Item[]
158
     */
159
    protected function createItems(array $postData, string $listId): array
160
    {
161
        $items = [];
162
163
        $i = 1;
164
        while (isset($postData["new$i"])) {
165
            $itemData = $postData["new$i"];
166
167
            $i++;
168
169
            if (!empty($itemData['is_deleted'])) {
170
                continue;
171
            }
172
173
            $items[] = $this->createItem($itemData, $listId);
174
        }
175
176
        $i = 1;
177
        while (isset($postData["existing$i"])) {
178
            $items[] = $this->createItem($postData["existing$i"], $listId);
179
180
            $i++;
181
        }
182
183
        return $items;
184
    }
185
186
    /**
187
     * @param array  $itemData
188
     * @param string $listId
189
     *
190
     * @return Item
191
     */
192
    protected function createItem(array $itemData, string $listId): Item
193
    {
194
        $itemId   = $itemData['id'] ?? '';
195
        $name     = $itemData['name'] ?? '';
196
        $nameHref = $itemData['name_href'] ?? '';
197
        $body     = $itemData['body'] ?? '';
198
        $bodyHref = $itemData['body_href'] ?? '';
199
        $imgSrc   = $itemData['img_src'] ?? '';
200
        $imgAlt   = $itemData['img_alt'] ?? '';
201
        $imgHref  = $itemData['img_href'] ?? '';
202
        $deleted  = !empty($itemData['is_deleted']);
203
204
        return new Item(
205
            $itemId,
206
            $listId,
207
            $name,
208
            $nameHref,
209
            $body,
210
            $bodyHref,
211
            $imgSrc,
212
            $imgAlt,
213
            $imgHref,
214
            $deleted
215
        );
216
    }
217
}
218