Completed
Push — master ( 33476c...427ebd )
by Bertrand
09:14
created

HandleInteractions::checkPageExist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
4
namespace App\Src\UseCases\Domain\Users\Interactions;
5
6
7
use App\Src\UseCases\Domain\Agricultural\Model\AnonymousUser;
8
use App\Src\UseCases\Domain\Agricultural\Model\CanInteract;
9
use App\Src\UseCases\Domain\Agricultural\Model\RegisteredUser;
10
use App\Src\UseCases\Domain\Exceptions\PageNotFound;
11
use App\Src\UseCases\Domain\Ports\InteractionRepository;
12
use App\Src\UseCases\Domain\Ports\PageRepository;
13
use App\Src\UseCases\Domain\Shared\Gateway\AuthGateway;
14
use Exception;
15
16
class HandleInteractions
17
{
18
    private $pageRepository;
19
    private $interactionRepository;
20
    private $authGateway;
21
22
    private $allowedInteractions = [
23
        'follow', 'unfollow', 'done', 'undone', 'applause', 'unapplause'
24
    ];
25
26
    public function __construct(
27
        PageRepository $pageRepository,
28
        InteractionRepository $interactionRepository,
29
        AuthGateway $authGateway
30
    )
31
    {
32
        $this->pageRepository = $pageRepository;
33
        $this->interactionRepository = $interactionRepository;
34
        $this->authGateway = $authGateway;
35
    }
36
37
    /**
38
     * @param string $pageId
39
     * @param array $interactions
40
     * @param array $doneValue
41
     * @throws PageNotFound
42
     * @throws Exception
43
     */
44
    public function execute(string $pageId, array $interactions, array $doneValue = []):void
45
    {
46
        $this->checkAllowedInteractions($interactions);
47
        $this->checkPageExist($pageId);
48
49
        $canInteractUser = $this->getInteractUser();
50
        $interaction = $this->interactionRepository->getByInteractUser($canInteractUser, $pageId);
0 ignored issues
show
Bug introduced by
$pageId of type string is incompatible with the type integer expected by parameter $pageId of App\Src\UseCases\Domain\...ry::getByInteractUser(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $interaction = $this->interactionRepository->getByInteractUser($canInteractUser, /** @scrutinizer ignore-type */ $pageId);
Loading history...
51
        if(!isset($interaction)) {
52
            $canInteractUser->addInteraction($interactions, $pageId, $doneValue);
0 ignored issues
show
Bug introduced by
$pageId of type string is incompatible with the type integer expected by parameter $pageId of App\Src\UseCases\Domain\...dUser::addInteraction(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
            $canInteractUser->addInteraction($interactions, /** @scrutinizer ignore-type */ $pageId, $doneValue);
Loading history...
Bug introduced by
$pageId of type string is incompatible with the type integer expected by parameter $pageId of App\Src\UseCases\Domain\...sUser::addInteraction(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

52
            $canInteractUser->addInteraction($interactions, /** @scrutinizer ignore-type */ $pageId, $doneValue);
Loading history...
53
            return;
54
        }
55
        $canInteractUser->updateInteraction($interaction, $interactions, $doneValue);
56
    }
57
58
    private function checkPageExist(string $pageId): void
59
    {
60
        $page = $this->pageRepository->get($pageId);
61
        if (!isset($page)) {
62
            throw new PageNotFound(PageNotFound::error);
63
        }
64
    }
65
66
    /**
67
     * @param array $interactions
68
     * @throws Exception
69
     */
70
    private function checkAllowedInteractions(array $interactions): void
71
    {
72
        if (empty(array_intersect($interactions, $this->allowedInteractions))) {
73
            throw new Exception('interaction_not_allowed');
74
        }
75
    }
76
77
    private function getInteractUser():CanInteract
78
    {
79
        $currentUser = $this->authGateway->current();
80
        if (isset($currentUser)) {
81
            return new RegisteredUser($currentUser->id());
82
        }
83
        return new AnonymousUser($this->authGateway->wikiSessionId());
0 ignored issues
show
Bug introduced by
It seems like $this->authGateway->wikiSessionId() can also be of type null; however, parameter $identifier of App\Src\UseCases\Domain\...mousUser::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        return new AnonymousUser(/** @scrutinizer ignore-type */ $this->authGateway->wikiSessionId());
Loading history...
84
    }
85
}
86