Passed
Push — master ( 8bca10...4ac827 )
by Peter
02:35
created

ContentList::fillEntity()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 25
c 2
b 0
f 0
nc 2
nop 3
dl 0
loc 36
rs 9.52
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']);
99
        $withLinks = !empty($postData['with_links']);
100
        $withImage = !empty($postData['with_image']);
101
        $withBody  = !empty($postData['with_body']);
102
        $withHtml  = !empty($postData['with_html']);
103
104
        $items = $this->createItems($postData, $entity->getId());
105
106
        $entity
107
            ->setType($type)
108
            ->setIdentifier($identifier)
109
            ->setClasses($classes)
110
            ->setName($name)
111
            ->setProtected($protected)
112
            ->setWithImage($withImage)
113
            ->setWithLinks($withLinks)
114
            ->setWithBody($withBody)
115
            ->setWithHtml($withHtml)
116
            ->setItems($items);
117
118
        return $entity;
119
    }
120
121
    /**
122
     * @param Entity $entity
123
     * @param array  $postData
124
     *
125
     * @return array
126
     * @throws \Casbin\Exceptions\CasbinException
127
     */
128
    protected function protectPostData(Entity $entity, array $postData): array
129
    {
130
        if (!$entity->isProtected()) {
131
            return $postData;
132
        }
133
134
        $username        = $this->session->get(Session::USERNAME);
135
        $advancedAllowed = $this->enforcer->enforce(
136
            $username,
137
            Authorization::RESOURCE_LISTS,
138
            Authorization::ROLE_ADVANCED_WRITE
139
        );
140
141
        if ($advancedAllowed) {
142
            return $postData;
143
        }
144
145
        $postData['type_id']    = $entity->getType()->getId();
146
        $postData['identifier'] = $entity->getIdentifier();
147
        $postData['protected']  = $entity->isProtected();
148
        $postData['with_image'] = $entity->isWithImage();
149
        $postData['with_links'] = $entity->isWithLinks();
150
        $postData['with_body']  = $entity->isWithBody();
151
        $postData['with_html']  = $entity->isWithHtml();
152
153
        return $postData;
154
    }
155
156
    /**
157
     * @param array  $postData
158
     * @param string $listId
159
     *
160
     * @return Item[]
161
     */
162
    protected function createItems(array $postData, string $listId): array
163
    {
164
        $items = [];
165
166
        $i = 1;
167
        while (isset($postData["new$i"])) {
168
            $itemData = $postData["new$i"];
169
170
            $i++;
171
172
            if (!empty($itemData['is_deleted'])) {
173
                continue;
174
            }
175
176
            $items[] = $this->createItem($itemData, $listId);
177
        }
178
179
        $i = 1;
180
        while (isset($postData["existing$i"])) {
181
            $items[] = $this->createItem($postData["existing$i"], $listId);
182
183
            $i++;
184
        }
185
186
        return $items;
187
    }
188
189
    /**
190
     * @param array  $itemData
191
     * @param string $listId
192
     *
193
     * @return Item
194
     */
195
    protected function createItem(array $itemData, string $listId): Item
196
    {
197
        $itemId   = $itemData['id'] ?? '';
198
        $name     = $itemData['name'] ?? '';
199
        $nameHref = $itemData['name_href'] ?? '';
200
        $body     = $itemData['body'] ?? '';
201
        $bodyHref = $itemData['body_href'] ?? '';
202
        $imgSrc   = $itemData['img_src'] ?? '';
203
        $imgAlt   = $itemData['img_alt'] ?? '';
204
        $imgHref  = $itemData['img_href'] ?? '';
205
        $deleted  = !empty($itemData['is_deleted']);
206
207
        return new Item(
208
            $itemId,
209
            $listId,
210
            $name,
211
            $nameHref,
212
            $body,
213
            $bodyHref,
214
            $imgSrc,
215
            $imgAlt,
216
            $imgHref,
217
            $deleted
218
        );
219
    }
220
}
221