Error::getException()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
namespace Realshadow\Redtube\Entities;
4
5
use JMS\Serializer\Annotation as JMS;
6
use Realshadow\Redtube\Exceptions\NoInputParametersSpecifiedException;
7
use Realshadow\Redtube\Exceptions\NoSuchDataProviderException;
8
use Realshadow\Redtube\Exceptions\NoSuchMethodException;
9
use Realshadow\Redtube\Exceptions\NotFoundException;
10
11
12
/**
13
 * Error
14
 *
15
 * @package Realshadow\Redtube\Entities
16
 * @author Lukáš Homza <[email protected]>
17
 *
18
 * @JMS\XmlRoot("error")
19
 */
20
class Error
21
{
22
23
    /**
24
     * @var string $message
25
     *
26
     * @JMS\XmlElement()
27
     * @JMS\Type("string")
28
     * @JMS\SerializedName("message")
29
     */
30
    protected $message;
31
32
    /**
33
     * @var int $code
34
     *
35
     * @JMS\XmlElement()
36
     * @JMS\Type("integer")
37
     * @JMS\SerializedName("code")
38
     */
39
    protected $code;
40
41
    /**
42
     * Map of APIs error codes to exceptions
43
     *
44
     * @var array $exceptionMap
45
     *
46
     * @JMS\Exclude()
47
     */
48
    private $exceptionMap = [
49
        1001 => NoSuchMethodException::class,
50
        1002 => NoSuchDataProviderException::class,
51
        1003 => NoInputParametersSpecifiedException::class,
52
    ];
53
54
    /**
55
     * Get the corresponding exception mapped to APIs error code
56
     *
57
     * @return NotFoundException
58
     */
59
    public function getException()
60
    {
61
        if (isset($this->exceptionMap[$this->code])) {
62
            $exception = new $this->exceptionMap[$this->code];
63
        } else {
64
            $exception = new NotFoundException($this->message, $this->code);
65
        }
66
67
        return $exception;
68
    }
69
70
}
71