1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the zibios/sharep. |
7
|
|
|
* |
8
|
|
|
* (c) Zbigniew Ślązak |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace App\Slack\SlashCommand; |
12
|
|
|
|
13
|
|
|
use App\Entity\Parking\Member; |
14
|
|
|
use App\Repository\Entity\Parking\MemberRepository; |
15
|
|
|
use App\Slack\MessageBuilder\Layout; |
16
|
|
|
use App\Slack\SlashCommand\Sharep\ErrorMessage; |
17
|
|
|
use App\Slack\SlashCommand\Sharep\NotRecognizedUserMessage; |
18
|
|
|
use App\Slack\SlashCommand\Sharep\SharepCommandProcessor; |
19
|
|
|
use JMS\Serializer\SerializerInterface; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
|
22
|
|
|
class CommandHelper |
23
|
|
|
{ |
24
|
|
|
/** @var SharepCommandProcessor */ |
25
|
|
|
private $sharepCommandProcessor; |
26
|
|
|
/** @var ErrorMessage */ |
27
|
|
|
private $errorMessage; |
28
|
|
|
/** @var NotRecognizedUserMessage */ |
29
|
|
|
private $notRecognizedUserMessage; |
30
|
|
|
/** @var SerializerInterface */ |
31
|
|
|
private $serializer; |
32
|
|
|
/** @var MemberRepository */ |
33
|
|
|
private $memberRepository; |
34
|
|
|
/** @var LoggerInterface */ |
35
|
|
|
private $logger; |
36
|
|
|
|
37
|
3 |
|
public function __construct( |
38
|
|
|
SharepCommandProcessor $sharepCommandProcessor, |
39
|
|
|
ErrorMessage $errorMessage, |
40
|
|
|
NotRecognizedUserMessage $notRecognizedUserMessage, |
41
|
|
|
SerializerInterface $serializer, |
42
|
|
|
MemberRepository $memberRepository, |
43
|
|
|
LoggerInterface $logger |
44
|
|
|
) { |
45
|
3 |
|
$this->sharepCommandProcessor = $sharepCommandProcessor; |
46
|
3 |
|
$this->errorMessage = $errorMessage; |
47
|
3 |
|
$this->notRecognizedUserMessage = $notRecognizedUserMessage; |
48
|
3 |
|
$this->serializer = $serializer; |
49
|
3 |
|
$this->memberRepository = $memberRepository; |
50
|
3 |
|
$this->logger = $logger; |
51
|
3 |
|
} |
52
|
|
|
|
53
|
3 |
|
public function handleWebhook(array $data): Layout |
54
|
|
|
{ |
55
|
|
|
try { |
56
|
3 |
|
$message = null; |
57
|
3 |
|
$commandData = $this->calculateCommandData($data); |
58
|
|
|
|
59
|
3 |
|
$member = $this->memberRepository->findOneBySlackUserId($commandData->userId); |
60
|
3 |
|
if (!$member instanceof Member) { |
61
|
|
|
$message = $this->notRecognizedUserMessage->generate(); |
62
|
|
|
} |
63
|
|
|
|
64
|
3 |
|
if (SharepCommandProcessor::COMMAND === $commandData->command) { |
65
|
2 |
|
$message = $this->sharepCommandProcessor->process($commandData); |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
if (!$message instanceof Layout) { |
69
|
1 |
|
$message = $this->errorMessage->generate(); |
70
|
|
|
} |
71
|
2 |
|
} catch (\Throwable $e) { |
72
|
2 |
|
$this->logger->error($e); |
73
|
2 |
|
$message = $this->errorMessage->generate(); |
74
|
|
|
} |
75
|
|
|
|
76
|
3 |
|
return $message; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
private function calculateCommandData(array $data): CommandData |
80
|
|
|
{ |
81
|
3 |
|
$json = json_encode($data, JSON_THROW_ON_ERROR); |
82
|
|
|
/** @var CommandData $commandData */ |
83
|
3 |
|
$commandData = $this->serializer->deserialize($json, CommandData::class, 'json'); |
84
|
|
|
|
85
|
3 |
|
return $commandData; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|