Completed
Push — odev ( a25a56...21e788 )
by De Cramer
02:29
created

ChatNotification   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 55
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A sendMessage() 0 13 2
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
    protected $colorCodes = [];
27
28
    /**
29
     * ChatNotification constructor.
30
     *
31
     * @param Connection $connection
32
     * @param Translations $translations
33
     */
34 6
    public function __construct(
35
        Connection $connection,
36
        Translations $translations,
37
        PlayerStorage $playerStorage,
38
        $colorCodes
39
    ){
40 6
        $this->connection = $connection;
41 6
        $this->translations = $translations;
42 6
        $this->playerStorage = $playerStorage;
43
44 6
        foreach ($colorCodes as $code => $colorCode) {
45 6
            $this->colorCodes["{" . $code . "}"] = '$z' . $colorCode;
46
        }
47 6
    }
48
49
    /**
50
     * Send message.
51
     *
52
     * @param string $messageId
53
     * @param string|string[]|null $to
54
     * @param string[] $parameters
55
     */
56 2
    public function sendMessage($messageId, $to = null, $parameters = [])
57
    {
58 2
        $parameters = array_merge($this->colorCodes, $parameters);
59
60 2
        if (is_string($to)) {
61 1
            $player = $this->playerStorage->getPlayerInfo($to);
62 1
            $message = $this->translations->getTranslation($messageId, $parameters, strtolower($player->getLanguage()));
63
        } else {
64 1
            $message = $this->translations->getTranslations($messageId, $parameters);
65
        }
66
67 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 64 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...
68
    }
69
}