Completed
Push — dev ( ade326...e1b211 )
by
unknown
03:23
created

ChatNotification::sendMessage()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 22
Code Lines 12

Duplication

Lines 8
Ratio 36.36 %

Code Coverage

Tests 11
CRAP Score 4.0582

Importance

Changes 0
Metric Value
dl 8
loc 22
ccs 11
cts 13
cp 0.8462
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 12
nc 8
nop 3
crap 4.0582
1
<?php
2
3
namespace eXpansion\Framework\Core\Helpers;
4
5
use eXpansion\Framework\Core\Model\Helpers\ChatNotificationInterface;
6
use eXpansion\Framework\Core\Services\Console;
7
use eXpansion\Framework\Core\Storage\PlayerStorage;
8
use Maniaplanet\DedicatedServer\Connection;
9
10
/**
11
 * Class ChatNotification
12
 *
13
 * @package eXpansion\Framework\Core\Helpers;
14
 * @author oliver de Cramer <[email protected]>
15
 */
16
class ChatNotification implements ChatNotificationInterface
17
{
18
    /** @var  Connection */
19
    protected $connection;
20
21
    /** @var Translations */
22
    protected $translations;
23
24
    /** @var PlayerStorage */
25
    protected $playerStorage;
26
27
    /** @var Console */
28
    protected $console;
29
30
    /**
31
     * ChatNotification constructor.
32
     *
33
     * @param Connection $connection
34
     * @param Translations $translations
35
     * @param PlayerStorage $playerStorage
36
     */
37 6
    public function __construct(
38
        Connection $connection,
39
        Translations $translations,
40
        PlayerStorage $playerStorage,
41
        Console $console
42
    ) {
43 6
        $this->connection = $connection;
44 6
        $this->translations = $translations;
45 6
        $this->playerStorage = $playerStorage;
46 6
        $this->console = $console;
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
        $message = $messageId;
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
        }
64
65 2 View Code Duplication
        if (is_array($to)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
            $to = implode(",", $to);
67
            $message = $this->translations->getTranslations($messageId, $parameters);
68
        }
69
70 2 View Code Duplication
        if ($to === null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71 1
            $message = $this->translations->getTranslations($messageId, $parameters);
72 1
            $this->console->writeln(end($message)['Text']);
73
        }
74
75 2
        $this->connection->chatSendServerMessage($message, $to);
0 ignored issues
show
Bug introduced by
It seems like $message 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...
76
77 2
    }
78
79
    /**
80
     * Return messageId with arguments as a string
81
     * Usage: used for retrieving partials for chat messages
82
     *  * defaults to English locale, without parameters
83
     *
84
     * @param $messageId
85
     * @param array $parameters
86
     * @param string $locale
87
     * @return mixed
88
     */
89
    public function getMessage($messageId, $parameters = [], $locale = "en")
90
    {
91
        return $this->translations->getTranslation($messageId, $parameters, $locale);
92
    }
93
94
95
}
96