Completed
Push — master ( 8b4718...61d59f )
by Oleg
02:27
created

Exception::getRateLimitRemaining()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @abstract API client for Optimizely.
4
 * @author Oleg Krivtsov <[email protected]>
5
 * @date 14 November 2016
6
 * @copyright (c) 2016, Web Marketing ROI
7
 */
8
namespace WebMarketingROI\OptimizelyPHP;
9
10
/**
11
 * Contains error/exception information returned by Optimizely API.
12
 */
13
class Exception extends \Exception
14
{
15
    // Error codes returned as part of exception information.
16
    const CODE_INVALID_ARG       = -1; // Invalid argument passed.
17
    const CODE_CURL_ERROR        = -2; // Some error in CURL.
18
    const CODE_API_ERROR         = -3; // Some error condition returned by Optimizely API.
19
    
20
    /**
21
     * HTTP response code.
22
     * @var integer
23
     */
24
    private $httpCode;
25
    
26
    /**
27
     * Error UUID extracted from response JSON.
28
     * @var type 
29
     */
30
    private $uuid;
31
    
32
    /**
33
     * The limit for this endpoint
34
     * @var integer
35
     */
36
    private $rateLimit;
37
    
38
    /**
39
     * The amount of calls remaining for this endpoint
40
     * @var integer
41
     */
42
    private $rateLimitRemaining;
43
    
44
    /**
45
     * The X-RateLimit-Reset header provides a Unix UTC timestamp, letting you 
46
     * know the exact time that your fresh new rate limit kicks in.
47
     * @var string
48
     */
49
    private $rateLimitReset;
50
    
51
    /**
52
     * Constructor.
53
     */
54 25
    public function __construct($message, $code = self::CODE_INVALID_ARG, $options = array())
55
    {
56 25
        parent::__construct($message, $code);
57
        
58 25
        if (isset($options['http_code']))
59 25
            $this->setHttpCode($options['http_code']);
60
                
61 25
        if (isset($options['uuid']))
62 25
            $this->setUuid($options['uuid']);
63
        
64 25
        if (isset($options['rate_limit']))
65 25
            $this->setRateLimit($options['rate_limit']);
66
        
67 25
        if (isset($options['rate_limit_remaining']))
68 25
            $this->setRateLimitRemaining($options['rate_limit_remaining']);
69
        
70 25
        if (isset($options['rate_limit_reset']))
71 25
            $this->setRateLimitReset($options['rate_limit_reset']);
72 25
    }
73
    
74
    /**
75
     * Return HTTP response code.
76
     * @return integer
77
     */
78 2
    public function getHttpCode()
79
    {
80 2
        return $this->httpCode;
81
    }
82
            
83
    /**
84
     * Set HTTP response code.
85
     * @param integer $code
0 ignored issues
show
Bug introduced by
There is no parameter named $code. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
86
     */
87 3
    public function setHttpCode($httpCode)
88
    {
89 3
        $this->httpCode = $httpCode;
90 3
    }
91
    
92
    /**
93
     * Return error UUID.
94
     * @return string
95
     */
96 1
    public function getUuid()
97
    {
98 1
        return $this->uuid;
99
    }
100
            
101
    /**
102
     * Set message
103
     * @param string $message
0 ignored issues
show
Bug introduced by
There is no parameter named $message. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
104
     */
105 1
    public function setUuid($uuid)
106
    {
107 1
        $this->uuid = $uuid;
108 1
    }    
109
    
110
    /**
111
     * Return rate limit.
112
     * @return integer|null
113
     */
114 1
    public function getRateLimit()
115
    {
116 1
        return $this->rateLimit;
117
    }
118
    
119
    /**
120
     * Set rate limit.
121
     * @param integer|null $rateLimit
122
     */
123 1
    public function setRateLimit($rateLimit)
124
    {
125 1
        $this->rateLimit = $rateLimit;
126 1
    }
127
    
128
    /**
129
     * Return the amount of calls remaining.
130
     * @return integer|null
131
     */
132 1
    public function getRateLimitRemaining()
133
    {
134 1
        return $this->rateLimitRemaining;
135
    }
136
    
137
    /**
138
     * Set the amount of calls remaining.
139
     * @param integer|null $rateLimitRemaining
140
     */
141 1
    public function setRateLimitRemaining($rateLimitRemaining)
142
    {
143 1
        $this->rateLimitRemaining = $rateLimitRemaining;
144 1
    }
145
    
146
    /**
147
     * Return the exact time that your fresh new rate limit kicks in.
148
     * @return integer|null
149
     */
150 1
    public function getRateLimitReset()
151
    {
152 1
        return $this->rateLimitReset;
153
    }
154
    
155
    /**
156
     * Set the exact time that your fresh new rate limit kicks in.
157
     * @param integer|null $rateLimitReset
158
     */
159 1
    public function setRateLimitReset($rateLimitReset)
160
    {
161 1
        $this->rateLimitReset = $rateLimitReset;
0 ignored issues
show
Documentation Bug introduced by
It seems like $rateLimitReset can also be of type integer. However, the property $rateLimitReset is declared as type string. 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...
162 1
    }
163
}
164
165
166