|
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) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
71
|
|
|
{ |
|
72
|
|
|
return $users[0]->getPreferredLanguage(); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.