PageLayout   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 31
c 4
b 0
f 0
dl 0
loc 93
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A createAssets() 0 13 3
A __construct() 0 10 1
A fillEntity() 0 23 2
A createEntity() 0 3 1
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\PageLayout as Entity;
10
use AbterPhp\Website\Domain\Entities\PageLayout\Assets;
11
use AbterPhp\Website\Orm\PageLayoutRepo as GridRepo;
12
use AbterPhp\Website\Validation\Factory\PageLayout as ValidatorFactory;
13
use Cocur\Slugify\Slugify;
14
use Opulence\Events\Dispatchers\IEventDispatcher;
15
use Opulence\Http\Requests\UploadedFile;
16
use Opulence\Orm\IUnitOfWork;
17
18
class PageLayout extends RepoServiceAbstract
19
{
20
    /** @var Slugify */
21
    protected $slugify;
22
23
    /** @var GridRepo */
24
    protected $repo;
25
26
    /**
27
     * PageLayout constructor.
28
     *
29
     * @param GridRepo         $repo
30
     * @param ValidatorFactory $validatorFactory
31
     * @param IUnitOfWork      $unitOfWork
32
     * @param IEventDispatcher $eventDispatcher
33
     * @param Slugify          $slugify
34
     */
35
    public function __construct(
36
        GridRepo $repo,
37
        ValidatorFactory $validatorFactory,
38
        IUnitOfWork $unitOfWork,
39
        IEventDispatcher $eventDispatcher,
40
        Slugify $slugify
41
    ) {
42
        parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher);
43
44
        $this->slugify = $slugify;
45
    }
46
47
48
    /**
49
     * @param string $entityId
50
     *
51
     * @return Entity
52
     */
53
    public function createEntity(string $entityId): IStringerEntity
54
    {
55
        return new Entity($entityId, '', '', '', '', new Assets('', '', '', [], []));
56
    }
57
58
    /**
59
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
60
     *
61
     * @param IStringerEntity $entity
62
     * @param array           $postData
63
     * @param UploadedFile[]  $fileData
64
     *
65
     * @return Entity
66
     */
67
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
68
    {
69
        assert($entity instanceof Entity, new \InvalidArgumentException());
70
71
        $name    = $postData['name'];
72
        $classes = $postData['classes'] ?? '';
73
74
        $identifier = $postData['identifier'] ?? $entity->getIdentifier();
75
        $identifier = $identifier ?: $name;
76
        $identifier = $this->slugify->slugify($identifier);
77
78
        $assets = $this->createAssets($postData, $identifier);
79
80
        $body = $postData['body'];
81
82
        $entity
83
            ->setName($name)
84
            ->setClasses($classes)
85
            ->setIdentifier($identifier)
86
            ->setBody($body)
87
            ->setAssets($assets);
88
89
        return $entity;
90
    }
91
92
    /**
93
     * @param array  $postData
94
     * @param string $identifier
95
     *
96
     * @return Assets
97
     */
98
    protected function createAssets(array $postData, string $identifier): Assets
99
    {
100
        $header   = $postData['header'] ?? '';
101
        $footer   = $postData['footer'] ?? '';
102
        $cssFiles = empty($postData['css-files']) ? [] : explode('\r\n', $postData['css-files']);
103
        $jsFiles  = empty($postData['js-files']) ? [] : explode('\r\n', $postData['js-files']);
104
105
        return new Assets(
106
            $identifier,
107
            $header,
108
            $footer,
109
            $cssFiles,
110
            $jsFiles
111
        );
112
    }
113
}
114