Completed
Push — master ( fec51f...63eb9a )
by Rémi
03:56
created

GameResultHandler::getUserByContext()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
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\MessageFactory;
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 MiniGameApp\Event\MiniGameAppErrorEvent;
16
use MiniGameMessageApp\Finder\MiniGameUserFinder;
17
use MiniGameMessageApp\MiniGameApplicationUser;
18
use Psr\Log\LoggerAwareInterface;
19
use Psr\Log\LoggerAwareTrait;
20
use Psr\Log\NullLogger;
21
use RemiSan\Context\Context;
22
23
class GameResultHandler implements MessageEventHandler, LoggerAwareInterface
24
{
25
    use LoggerAwareTrait;
26
27
    /**
28
     * @var MiniGameUserFinder
29
     */
30
    private $userFinder;
31
32
    /**
33
     * @var ContextUserFinder
34
     */
35
    private $contextUserFinder;
36
37
    /**
38
     * @var MessageFinder
39
     */
40
    private $messageFinder;
41
42
    /**
43
     * @var MessageFactory
44
     */
45
    private $messageFactory;
46
47
    /**
48
     * @var MessageSender
49
     */
50
    private $messageSender;
51
52
    /**
53
     * Constructor
54
     *
55
     * @param MiniGameUserFinder $userFinder
56
     * @param ContextUserFinder  $contextUserFinder
57
     * @param MessageFinder      $messageFinder
58
     * @param MessageFactory     $messageFactory
59
     * @param MessageSender      $messageSender
60
     */
61 18
    public function __construct(
62
        MiniGameUserFinder $userFinder,
63
        ContextUserFinder $contextUserFinder,
64
        MessageFinder $messageFinder,
65
        MessageFactory $messageFactory,
66
        MessageSender $messageSender
67
    ) {
68 18
        $this->userFinder = $userFinder;
69 18
        $this->contextUserFinder = $contextUserFinder;
70 18
        $this->messageFinder = $messageFinder;
71 18
        $this->messageFactory = $messageFactory;
72 18
        $this->messageSender = $messageSender;
73 18
        $this->logger = new NullLogger();
74 18
    }
75
76
    /**
77
     * Handle an event.
78
     *
79
     * @param EventInterface $event
80
     * @param Context        $context
81
     *
82
     * @return void
83
     */
84 18
    public function handle(EventInterface $event, Context $context = null)
85
    {
86 18
        if (! $event instanceof GameResult) {
87 3
            return;
88
        }
89
90 15
        $this->logger->info(sprintf('Send message after "%s"', $event->getName()));
91 15
        $messageContext = $this->getMessageContext($context);
92
93 15
        $users = $this->getUsersList($event, $messageContext);
94
95 12
        $this->sendMessage($event, $users, $messageContext);
0 ignored issues
show
Documentation introduced by
$users is of type array<integer,object<Mes...\ApplicationUser>|null>, but the function expects a array<integer,object<Mes...\User\ApplicationUser>>.

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...
96 12
    }
97
98
    /**
99
     * @param  GameResult        $gameResult
100
     * @param  ApplicationUser[] $users
101
     * @param  mixed             $messageContext
102
     * @return void
103
     */
104 12
    private function sendMessage(GameResult $gameResult, array $users = [], $messageContext = null)
105
    {
106 12
        $message = $this->messageFactory->buildMessage($users, $gameResult);
107
108 12
        if (!$message) {
109 3
            $this->logger->warning('Message could not be generated');
110 3
            return;
111
        }
112
113 9
        $this->messageSender->send($message, $messageContext);
114 9
    }
115
116
    /**
117
     * @param  Context $context
118
     * @return mixed
119
     */
120 15
    private function getMessageContext(Context $context = null)
121
    {
122 15
        if (!$context) {
123 9
            return null;
124
        }
125
126 6
        $message = $this->messageFinder->findByReference($context->getValue());
127 6
        return ($message) ? $message->getSource() : null;
128
    }
129
130
    /**
131
     * @param  PlayerId $playerId
132
     * @param  mixed    $contextMessage
133
     * @return ApplicationUser
134
     */
135 12
    private function getUser(PlayerId $playerId = null, $contextMessage = null)
136
    {
137
        // Build message
138 12
        $user = $this->getUserByPlayerId($playerId);
139 12
        if (!$user) {
140 9
            $this->logger->debug('Try to get user by context message');
141 9
            $user = $this->getUserByContext($contextMessage);
142 9
            if (!$user) {
143 6
                if (!$playerId) {
144 3
                    $this->logger->debug('No user was found');
145 3
                    return null;
146
                }
147 3
                throw new \InvalidArgumentException('User not found!');
148
            }
149 2
        }
150
151 6
        return $user;
152
    }
153
154
    /**
155
     * @param  PlayerId $id
156
     * @return ApplicationUser
157
     */
158 12
    private function getUserByPlayerId(PlayerId $id = null)
159
    {
160 12
        return ($id) ? $this->userFinder->getByPlayerId($id) : null;
161
    }
162
163
    /**
164
     * @param  mixed $contextMessage
165
     * @return ApplicationUser
166
     */
167 9
    private function getUserByContext($contextMessage = null)
168
    {
169 9
        if (!$contextMessage) {
170 3
            $this->logger->debug('No context message to retrieve user from');
171 3
            return null;
172
        };
173
174 6
        return $this->contextUserFinder->getUserByContextMessage($contextMessage);
175
    }
176
177
    /**
178
     * @param GameResult $event
179
     * @param mixed      $messageContext
180
     *
181
     * @return MiniGameApplicationUser[]
182
     */
183 15
    private function getUsersList(GameResult $event, $messageContext)
184
    {
185 15
        if ($event instanceof AllPlayersResult) {
186 3
            return $this->userFinder->getByGameId($event->getGameId());
187
        }
188
189 12
        return [$this->getUser($event->getPlayerId(), $messageContext)];
190
    }
191
}
192