Completed
Push — trunk ( 0e7a2e...6a6c5e )
by SuperNova.WS
07:28
created

ExceptionSnLocalized   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 41
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPlayerLocalization() 0 4 2
A __construct() 0 3 1
A getMessageLocalized() 0 7 3
1
<?php
2
/**
3
 * Created by Gorlum 04.06.2017 15:18
4
 */
5
6
namespace Common\Exceptions;
7
8
use Throwable;
9
10
/**
11
 * Class ExceptionSnLocalized
12
 *
13
 * Localized Exception - uses Message as locale string ID
14
 *
15
 * @package Exceptions
16
 */
17
class ExceptionSnLocalized extends \Exception {
18
19
  /**
20
   * @var array $sprintf
21
   */
22
  protected $sprintf = array();
23
24
  /**
25
   * ExceptionSnLocalized constructor.
26
   *
27
   * @param string         $message
28
   * @param int            $code
29
   * @param Throwable|null $previous
30
   * @param array          $sprintf - params for sprintf() call to embed into message
31
   */
32
  public function __construct($message = "", $code = 0, Throwable $previous = null, $sprintf = array()) {
33
    parent::__construct($message, $code, $previous);
34
    $this->sprintf = $sprintf;
35
  }
36
37
  /**
38
   * @param string $message
39
   *
40
   * @return string
41
   */
42
  protected function getPlayerLocalization($message) {
43
    global $lang;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
44
45
    return !empty($lang[$message]) ? $lang[$message] : '';
46
  }
47
48
  /**
49
   * @return string
50
   */
51
  public function getMessageLocalized() {
52
    $message = $this->getPlayerLocalization($this->getMessage());
53
    if (is_array($this->sprintf) && !empty($this->sprintf)) {
54
      $message = call_user_func_array('sprintf', array_merge(array($message), $this->sprintf));
55
    }
56
57
    return $message;
58
  }
59
60
}
61