AddCharacteristicsToContext::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 15
rs 9.9332
1
<?php
2
3
4
namespace App\Src\UseCases\Domain\Context\UseCases;
5
6
7
use App\Src\UseCases\Domain\Ports\CharacteristicsRepository;
8
use App\Src\UseCases\Domain\Ports\ContextRepository;
9
use App\Src\UseCases\Domain\Ports\PageRepository;
10
use App\Src\UseCases\Domain\Shared\Gateway\AuthGateway;
11
12
class AddCharacteristicsToContext
13
{
14
    private $authGateway;
15
    private $contextRepository;
16
    private $pageRepository;
17
    private $characteristicsRepository;
18
19
    public function __construct(
20
        AuthGateway $authGateway,
21
        ContextRepository $contextRepository,
22
        PageRepository $pageRepository,
23
        CharacteristicsRepository $characteristicsRepository
24
    )
25
    {
26
        $this->authGateway = $authGateway;
27
        $this->contextRepository = $contextRepository;
28
        $this->pageRepository = $pageRepository;
29
        $this->characteristicsRepository = $characteristicsRepository;
30
    }
31
32
    public function execute(array $pagesIds)
33
    {
34
        $user = $this->authGateway->current();
35
        $context = $this->contextRepository->getByUser($user->id());
36
        $pages = $this->pageRepository->getByIds($pagesIds);
37
38
        $characteristics = [];
39
        foreach ($pages as $page){
40
            $characteristic = $this->characteristicsRepository->getByPageId($page->pageId());
41
            if(!isset($characteristic)){
42
                $characteristic = $page->createCharacteristicAssociated();
43
            }
44
            $characteristics[] = $characteristic->id();
45
        }
46
        $context->addCharacteristics($characteristics, $user->id());
47
    }
48
}
49