Completed
Pull Request — master (#104)
by
unknown
03:28
created

ChatNotification::getMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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