GameResultHandler::getUsersList()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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 MiniGameMessageApp\Finder\MiniGameUserFinder;
16
use Psr\Log\LoggerAwareInterface;
17
use Psr\Log\LoggerAwareTrait;
18
use Psr\Log\NullLogger;
19
use RemiSan\Context\Context;
20
21
class GameResultHandler implements MessageEventHandler, LoggerAwareInterface
22
{
23
    use LoggerAwareTrait;
24
25
    /**
26
     * @var MiniGameUserFinder
27
     */
28
    private $userFinder;
29
30
    /**
31
     * @var ContextUserFinder
32
     */
33
    private $contextUserFinder;
34
35
    /**
36
     * @var MessageFinder
37
     */
38
    private $messageFinder;
39
40
    /**
41
     * @var MessageFactory
42
     */
43
    private $messageFactory;
44
45
    /**
46
     * @var MessageSender
47
     */
48
    private $messageSender;
49
50
    /**
51
     * Constructor
52
     *
53
     * @param MiniGameUserFinder $userFinder
54
     * @param ContextUserFinder  $contextUserFinder
55
     * @param MessageFinder      $messageFinder
56
     * @param MessageFactory     $messageFactory
57
     * @param MessageSender      $messageSender
58
     */
59 18
    public function __construct(
60
        MiniGameUserFinder $userFinder,
61
        ContextUserFinder $contextUserFinder,
62
        MessageFinder $messageFinder,
63
        MessageFactory $messageFactory,
64
        MessageSender $messageSender
65
    ) {
66 18
        $this->userFinder = $userFinder;
67 18
        $this->contextUserFinder = $contextUserFinder;
68 18
        $this->messageFinder = $messageFinder;
69 18
        $this->messageFactory = $messageFactory;
70 18
        $this->messageSender = $messageSender;
71 18
        $this->logger = new NullLogger();
72 18
    }
73
74
    /**
75
     * Handle an event.
76
     *
77
     * @param EventInterface $event
78
     * @param Context        $context
79
     *
80
     * @return void
81
     */
82 18
    public function handle(EventInterface $event, Context $context = null)
83
    {
84 18
        if (! $event instanceof GameResult) {
85 3
            return;
86
        }
87
88 15
        $this->logger->info(sprintf('Send message after "%s"', $event->getName()));
89 15
        $messageContext = $this->getMessageContext($context);
90
91 15
        $users = $this->getUsersList($event, $messageContext);
92
93 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...
94 12
    }
95
96
    /**
97
     * @param  GameResult        $gameResult
98
     * @param  ApplicationUser[] $users
99
     * @param  mixed             $messageContext
100
     * @return void
101
     */
102 12
    private function sendMessage(GameResult $gameResult, array $users = [], $messageContext = null)
103
    {
104 12
        $message = $this->messageFactory->buildMessage($users, $gameResult);
105
106 12
        if (!$message) {
107 3
            $this->logger->warning('Message could not be generated');
108 3
            return;
109
        }
110
111 9
        $this->messageSender->send($message, $messageContext);
112 9
    }
113
114
    /**
115
     * @param  Context $context
116
     * @return mixed
117
     */
118 15
    private function getMessageContext(Context $context = null)
119
    {
120 15
        if (!$context) {
121 9
            return null;
122
        }
123
124 6
        $message = $this->messageFinder->findByReference($context->getValue());
125 6
        return ($message) ? $message->getSource() : null;
126
    }
127
128
    /**
129
     * @param  PlayerId $playerId
130
     * @param  mixed    $contextMessage
131
     * @return ApplicationUser
132
     */
133 12
    private function getUser(PlayerId $playerId = null, $contextMessage = null)
134
    {
135
        // Build message
136 12
        $user = $this->getUserByPlayerId($playerId);
137 12
        if (!$user) {
138 9
            $this->logger->debug('Try to get user by context message');
139 9
            $user = $this->getUserByContext($contextMessage);
140 9
            if (!$user) {
141 6
                if (!$playerId) {
142 3
                    $this->logger->debug('No user was found');
143 3
                    return null;
144
                }
145 3
                throw new \InvalidArgumentException('User not found!');
146
            }
147 2
        }
148
149 6
        return $user;
150
    }
151
152
    /**
153
     * @param  PlayerId $id
154
     * @return ApplicationUser
155
     */
156 12
    private function getUserByPlayerId(PlayerId $id = null)
157
    {
158 12
        return ($id) ? $this->userFinder->getByPlayerId($id) : null;
159
    }
160
161
    /**
162
     * @param  mixed $contextMessage
163
     * @return ApplicationUser
164
     */
165 9
    private function getUserByContext($contextMessage = null)
166
    {
167 9
        if (!$contextMessage) {
168 3
            $this->logger->debug('No context message to retrieve user from');
169 3
            return null;
170
        };
171
172 6
        return $this->contextUserFinder->getUserByContextMessage($contextMessage);
173
    }
174
175
    /**
176
     * @param GameResult $event
177
     * @param mixed      $messageContext
178
     *
179
     * @return ApplicationUser[]
180
     */
181 15
    private function getUsersList(GameResult $event, $messageContext)
182
    {
183 15
        if ($event instanceof AllPlayersResult) {
184 3
            return $this->userFinder->getByGameId($event->getGameId());
185
        }
186
187 12
        return [ $this->getUser($event->getPlayerId(), $messageContext) ];
188
    }
189
}
190