Failed Conditions
Push — master ( eac8ff...e4a057 )
by Zbigniew
04:46
created

CommandHelper::handleWebhook()   A

Complexity

Conditions 5
Paths 11

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.7283

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 9
cts 13
cp 0.6923
rs 9.2248
c 0
b 0
f 0
cc 5
nc 11
nop 1
crap 5.7283
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\Data\CommandData;
17
use App\Slack\PredefinedMessage\ErrorMessage;
18
use App\Slack\PredefinedMessage\NotRecognizedUserMessage;
19
use App\Slack\SlashCommand\Sharep\SharepCommandProcessor;
20
use JMS\Serializer\SerializerInterface;
21
use Psr\Log\LoggerInterface;
22
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
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