Completed
Push — master ( 403af5...380e9a )
by Rémi
07:37
created

MessageFactory::buildMessage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 3
crap 3
1
<?php
2
3
namespace MessageApp\Message;
4
5
use MessageApp\Message\TextExtractor\MessageTextExtractor;
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)
0 ignored issues
show
Unused Code introduced by
The parameter $language is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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);
41
42 9
        if ($messageText === null) {
43 3
            return null;
44
        }
45
46
        // $language = ($language) ? : self::getLanguage($filteredUsers); // TODO use it
47
48 6
        return new DefaultMessage($filteredUsers, vsprintf($messageText->getKey(), $messageText->getParameters()));
49
    }
50
51
    /**
52
     * @param  ApplicationUser[] $users
53
     * @return ApplicationUser[]
54
     */
55 18
    private static function filterUsers(array $users)
56
    {
57 18
        return array_values(
58 12
            array_unique(
59 18
                array_filter($users, function (ApplicationUser $user = null) {
60 15
                    return $user !== null && !$user instanceof UndefinedApplicationUser;
61 18
                })
62 12
            )
63 12
        );
64
    }
65
66
    /**
67
     * @param  ApplicationUser[] $users
68
     * @return string
69
     */
70
    private static function getLanguage(array $users)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
71
    {
72
        return $users[0]->getPreferredLanguage();
73
    }
74
}
75