Passed
Push — master ( 427ebd...80d980 )
by Bertrand
14:55 queued 07:32
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
48
        $canInteractUser = $this->getInteractUser();
49
        $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

49
        $interaction = $this->interactionRepository->getByInteractUser($canInteractUser, /** @scrutinizer ignore-type */ $pageId);
Loading history...
50
        if(!isset($interaction)) {
51
            $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

51
            $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

51
            $canInteractUser->addInteraction($interactions, /** @scrutinizer ignore-type */ $pageId, $doneValue);
Loading history...
52
            return;
53
        }
54
        $canInteractUser->updateInteraction($interaction, $interactions, $doneValue);
55
    }
56
57
    /**
58
     * @param array $interactions
59
     * @throws Exception
60
     */
61
    private function checkAllowedInteractions(array $interactions): void
62
    {
63
        if (empty(array_intersect($interactions, $this->allowedInteractions))) {
64
            throw new Exception('interaction_not_allowed');
65
        }
66
    }
67
68
    private function getInteractUser():CanInteract
69
    {
70
        $currentUser = $this->authGateway->current();
71
        if (isset($currentUser)) {
72
            return new RegisteredUser($currentUser->id());
73
        }
74
        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

74
        return new AnonymousUser(/** @scrutinizer ignore-type */ $this->authGateway->wikiSessionId());
Loading history...
75
    }
76
}
77