Passed
Push — master ( 9c7246...3b2dd4 )
by Peter
03:42
created

PageLayout::createAssets()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 4
nop 1
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Service\Execute;
6
7
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
8
use AbterPhp\Framework\Http\Service\Execute\RepoServiceAbstract;
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
    /**
24
     * PageLayout constructor.
25
     *
26
     * @param GridRepo         $repo
27
     * @param ValidatorFactory $validatorFactory
28
     * @param IUnitOfWork      $unitOfWork
29
     * @param IEventDispatcher $eventDispatcher
30
     * @param Slugify          $slugify
31
     */
32
    public function __construct(
33
        GridRepo $repo,
34
        ValidatorFactory $validatorFactory,
35
        IUnitOfWork $unitOfWork,
36
        IEventDispatcher $eventDispatcher,
37
        Slugify $slugify
38
    ) {
39
        parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher);
40
41
        $this->slugify = $slugify;
42
    }
43
44
45
    /**
46
     * @param string $entityId
47
     *
48
     * @return Entity
49
     */
50
    public function createEntity(string $entityId): IStringerEntity
51
    {
52
        return new Entity($entityId, '', '', new Assets('', '', '', [], []));
53
    }
54
55
    /**
56
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
57
     *
58
     * @param Entity         $entity
59
     * @param array          $postData
60
     * @param UploadedFile[] $fileData
61
     *
62
     * @return Entity
63
     */
64
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
65
    {
66
        $identifier = $this->slugify->slugify((string)$postData['identifier']);
67
68
        $assets = $this->createAssets($postData);
69
70
        $entity
71
            ->setIdentifier($identifier)
0 ignored issues
show
Bug introduced by
The method setIdentifier() does not exist on AbterPhp\Framework\Domain\Entities\IStringerEntity. It seems like you code against a sub-type of said class. However, the method does not exist in AbterPhp\Admin\Domain\Entities\Token or AbterPhp\Admin\Domain\Entities\LoginAttempt or AbterPhp\Admin\Domain\Entities\User or AbterPhp\Admin\Domain\Entities\ApiClient. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
            ->/** @scrutinizer ignore-call */ 
72
              setIdentifier($identifier)
Loading history...
72
            ->setBody((string)$postData['body'])
73
            ->setAssets($assets)
74
        ;
75
76
        return $entity;
77
    }
78
79
    /**
80
     * @param array $postData
81
     *
82
     * @return Assets
83
     */
84
    protected function createAssets(array $postData): Assets
85
    {
86
        if (is_string($postData['css-files'])) {
87
            $postData['css-files'] = explode('\r\n', $postData['css-files']);
88
        }
89
90
        if (is_string($postData['js-files'])) {
91
            $postData['js-files'] = explode('\r\n', $postData['js-files']);
92
        }
93
94
        return new Assets(
95
            $postData['identifier'],
96
            $postData['header'],
97
            $postData['footer'],
98
            $postData['css-files'],
99
            $postData['js-files']
100
        );
101
    }
102
}
103