AbstractEmitter::getParam()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Apix\Log\Emitter;
4
5
use Apix\Log\Logger;
6
use Apix\Log\Logger\LoggerInterface;
7
8
abstract class AbstractEmitter implements EmitterInterface
9
{
10
    /**
11
     * Holds the emitter/transporter's URL.
12
     *
13
     * @var string
14
     */
15
    protected $url;
16
17
    /**
18
     * Holds the emitter/transporter's debug mode.
19
     *
20
     * @var bool
21
     */
22
    protected $debug = false;
23
24
    /**
25
     * Holds the query parameters.
26
     *
27
     * @var array
28
     */
29
    protected $params = array();
30
31
    /**
32
     * Sets the emitter/transporter's URL.
33
     *
34
     * @param string $url
35
     */
36 64
    public function setUrl($url)
37
    {
38 64
        $this->url = vsprintf($url, $this->params);
39 64
    }
40
41
    /**
42
     * Sets the emitter/transporter's debug mode.
43
     *
44
     * @param bool $bool
45
     */
46 108
    public function setDebug($bool = true)
47
    {
48 108
        $this->debug = (bool) $bool;
49 108
    }
50
51
    /**
52
     * Returns the current query parameters.
53
     *
54
     * @return array
55
     */
56 76
    public function getParams()
57
    {
58 76
        return $this->params;
59
    }
60
61
    /**
62
     * Checks wether the named query parameter exists.
63
     *
64
     * @param string $key
65
     *
66
     * @return bool
67
     */
68 12
    public function hasParam($key)
69
    {
70 12
        return isset($this->params[$key]);
71
    }
72
73
    /**
74
     * Returns the named query parameter.
75
     *
76
     * @param string $key
77
     *
78
     * @return mixed
79
     */
80 16
    public function getParam($key)
81
    {
82 16
        return $this->params[$key];
83
    }
84
85
    /**
86
     * Sets a query parameter.
87
     *
88
     * @param string $key
89
     * @param mixed  $value
90
     */
91 76
    public function setParam($key, $value)
92
    {
93 76
        $this->params[$key] = $value;
94 76
    }
95
96
    /**
97
     * Merges new query parameters.
98
     *
99
     * @param array $params
100
     *
101
     * @return array
102
     */
103 108
    public function setParams(array $params)
104
    {
105 108
        $this->params = $params;
106 108
    }
107
108
    /**
109
     * Merges new query parameters.
110
     *
111
     * @param array $params
112
     *
113
     * @return array
114
     */
115 108
    public function addParams(array $params)
116
    {
117 108
        $this->params = array_merge($this->params, $params);
118 108
    }
119
120
    /**
121
     * @var Logger
122
     */
123
    protected $error_handler = null;
124
125 4
    public function setErrorHandler(LoggerInterface $logger)
126
    {
127 4
        $this->error_handler = $logger;
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger of type object<Apix\Log\Logger\LoggerInterface> is incompatible with the declared type object<Apix\Log\Logger> of property $error_handler.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
128 4
    }
129
130
    /**
131
     * Returns the.
132
     */
133 8
    public function getErrorHandler()
134
    {
135 8
        $this->error_handler = $this->error_handler ?: new Logger\ErrorLog('/dev/stdout');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->error_handler ?: ...ErrorLog('/dev/stdout') can also be of type object<Apix\Log\Logger\ErrorLog>. However, the property $error_handler is declared as type object<Apix\Log\Logger>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
136
137 8
        return $this->error_handler;
138
    }
139
140
    /**
141
     * Handle error.
142
     *
143
     * @param string $code
144
     * @param string $msg
145
     */
146 4
    public function handleError($code, $msg)
147
    {
148 4
        $logger = $this->getErrorHandler();
149 4
        $logger->error(
150 4
            '{0} - {1} - Code #{2}',
151 4
            array(get_class($this), $msg, $code)
152 3
        );
153 4
    }
154
}
155