Completed
Pull Request — master (#278)
by De Cramer
03:36
created

ChatNotification::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

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