FileCategory::createEntity()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Files\Service\Execute;
6
7
use AbterPhp\Admin\Domain\Entities\UserGroup;
8
use AbterPhp\Admin\Service\Execute\RepoServiceAbstract;
9
use AbterPhp\Files\Domain\Entities\FileCategory as Entity;
10
use AbterPhp\Files\Orm\FileCategoryRepo as GridRepo;
11
use AbterPhp\Files\Validation\Factory\FileCategory as ValidatorFactory;
12
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
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 FileCategory extends RepoServiceAbstract
19
{
20
    /** @var Slugify */
21
    protected $slugify;
22
23
    /**
24
     * FileCategory 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
     * @param string $entityId
46
     *
47
     * @return Entity
48
     */
49
    public function createEntity(string $entityId): IStringerEntity
50
    {
51
        $entity = new Entity($entityId, '', '', false);
52
53
        return $entity;
54
    }
55
56
    /**
57
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
58
     *
59
     * @param IStringerEntity $entity
60
     * @param array           $postData
61
     * @param UploadedFile[]  $fileData
62
     *
63
     * @return Entity
64
     */
65
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
66
    {
67
        if (!($entity instanceof Entity)) {
68
            throw new \InvalidArgumentException('Invalid entity');
69
        }
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
        $isPublic = isset($postData['is_public']) ? (bool)$postData['is_public'] : false;
85
86
        $entity
87
            ->setName($name)
88
            ->setIdentifier($identifier)
89
            ->setUserGroups($userGroups)
90
            ->setIsPublic($isPublic);
91
92
        return $entity;
93
    }
94
}
95