Completed
Branch master (220ce5)
by De Cramer
16:11
created

ChatNotification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 6
cts 6
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
crap 1
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
use Maniaplanet\DedicatedServer\Xmlrpc\UnknownPlayerException;
10
11
/**
12
 * Class ChatNotification
13
 *
14
 * @package eXpansion\Framework\Core\Helpers;
15
 * @author oliver de Cramer <[email protected]>
16
 */
17
class ChatNotification implements ChatNotificationInterface
18
{
19
    /** @var  Connection */
20
    protected $connection;
21
22
    /** @var Translations */
23
    protected $translations;
24
25
    /** @var PlayerStorage */
26
    protected $playerStorage;
27
28
    /** @var Console */
29
    protected $console;
30
31
    /**
32
     * ChatNotification constructor.
33
     *
34
     * @param Connection $connection
35
     * @param Translations $translations
36
     * @param PlayerStorage $playerStorage
37
     */
38 8
    public function __construct(
39
        Connection $connection,
40
        Translations $translations,
41
        PlayerStorage $playerStorage,
42
        Console $console
43
    ) {
44 8
        $this->connection = $connection;
45 8
        $this->translations = $translations;
46 8
        $this->playerStorage = $playerStorage;
47 8
        $this->console = $console;
48 8
    }
49
50
    /**
51
     * Send message.
52
     *
53
     * @param string $messageId
54
     * @param string|string[]|null $to
55
     * @param string[] $parameters
56
     */
57 3
    public function sendMessage($messageId, $to = null, $parameters = [])
58
    {
59 3
        $message = $messageId;
60
61 3
        if (is_string($to)) {
62 1
            $player = $this->playerStorage->getPlayerInfo($to);
63 1
            $message = $this->translations->getTranslation($messageId, $parameters, strtolower($player->getLanguage()));
64 1
        }
65
66 3 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...
67 1
            $to = implode(",", $to);
68 1
            $message = $this->translations->getTranslations($messageId, $parameters);
69 1
        }
70
71 3 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...
72 1
            $message = $this->translations->getTranslations($messageId, $parameters);
73 1
            $this->console->writeln(end($message)['Text']);
74 1
        }
75
76
        try {
77 3
            $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...
78 3
        } catch (UnknownPlayerException $e) {
79
            // Nothing to do, it happens.
80
        }
81 3
    }
82
83
    /**
84
     * Return messageId with arguments as a string
85
     * Usage: used for retrieving partials for chat messages
86
     *  * defaults to English locale, without parameters
87
     *
88
     * @param string $messageId
89
     * @param array $parameters
90
     * @param string $locale
91
     * @return string
92
     */
93 1
    public function getMessage($messageId, $parameters = [], $locale = "en")
94
    {
95 1
        return $this->translations->getTranslation($messageId, $parameters, $locale);
96
    }
97
98
99
}
100