Passed
Push — master ( 7ba2cb...1d30f9 )
by Peter
02:20
created

UserApiKey   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 18
dl 0
loc 61
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createEntity() 0 5 1
A __construct() 0 11 1
A fillEntity() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Service\Execute;
6
7
use AbterPhp\Admin\Domain\Entities\AdminResource;
8
use AbterPhp\Admin\Domain\Entities\UserApiKey as Entity;
9
use AbterPhp\Admin\Orm\UserApiKeyRepo as GridRepo;
10
use AbterPhp\Admin\Validation\Factory\UserApiKey as ValidatorFactory;
11
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
12
use AbterPhp\Framework\Http\Service\Execute\RepoServiceAbstract;
13
use Opulence\Events\Dispatchers\IEventDispatcher;
14
use Opulence\Orm\IUnitOfWork;
15
16
class UserApiKey extends RepoServiceAbstract
17
{
18
    /**
19
     * UserApiKey constructor.
20
     *
21
     * @param GridRepo         $repo
22
     * @param ValidatorFactory $validatorFactory
23
     * @param IUnitOfWork      $unitOfWork
24
     * @param IEventDispatcher $eventDispatcher
25
     */
26
    public function __construct(
27
        GridRepo $repo,
28
        ValidatorFactory $validatorFactory,
29
        IUnitOfWork $unitOfWork,
30
        IEventDispatcher $eventDispatcher
31
    ) {
32
        parent::__construct(
33
            $repo,
34
            $validatorFactory,
35
            $unitOfWork,
36
            $eventDispatcher
37
        );
38
    }
39
40
    /**
41
     * @param string $entityId
42
     *
43
     * @return Entity
44
     */
45
    public function createEntity(string $entityId): IStringerEntity
46
    {
47
        $entity = new Entity($entityId, '', '');
48
49
        return $entity;
50
    }
51
52
    /**
53
     * @param Entity $entity
54
     * @param array  $data
55
     *
56
     * @return Entity
57
     */
58
    protected function fillEntity(IStringerEntity $entity, array $data): IStringerEntity
59
    {
60
        if (!($entity instanceof Entity)) {
61
            return $entity;
62
        }
63
64
        $description = (string)$data['description'];
65
        $userId      = (string)$data['user_id'];
66
67
        $adminResources = [];
68
        if (array_key_exists('admin_resource_ids', $data)) {
69
            foreach ($data['admin_resource_ids'] as $id) {
70
                $adminResources[] = new AdminResource((string)$id, '');
71
            }
72
        }
73
74
        $entity->setDescription($description)->setUserId($userId)->setAdminResources($adminResources);
75
76
        return $entity;
77
    }
78
}
79