ApiException::getApiCodeError()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace TopMcFrance\Api;
4
5
/**
6
 * Provide exception when the check code isn't valid
7
 *
8
 * @author Jérôme Desjardins <[email protected]>
9
 */
10
class ApiException extends \Exception
11
{
12
    protected $useDate = null;
13
    
14
    protected $apiMessage = null;
15
    
16
    protected $apiCodeError = 0;
17
    
18
    /**
19
     * Return the date of use for already use exception
20
     * @return string
21
     */
22
    public function getUseDate()
23
    {
24
        return $this->useDate;
25
    }
26
    
27
    /**
28
     * get api message error
29
     * @return string
30
     */
31
    public function getApiMessage()
32
    {
33
        return $this->apiMessage;
34
    }
35
36
    /**
37
     * Get api code error
38
     * @return string
39
     */
40
    public function getApiCodeError()
41
    {
42
        return $this->apiCodeError;
43
    }
44
 
45
    /**
46
     * Exception if code already use
47
     * @param string $code
48
     * @param mixed $response
49
     * @return \self
50
     */
51 1
    public static function alreadyUsed($code, $response){
52 1
        $exception = new self(sprintf('Code %s has already used at %s.',
53 1
            $code,
54 1
            $response->date_use
55 1
        ));
56
        
57 1
        $exception->useDate = $response->date_use;
58 1
        $exception->baseException($response);
59
        
60 1
        return $exception;
61
    }
62
    
63
    /**
64
     * Exception if code isn't valid
65
     * @param string $code
66
     * @param mixed $response
67
     * @return \self
68
     */
69 1
    public static function invalidCode($code, $response){
70 1
        $exception = new self(sprintf('Code %s isn\'t valid.',
71
            $code
72 1
        ));
73
         
74 1
        $exception->baseException($response);
75
        
76 1
        return $exception;
77
    }
78
    
79
    /**
80
     * Exception if unknow code
81
     * @param mixed $response
82
     * @return \self
83
     */
84
    public static function unknowException($message, $response){
85
        $exception = new self(sprintf('Unknow code error : %s.',
86
            $message
87
        ));
88
         
89
        $exception->baseException($response);
90
        
91
        return $exception;
92
    } 
93
    
94 2
    private function baseException($response){
95 2
        $this->apiCodeError = $response->erreur;
96 2
        $this->apiMessage = $response->detail;
97 2
    }
98
}
99