Completed
Push — master ( 86e7b3...2c087d )
by Rémi
04:54
created

MessageFactory   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 96.15%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 5
dl 0
loc 79
ccs 25
cts 26
cp 0.9615
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B buildMessage() 0 24 5
A filterUsers() 0 10 2
A getLanguage() 0 4 1
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
use RemiSan\Intl\ResourceTranslator;
9
10
class MessageFactory
11
{
12
    /**
13
     * @var MessageTextExtractor
14
     */
15
    private $extractor;
16
17
    /**
18
     * @var ResourceTranslator
19
     */
20
    private $resourceTranslator;
21
22
    /**
23
     * Constructor.
24
     *
25
     * @param MessageTextExtractor $extractor
26
     * @param ResourceTranslator   $resourceTranslator
27
     */
28 18
    public function __construct(MessageTextExtractor $extractor, ResourceTranslator $resourceTranslator)
29
    {
30 18
        $this->extractor = $extractor;
31 18
        $this->resourceTranslator = $resourceTranslator;
32 18
    }
33
34
    /**
35
     * @param  ApplicationUser[] $users
36
     * @param  object            $object
37
     * @param  string            $language
38
     * @return DefaultMessage
39
     */
40 18
    public function buildMessage(array $users, $object, $language = null)
41
    {
42 18
        $filteredUsers = self::filterUsers($users);
43
44 18
        if (count($filteredUsers) === 0) {
45 9
            return null;
46
        }
47
48 9
        $messageResource = $this->extractor->extractMessage($object);
49
50 9
        if ($messageResource === null) {
51 3
            return null;
52
        }
53
54 6
        $language = ($language) ? : self::getLanguage($filteredUsers);
55
56
        try {
57 6
            $translatedText = $this->resourceTranslator->translate($language, $messageResource);
58 4
        } catch (\IntlException $e) {
0 ignored issues
show
Bug introduced by
The class IntlException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
59
            $translatedText = $messageResource->getKey();
60
        }
61
62 6
        return new DefaultMessage($filteredUsers, $translatedText);
63
    }
64
65
    /**
66
     * @param  ApplicationUser[] $users
67
     * @return ApplicationUser[]
68
     */
69 18
    private static function filterUsers(array $users)
70
    {
71 18
        return array_values(
72 12
            array_unique(
73 18
                array_filter($users, function (ApplicationUser $user = null) {
74 15
                    return $user !== null && !$user instanceof UndefinedApplicationUser;
75 18
                })
76 12
            )
77 12
        );
78
    }
79
80
    /**
81
     * @param  ApplicationUser[] $users
82
     * @return string
83
     */
84 3
    private static function getLanguage(array $users)
85
    {
86 3
        return $users[0]->getPreferredLanguage();
87
    }
88
}
89