BlockLayout::createEntity()   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 1
dl 0
loc 3
rs 10
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\Domain\Entities\IStringerEntity;
9
use AbterPhp\Website\Domain\Entities\BlockLayout as Entity;
10
use AbterPhp\Website\Orm\BlockLayoutRepo as GridRepo;
11
use AbterPhp\Website\Validation\Factory\BlockLayout as ValidatorFactory;
12
use Cocur\Slugify\Slugify;
13
use Opulence\Events\Dispatchers\IEventDispatcher;
14
use Opulence\Http\Requests\UploadedFile;
15
use Opulence\Orm\IUnitOfWork;
16
17
class BlockLayout extends RepoServiceAbstract
18
{
19
    /** @var Slugify */
20
    protected $slugify;
21
22
    /** @var GridRepo */
23
    protected $repo;
24
25
    /**
26
     * BlockLayout constructor.
27
     *
28
     * @param GridRepo         $repo
29
     * @param ValidatorFactory $validatorFactory
30
     * @param IUnitOfWork      $unitOfWork
31
     * @param IEventDispatcher $eventDispatcher
32
     * @param Slugify          $slugify
33
     */
34
    public function __construct(
35
        GridRepo $repo,
36
        ValidatorFactory $validatorFactory,
37
        IUnitOfWork $unitOfWork,
38
        IEventDispatcher $eventDispatcher,
39
        Slugify $slugify
40
    ) {
41
        parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher);
42
43
        $this->slugify = $slugify;
44
    }
45
46
47
    /**
48
     * @param string $entityId
49
     *
50
     * @return Entity
51
     */
52
    public function createEntity(string $entityId): IStringerEntity
53
    {
54
        return new Entity($entityId, '', '', '');
55
    }
56
57
    /**
58
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
59
     *
60
     * @param IStringerEntity $entity
61
     * @param array           $postData
62
     * @param UploadedFile[]  $fileData
63
     *
64
     * @return Entity
65
     */
66
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
67
    {
68
        assert($entity instanceof Entity, new \InvalidArgumentException());
69
70
        $name = $postData['name'];
71
72
        $identifier = $postData['identifier'] ?? $entity->getIdentifier();
73
        $identifier = $identifier ?: $name;
74
        $identifier = $this->slugify->slugify($identifier);
75
76
        $body = $postData['body'];
77
78
        $entity
79
            ->setName($name)
80
            ->setIdentifier($identifier)
81
            ->setBody($body);
82
83
        return $entity;
84
    }
85
}
86