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

MessageFactory::buildMessage()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.2
cc 4
eloc 8
nc 3
nop 3
crap 4
1
<?php
2
3
namespace MiniGameMessageApp\Message;
4
5
use MessageApp\Message\DefaultMessage;
6
use MessageApp\User\ApplicationUser;
7
use MessageApp\User\UndefinedApplicationUser;
8
9
class MessageFactory
10
{
11
    /**
12
     * @var MessageTextExtractor
13
     */
14
    private $extractor;
15
16
    /**
17
     * Constructor.
18
     *
19
     * @param MessageTextExtractor $extractor
20
     */
21 18
    public function __construct(MessageTextExtractor $extractor)
22
    {
23 18
        $this->extractor = $extractor;
24 18
    }
25
26
    /**
27
     * @param  ApplicationUser[] $users
28
     * @param  object            $object
29
     * @param  string            $language
30
     * @return DefaultMessage
31
     */
32 18
    public function buildMessage(array $users, $object, $language = null)
33
    {
34 18
        $filteredUsers = self::filterUsers($users);
35
36 18
        if (count($filteredUsers) === 0) {
37 9
            return null;
38
        }
39
40 9
        $messageText = $this->extractor->extractMessage($object, ($language) ? : self::getLanguage($filteredUsers));
41
42 9
        if ($messageText === null) {
43 3
            return null;
44
        }
45
46 6
        return new DefaultMessage($filteredUsers, $messageText);
0 ignored issues
show
Documentation introduced by
$filteredUsers is of type array<integer,object<Mes...\User\ApplicationUser>>, but the function expects a object<MessageApp\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...
47
    }
48
49
    /**
50
     * @param  ApplicationUser[] $users
51
     * @return ApplicationUser[]
52
     */
53 18
    private static function filterUsers(array $users)
54
    {
55 18
        return array_values(
56 12
            array_unique(
57 18
                array_filter($users, function (ApplicationUser $user = null) {
58 15
                    return $user !== null && !$user instanceof UndefinedApplicationUser;
59 18
                })
60 12
            )
61 12
        );
62
    }
63
64
    /**
65
     * @param  ApplicationUser[] $users
66
     * @return string
67
     */
68 3
    private static function getLanguage(array $users)
69
    {
70 3
        return $users[0]->getPreferredLanguage();
71
    }
72
}
73