PageCategory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 20
c 2
b 0
f 0
dl 0
loc 72
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A createEntity() 0 3 1
A fillEntity() 0 23 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Website\Service\Execute;
6
7
use AbterPhp\Admin\Domain\Entities\UserGroup;
8
use AbterPhp\Admin\Service\Execute\RepoServiceAbstract;
9
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
10
use AbterPhp\Website\Domain\Entities\PageCategory as Entity;
11
use AbterPhp\Website\Orm\PageCategoryRepo as GridRepo;
12
use AbterPhp\Website\Validation\Factory\PageCategory 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 PageCategory extends RepoServiceAbstract
19
{
20
    /** @var Slugify */
21
    protected $slugify;
22
23
    /** @var GridRepo */
24
    protected $repo;
25
26
    /**
27
     * PageCategory 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, '', '');
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
73
        $identifier = $postData['identifier'] ?? $entity->getIdentifier();
74
        $identifier = $identifier ?: $name;
75
        $identifier = $this->slugify->slugify($identifier);
76
77
        $userGroups = [];
78
        if (array_key_exists('user_group_ids', $postData)) {
79
            foreach ($postData['user_group_ids'] as $id) {
80
                $userGroups[] = new UserGroup((string)$id, '', '');
81
            }
82
        }
83
84
        $entity
85
            ->setName($name)
86
            ->setIdentifier($identifier)
87
            ->setUserGroups($userGroups);
88
89
        return $entity;
90
    }
91
}
92