Completed
Push — master ( 6c633d...7a6af2 )
by keika
10:40
created

ExceptionFactory::createClientException()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.4286
ccs 13
cts 13
cp 1
cc 3
eloc 13
nc 3
nop 1
crap 3
1
<?php
2
3
namespace keika299\ConohaAPI\Common\Exceptions\Network;
4
5
6
use keika299\ConohaAPI\Common\Exceptions\ConohaAPIException;
7
use GuzzleHttp\Exception\RequestException as GuzzleHttpRequestException;
8
use keika299\ConohaAPI\Common\Exceptions\Network\Client;
9
10
class ExceptionFactory
11
{
12
    /**
13
     * Class ExceptionFactory
14
     *
15
     * This class create ConohaAPIException.
16
     *
17
     * @param \Exception $exception
18
     * @return \keika299\ConohaAPI\Common\Exceptions\IConohaAPIException
19
     */
20 1
    public static function build(\Exception $exception)
21
    {
22 1
        if ($exception instanceof GuzzleHttpRequestException) {
23 1
            return ExceptionFactory::createClientException($exception);
24
        }
25
26
        return new ConohaAPIException('Conoha API Exception', 0, $exception);
27
    }
28
29
    /**
30
     * @param GuzzleHttpRequestException $exception
31
     * @return Client\ClientException
32
     */
33 1
    private static function createClientException(GuzzleHttpRequestException $exception)
34
    {
35
        $exceptionArray = array(
36 1
            400 => 'BadRequest',
37 1
            401 => 'Unauthorized',
38 1
            403 => 'Forbidden',
39 1
            404 => 'NotFound',
40 1
            405 => 'MethodNotAllowed',
41
            406 => 'NotAcceptable'
42 1
        );
43
44 1
        foreach ($exceptionArray as $item => $value) {
45 1
            if ($item === $exception->getCode()) {
46 1
                $exceptionName = __NAMESPACE__ . '\Client\\' . $value . 'Exception';
47 1
                return new $exceptionName($exception);
48
            }
49 1
        }
50
51 1
        return new RequestException($exception);
52
    }
53
}
54