Passed
Push — master ( 956624...05ed8c )
by Bertrand
08:49
created

CreateCharacteristic::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
rs 10
1
<?php
2
3
4
namespace App\Src\UseCases\Domain\Context\UseCases;
5
6
7
use App\Src\UseCases\Domain\Context\Model\Characteristic;
8
use App\Src\UseCases\Domain\Ports\CharacteristicsRepository;
9
use App\Src\UseCases\Domain\Ports\ContextRepository;
10
use App\Src\UseCases\Domain\Shared\Gateway\AuthGateway;
11
12
class CreateCharacteristic
13
{
14
    private $characteristicRepository;
15
    private $authGateway;
16
    private $contextRepository;
17
18
    public function __construct(
19
        CharacteristicsRepository $characteristicRepository,
20
        AuthGateway $authGateway,
21
        ContextRepository $contextRepository
22
    )
23
    {
24
        $this->characteristicRepository = $characteristicRepository;
25
        $this->authGateway = $authGateway;
26
        $this->contextRepository = $contextRepository;
27
    }
28
29
    public function execute(string $id, string $type, string $title)
30
    {
31
        $characteristic = $this->characteristicRepository->getBy(['title' => $title, 'type' => $type]);
32
        if(!isset($characteristic)){
33
            $characteristic = new Characteristic($id, $type, $title, false);
34
            $characteristic->create();
35
        }
36
37
        $user = $this->authGateway->current();
38
        $context = $this->contextRepository->getByUser($user->id());
39
        $context->addCharacteristics([$characteristic->id()], $user->id());
40
    }
41
}
42