1 | <?php |
||
23 | class CommandHelper |
||
24 | { |
||
25 | /** @var SharepCommandProcessor */ |
||
26 | private $sharepCommandProcessor; |
||
27 | /** @var ErrorMessage */ |
||
28 | private $errorMessage; |
||
29 | /** @var NotRecognizedUserMessage */ |
||
30 | private $notRecognizedUserMessage; |
||
31 | /** @var SerializerInterface */ |
||
32 | private $serializer; |
||
33 | /** @var MemberRepository */ |
||
34 | private $memberRepository; |
||
35 | /** @var LoggerInterface */ |
||
36 | private $logger; |
||
37 | |||
38 | 3 | public function __construct( |
|
39 | SharepCommandProcessor $sharepCommandProcessor, |
||
40 | ErrorMessage $errorMessage, |
||
41 | NotRecognizedUserMessage $notRecognizedUserMessage, |
||
42 | SerializerInterface $serializer, |
||
43 | MemberRepository $memberRepository, |
||
44 | LoggerInterface $logger |
||
45 | ) { |
||
46 | 3 | $this->sharepCommandProcessor = $sharepCommandProcessor; |
|
47 | 3 | $this->errorMessage = $errorMessage; |
|
48 | 3 | $this->notRecognizedUserMessage = $notRecognizedUserMessage; |
|
49 | 3 | $this->serializer = $serializer; |
|
50 | 3 | $this->memberRepository = $memberRepository; |
|
51 | 3 | $this->logger = $logger; |
|
52 | 3 | } |
|
53 | |||
54 | 3 | public function handleWebhook(array $data): Layout |
|
55 | { |
||
56 | try { |
||
57 | 3 | $message = null; |
|
58 | 3 | $commandData = $this->calculateCommandData($data); |
|
59 | |||
60 | 3 | if (!$this->memberRepository->findOneBySlackUserId($commandData->userId) instanceof Member) { |
|
61 | return $this->notRecognizedUserMessage->generate(); |
||
62 | } |
||
63 | |||
64 | 3 | if (SharepCommandProcessor::COMMAND === $commandData->command) { |
|
65 | 2 | $message = $this->sharepCommandProcessor->process($commandData); |
|
66 | } |
||
67 | |||
68 | 3 | if (!$message instanceof Layout) { |
|
69 | 3 | $message = $this->errorMessage->generate(); |
|
70 | } |
||
71 | } catch (\Throwable $e) { |
||
72 | $this->logger->error($e); |
||
73 | $message = $this->errorMessage->generate(); |
||
74 | } |
||
75 | |||
76 | 3 | return $message; |
|
77 | } |
||
78 | |||
79 | 3 | private function calculateCommandData(array $data): CommandData |
|
87 | } |
||
88 |