Completed
Push — dev ( 30dc8a...08778e )
by
unknown
11:46
created

ChatNotification   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85.71%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 64
rs 10
c 1
b 0
f 0
ccs 12
cts 14
cp 0.8571

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendMessage() 0 11 2
A __construct() 0 9 1
A getMessage() 0 4 1
1
<?php
2
3
namespace eXpansion\Framework\Core\Helpers;
4
5
use eXpansion\Framework\Core\Model\Helpers\ChatNotificationInterface;
6
use eXpansion\Framework\Core\Storage\PlayerStorage;
7
use Maniaplanet\DedicatedServer\Connection;
8
9
/**
10
 * Class ChatNotification
11
 *
12
 * @package eXpansion\Framework\Core\Helpers;
13
 * @author oliver de Cramer <[email protected]>
14
 */
15
class ChatNotification implements ChatNotificationInterface
16
{
17
    /** @var  Connection */
18
    protected $connection;
19
20
    /** @var Translations */
21
    protected $translations;
22
23
    /** @var PlayerStorage */
24
    protected $playerStorage;
25
26
    /**
27
     * ChatNotification constructor.
28
     *
29
     * @param Connection $connection
30
     * @param Translations $translations
31
     * @param PlayerStorage $playerStorage
32
     */
33 6
    public function __construct(
34
        Connection $connection,
35
        Translations $translations,
36
        PlayerStorage $playerStorage
37
    ) {
38 6
        $this->connection = $connection;
39 6
        $this->translations = $translations;
40 6
        $this->playerStorage = $playerStorage;
41 6
    }
42
43
    /**
44
     * Send message.
45
     *
46
     * @param string $messageId
47
     * @param string|string[]|null $to
48
     * @param string[] $parameters
49
     */
50 2
    public function sendMessage($messageId, $to = null, $parameters = [])
51
    {
52 2
        if (is_string($to)) {
53 1
            $player = $this->playerStorage->getPlayerInfo($to);
54 1
            $message = $this->translations->getTranslation($messageId, $parameters, strtolower($player->getLanguage()));
55
        } else {
56 1
            $message = $this->translations->getTranslations($messageId, $parameters);
57
        }
58
59 2
        $this->connection->chatSendServerMessage($message, $to);
0 ignored issues
show
Bug introduced by
It seems like $message defined by $this->translations->get...messageId, $parameters) on line 56 can also be of type array; however, Maniaplanet\DedicatedSer...chatSendServerMessage() does only seem to accept string|array<integer,array<integer,string>>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60 2
    }
61
62
    /**
63
     * Return messageId with arguments as a string
64
     * Usage: used for retrieving partials for chat messages
65
     *  * defaults to English locale, without parameters
66
     *
67
     * @param $messageId
68
     * @param array $parameters
69
     * @param string $locale
70
     * @return mixed
71
     */
72
    public function getMessage($messageId, $parameters = [], $locale = "en")
73
    {
74
        return $this->translations->getTranslation($messageId, $parameters, $locale);
75
    }
76
77
78
}
79