Passed
Push — master ( a69d6c...5d5b4c )
by Manuele
04:23 queued 12s
created

Messages   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 46
ccs 0
cts 26
cp 0
rs 10
wmc 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A get() 0 30 6
1
<?php
2
3
/**
4
 * Votifier PHP Client
5
 *
6
 * @package   VotifierClient
7
 * @author    Manuele Vaccari <[email protected]>
8
 * @copyright Copyright (c) 2017-2020 Manuele Vaccari <[email protected]>
9
 * @license   https://github.com/D3strukt0r/votifier-client-php/blob/master/LICENSE.txt GNU General Public License v3.0
10
 * @link      https://github.com/D3strukt0r/votifier-client-php
11
 */
12
13
namespace D3strukt0r\VotifierClient;
14
15
/**
16
 * Internal use for translations.
17
 */
18
class Messages
19
{
20
    const NOT_VOTIFIER = 1;
21
    const NOT_SENT_PACKAGE = 2;
22
    const NOT_RECEIVED_PACKAGE = 3;
23
24
    const NUVOTIFIER_SERVER_ERROR = 100;
25
26
    /**
27
     * Translate and format a translation.
28
     *
29
     * @param int         $messageCode (Required) The message code to identify the required resource
30
     * @param string|null $language    (Optional) The language code (e. g. en, de, es).
31
     *
32
     * @return string returns the message in the specified language
33
     */
34
    public static function get(int $messageCode, string $language = null): string
35
    {
36
        $messages = [
37
            'en' => [
38
                self::NOT_VOTIFIER => 'The connection does not belong to Votifier',
39
                self::NOT_SENT_PACKAGE => 'Couldn\'t write to remote host',
40
                self::NOT_RECEIVED_PACKAGE => 'Unable to read server response',
41
                self::NUVOTIFIER_SERVER_ERROR => 'Votifier server error: {0}: {1}',
42
            ],
43
        ];
44
45
        $requestedMessage = $messages[$language ?: 'en'][$messageCode];
46
47
        $argsCount = \func_num_args();
48
49
        if ($argsCount > 2) {
50
            $firstArg = func_get_arg(2);
51
            if (\is_array($firstArg)) {
52
                foreach ($firstArg as $key => $value) {
53
                    $requestedMessage = str_replace('{'.$key.'}', $value, $requestedMessage);
54
                }
55
            } else {
56
                for ($i = 2; $i < $argsCount; ++$i) {
57
                    $arg = func_get_arg($i);
58
                    $requestedMessage = str_replace('{'.($i - 2).'}', $arg, $requestedMessage);
59
                }
60
            }
61
        }
62
63
        return $requestedMessage;
64
    }
65
}
66