Completed
Push — master ( ee19ca...fc64c3 )
by Rémi
03:30
created

GameResultHandler::getLanguage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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