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); |
|
|
|
|
51
|
|
|
if(!isset($interaction)) { |
52
|
|
|
$canInteractUser->addInteraction($interactions, $pageId, $doneValue); |
|
|
|
|
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()); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|