UserLanguage::fillEntity()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 2
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Service\Execute;
6
7
use AbterPhp\Admin\Domain\Entities\UserLanguage as Entity;
8
use AbterPhp\Admin\Orm\UserLanguageRepo as GridRepo;
9
use AbterPhp\Admin\Validation\Factory\UserLanguage as ValidatorFactory;
10
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
11
use Cocur\Slugify\Slugify;
12
use Opulence\Events\Dispatchers\IEventDispatcher;
13
use Opulence\Http\Requests\UploadedFile;
14
use Opulence\Orm\IUnitOfWork;
15
16
class UserLanguage extends RepoServiceAbstract
17
{
18
    protected Slugify $slugify;
19
20
    /**
21
     * UserLanguage constructor.
22
     *
23
     * @param GridRepo         $repo
24
     * @param ValidatorFactory $validatorFactory
25
     * @param IUnitOfWork      $unitOfWork
26
     * @param IEventDispatcher $eventDispatcher
27
     * @param Slugify          $slugify
28
     */
29
    public function __construct(
30
        GridRepo $repo,
31
        ValidatorFactory $validatorFactory,
32
        IUnitOfWork $unitOfWork,
33
        IEventDispatcher $eventDispatcher,
34
        Slugify $slugify
35
    ) {
36
        parent::__construct($repo, $validatorFactory, $unitOfWork, $eventDispatcher);
37
38
        $this->slugify = $slugify;
39
    }
40
41
    /**
42
     * @param string $entityId
43
     *
44
     * @return Entity
45
     */
46
    public function createEntity(string $entityId): IStringerEntity
47
    {
48
        return new Entity($entityId, '', '');
49
    }
50
51
    /**
52
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
53
     *
54
     * @param IStringerEntity $entity
55
     * @param array           $postData
56
     * @param UploadedFile[]  $fileData
57
     *
58
     * @return Entity
59
     */
60
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
61
    {
62
        assert($entity instanceof Entity, new \InvalidArgumentException('Invalid entity'));
63
64
        $name = $postData['name'];
65
66
        $identifier = $postData['identifier'] ?? $entity->getIdentifier();
67
        $identifier = $identifier ?: $entity->getName();
68
        $identifier = $this->slugify->slugify($identifier);
69
70
        $entity
71
            ->setName($name)
72
            ->setIdentifier($identifier);
73
74
        return $entity;
75
    }
76
}
77