Passed
Push — master ( 1d30f9...202808 )
by Peter
04:48
created

ApiClient::fillEntity()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 29
rs 9.3888
c 0
b 0
f 0
cc 5
nc 5
nop 3
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\ApiClient as Entity;
9
use AbterPhp\Admin\Orm\ApiClientRepo as GridRepo;
10
use AbterPhp\Admin\Validation\Factory\ApiClient as ValidatorFactory;
11
use AbterPhp\Framework\Crypto\Crypto;
12
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
13
use AbterPhp\Framework\Http\Service\Execute\RepoServiceAbstract;
14
use Opulence\Events\Dispatchers\IEventDispatcher;
15
use Opulence\Http\Requests\UploadedFile;
16
use Opulence\Orm\IUnitOfWork;
17
18
class ApiClient extends RepoServiceAbstract
19
{
20
    /** @var Crypto */
21
    protected $crypto;
22
23
    /**
24
     * ApiClient constructor.
25
     *
26
     * @param GridRepo         $repo
27
     * @param ValidatorFactory $validatorFactory
28
     * @param IUnitOfWork      $unitOfWork
29
     * @param IEventDispatcher $eventDispatcher
30
     * @param Crypto           $crypto
31
     */
32
    public function __construct(
33
        GridRepo $repo,
34
        ValidatorFactory $validatorFactory,
35
        IUnitOfWork $unitOfWork,
36
        IEventDispatcher $eventDispatcher,
37
        Crypto $crypto
38
    ) {
39
        parent::__construct(
40
            $repo,
41
            $validatorFactory,
42
            $unitOfWork,
43
            $eventDispatcher
44
        );
45
46
        $this->crypto = $crypto;
47
    }
48
49
    /**
50
     * @param string $entityId
51
     *
52
     * @return Entity
53
     */
54
    public function createEntity(string $entityId): IStringerEntity
55
    {
56
        $entity = new Entity($entityId, '', '', '');
57
58
        return $entity;
59
    }
60
61
    /**
62
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
63
     *
64
     * @param Entity         $entity
65
     * @param array          $postData
66
     * @param UploadedFile[] $fileData
67
     *
68
     * @return Entity
69
     */
70
    protected function fillEntity(IStringerEntity $entity, array $postData, array $fileData): IStringerEntity
71
    {
72
        if (!($entity instanceof Entity)) {
73
            return $entity;
74
        }
75
76
        $secret      = (string)$postData['secret'];
77
        $description = (string)$postData['description'];
78
        $userId      = (string)$postData['user_id'];
79
80
        $adminResources = [];
81
        if (array_key_exists('admin_resource_ids', $postData)) {
82
            foreach ($postData['admin_resource_ids'] as $id) {
83
                $adminResources[] = new AdminResource((string)$id, '');
84
            }
85
        }
86
87
        $entity
88
            ->setDescription($description)
89
            ->setUserId($userId)
90
            ->setAdminResources($adminResources);
91
92
        if ($secret) {
93
            $secret = $this->crypto->prepareSecret($secret);
94
95
            $entity->setSecret($this->crypto->hashCrypt($secret));
96
        }
97
98
        return $entity;
99
    }
100
}
101