Failed Conditions
Push — master ( 37c7e1...8f957f )
by Zbigniew
05:34
created

CommandHelper::handleWebhook()   A

Complexity

Conditions 5
Paths 17

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.583

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 10
cts 14
cp 0.7143
rs 9.2088
c 0
b 0
f 0
cc 5
nc 17
nop 1
crap 5.583
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 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