Completed
Push — master ( 6d89cf...2ab47d )
by Rémi
02:41
created

GameResultHandler::handle()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 20
ccs 14
cts 14
cp 1
rs 9.2
cc 4
eloc 12
nc 4
nop 2
crap 4
1
<?php
2
3
namespace MiniGameMessageApp\Handler;
4
5
use League\Event\EventInterface;
6
use MessageApp\Finder\MessageFinder;
7
use MessageApp\Listener\MessageEventHandler;
8
use MessageApp\Message\DefaultMessage;
9
use MessageApp\Message\Sender\MessageSender;
10
use MessageApp\User\ApplicationUser;
11
use MessageApp\User\Finder\ContextUserFinder;
12
use MiniGame\Entity\PlayerId;
13
use MiniGame\GameResult;
14
use MiniGame\Result\AllPlayersResult;
15
use MiniGameMessageApp\Message\MiniGameMessageExtractor;
16
use MiniGameMessageApp\ReadModel\Finder\MiniGameUserFinder;
17
use Psr\Log\LoggerAwareInterface;
18
use Psr\Log\LoggerAwareTrait;
19
use Psr\Log\NullLogger;
20
use RemiSan\Context\Context;
21
22
class GameResultHandler implements MessageEventHandler, LoggerAwareInterface
23
{
24
    use LoggerAwareTrait;
25
26
    /**
27
     * @var MiniGameUserFinder
28
     */
29
    private $userFinder;
30
31
    /**
32
     * @var ContextUserFinder
33
     */
34
    private $contextUserFinder;
35
36
    /**
37
     * @var MessageFinder
38
     */
39
    private $messageFinder;
40
41
    /**
42
     * @var MessageSender
43
     */
44
    private $messageSender;
45
46
    /**
47
     * @var MiniGameMessageExtractor
48
     */
49
    private $extractor;
50
51
    /**
52
     * Constructor
53
     *
54
     * @param MiniGameUserFinder       $userFinder
55
     * @param ContextUserFinder        $contextUserFinder
56
     * @param MessageFinder            $messageFinder
57
     * @param MessageSender            $messageSender
58
     * @param MiniGameMessageExtractor $extractor
59
     */
60 18
    public function __construct(
61
        MiniGameUserFinder $userFinder,
62
        ContextUserFinder $contextUserFinder,
63
        MessageFinder $messageFinder,
64
        MessageSender $messageSender,
65
        MiniGameMessageExtractor $extractor
66
    ) {
67 18
        $this->userFinder = $userFinder;
68 18
        $this->contextUserFinder = $contextUserFinder;
69 18
        $this->messageFinder = $messageFinder;
70 18
        $this->messageSender = $messageSender;
71 18
        $this->extractor = $extractor;
72 18
        $this->logger = new NullLogger();
73 18
    }
74
75
    /**
76
     * Handle an event.
77
     *
78
     * @param EventInterface $event
79
     * @param Context        $context
80
     *
81
     * @return void
82
     */
83 18
    public function handle(EventInterface $event, Context $context = null)
84
    {
85 18
        if (! $event instanceof GameResult) {
86 3
            return;
87
        }
88
89 15
        $this->logger->info(sprintf('Send message after "%s"', $event->getName()));
90
91 15
        $messageContext = $this->getMessageContext($context);
92
93 15
        if ($event instanceof AllPlayersResult) { // TODO message could have multiple targets
94 3
            $users = $this->userFinder->getByGameId($event->getGameId());
95 3
            foreach ($users as $user) {
96 3
                $this->sendMessage($event, $user, $messageContext);
97 3
            }
98 2
        } else {
99
            $user = $this->getUser($event->getPlayerId(), $messageContext);
100 2
            $this->sendMessage($event, $user, $messageContext);
101 2
        }
102 2
    }
103 12
104 9
    /**
105 9
     * @param  EventInterface  $event
106 6
     * @param  ApplicationUser $user
107 9
     * @param  mixed           $messageContext
108 6
     * @return void
109 6
     */
110
    private function sendMessage(EventInterface $event, ApplicationUser $user = null, $messageContext = null)
111 6
    {
112
        if (!$user) {
113 12
            return;
114
        }
115
116
        $message = new DefaultMessage(
117
            $user,
118
            $this->extractor->extractMessage(
119
                $event,
0 ignored issues
show
Documentation introduced by
$event is of type object<League\Event\EventInterface>, but the function expects a object<MiniGame\GameResult>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
120
                $user->getPreferredLanguage()
121 12
            )
122
        );
123 12
        $this->messageSender->send($message, $messageContext);
124 3
    }
125
126
    /**
127 9
     * @param  Context $context
128 9
     * @return mixed
129 9
     */
130
    private function getMessageContext(Context $context = null)
131
    {
132
        if (!$context) {
133
            return null;
134
        }
135 15
136
        $message = $this->messageFinder->findByReference($context->getValue());
137 15
        return ($message) ? $message->getSource() : null;
138 9
    }
139
140
    /**
141 6
     * @param  PlayerId $playerId
142 6
     * @param  mixed    $contextMessage
143
     * @return ApplicationUser
144
     */
145
    private function getUser(PlayerId $playerId = null, $contextMessage = null)
146
    {
147
        // Build message
148
        $user = $this->getUserByPlayerId($playerId);
149
        if (!$user) {
150 12
            $this->logger->debug('Try to get user by context message');
151
            $user = $this->getUserByContext($contextMessage);
152
            if (!$user) {
153 12
                if (!$playerId) {
154 12
                    $this->logger->debug('No user was found');
155 9
                    return null;
156 9
                }
157 9
                throw new \InvalidArgumentException('User not found!');
158 6
            }
159 3
        }
160 3
161
        return $user;
162 3
    }
163
164 2
    /**
165
     * @param  PlayerId $id
166 6
     * @return ApplicationUser
167
     */
168
    private function getUserByPlayerId(PlayerId $id = null)
169
    {
170
        return ($id) ? $this->userFinder->getByPlayerId($id) : null;
171
    }
172
173 12
    /**
174
     * @param  mixed $contextMessage
175 12
     * @return ApplicationUser
176
     */
177
    private function getUserByContext($contextMessage = null)
178
    {
179
        if (!$contextMessage) {
180
            $this->logger->debug('No context message to retrieve user from');
181
            return null;
182 9
        };
183
184 9
        return $this->contextUserFinder->getUserByContextMessage($contextMessage);
185 3
    }
186
}
187