Completed
Pull Request — master (#170)
by De Cramer
03:33
created

ChatNotification   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 92%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 88
ccs 23
cts 25
cp 0.92
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessage() 0 4 1
A __construct() 0 11 1
C sendMessage() 0 30 7
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 7
    public function __construct(
40
        Connection $connection,
41
        Translations $translations,
42
        PlayerStorage $playerStorage,
43
        Console $console
44
    ) {
45 7
        $this->connection = $connection;
46 7
        $this->translations = $translations;
47 7
        $this->playerStorage = $playerStorage;
48 7
        $this->console = $console;
49 7
    }
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) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Core\Model\UserGroups\Group does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
73
            $to = implode(",", $to->getLogins());
74
            $message = $this->translations->getTranslations($messageId, $parameters);
75
        }
76
77 3
        if ($to === null || $to instanceof Group) {
0 ignored issues
show
Bug introduced by
The class eXpansion\Framework\Core\Model\UserGroups\Group does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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 2
        } catch (UnknownPlayerException $e) {
85
            // Nothing to do, it happens.
86
        }
87 1
    }
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