Passed
Push — master ( 4ede65...0180bc )
by Peter
02:42
created

Block::protectPostData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 17
rs 9.9332
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\Block as Entity;
12
use AbterPhp\Website\Orm\BlockRepo as GridRepo;
13
use AbterPhp\Website\Validation\Factory\Block as ValidatorFactory;
14
use Casbin\Enforcer;
15
use Cocur\Slugify\Slugify;
16
use Opulence\Events\Dispatchers\IEventDispatcher;
17
use Opulence\Http\Requests\UploadedFile;
18
use Opulence\Orm\IUnitOfWork;
19
use Opulence\Sessions\ISession;
20
21
class Block extends RepoServiceAbstract
22
{
23
    /** @var Slugify */
24
    protected $slugify;
25
26
    /** @var GridRepo */
27
    protected $repo;
28
29
    /** @var ISession */
30
    protected $session;
31
32
    /** @var Enforcer */
33
    protected $enforcer;
34
35
    /**
36
     * Block constructor.
37
     *
38
     * @param GridRepo         $repo
39
     * @param ValidatorFactory $validatorFactory
40
     * @param IUnitOfWork      $unitOfWork
41
     * @param IEventDispatcher $eventDispatcher
42
     * @param Slugify          $slugify
43
     * @param ISession         $session
44
     * @param Enforcer         $enforcer
45
     */
46
    public function __construct(
47
        GridRepo $repo,
48
        ValidatorFactory $validatorFactory,
49
        IUnitOfWork $unitOfWork,
50
        IEventDispatcher $eventDispatcher,
51
        Slugify $slugify,
52
        ISession $session,
53
        Enforcer $enforcer
54
    ) {
55
        parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher);
56
57
        $this->slugify  = $slugify;
58
        $this->session  = $session;
59
        $this->enforcer = $enforcer;
60
    }
61
62
    /**
63
     * @param string $entityId
64
     *
65
     * @return Entity
66
     */
67
    public function createEntity(string $entityId): IStringerEntity
68
    {
69
        return new Entity($entityId, '', '', '', '', null);
70
    }
71
72
    /**
73
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
74
     *
75
     * @param IStringerEntity $entity
76
     * @param array           $postData
77
     * @param UploadedFile[]  $fileData
78
     *
79
     * @return Entity
80
     */
81
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
82
    {
83
        assert($entity instanceof Entity, new \InvalidArgumentException());
84
85
        $postData = $this->protectPostData($entity, $postData);
86
87
        $title = $postData['title'];
88
89
        $identifier = $postData['identifier'] ?? $entity->getIdentifier();
90
        $identifier = $identifier ?: $title;
91
        $identifier = $this->slugify->slugify($identifier);
92
93
        $body = $postData['body'];
94
95
        $layoutId = $postData['layout_id'];
96
        $layout   = '';
97
        if (empty($layoutId)) {
98
            $layoutId = null;
99
            $layout   = $postData['layout'] ?? '';
100
        }
101
102
        $entity
103
            ->setIdentifier($identifier)
104
            ->setTitle($title)
105
            ->setBody($body)
106
            ->setLayoutId($layoutId)
107
            ->setLayout($layout);
108
109
        return $entity;
110
    }
111
112
    /**
113
     * @param Entity $entity
114
     * @param array  $postData
115
     *
116
     * @return array
117
     * @throws \Casbin\Exceptions\CasbinException
118
     */
119
    protected function protectPostData(Entity $entity, array $postData): array
120
    {
121
        $username        = $this->session->get(Session::USERNAME);
122
        $advancedAllowed = $this->enforcer->enforce(
123
            $username,
124
            Authorization::RESOURCE_BLOCKS,
125
            Authorization::ROLE_ADVANCED_WRITE
126
        );
127
128
        if ($advancedAllowed) {
129
            return $postData;
130
        }
131
132
        $postData['layout_id'] = $postData['layout_id'] ?? $entity->getLayoutId();
133
        $postData['layout']    = $entity->getLayout();
134
135
        return $postData;
136
    }
137
}
138