Completed
Push — master ( 3e9c3d...99411a )
by Hugo
22s queued 11s
created

WannaSpeakApiException::create()   C

Complexity

Conditions 13
Paths 13

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 6.6166
c 0
b 0
f 0
cc 13
nc 13
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yproximite\WannaSpeakBundle\Exception\Api;
6
7
abstract class WannaSpeakApiException extends \RuntimeException implements WannaSpeakApiExceptionInterface
8
{
9
    private $statusCode;
10
11
    /**
12
     * @return static
13
     */
14
    public static function create(int $statusCode, string $message)
15
    {
16
        switch ($statusCode) {
17
            case 401:
18
                if ('Name already Exists' === $message) {
19
                    throw new SoundNameAlreadyExistsException($statusCode, $message);
20
                }
21
22
                throw new AuthFailedException($statusCode, $message);
23
            case 403:
24
                throw new BadAccountException($statusCode, $message);
25
            case 404:
26
                throw new UnknownMethodException($statusCode, $message);
27
            case 405:
28
                throw new MethodNotImplementedException($statusCode, $message);
29
            case 406:
30
                throw new NoDidAvailableForRegionException($statusCode, $message);
31
            case 407:
32
                if ('DID not exists or not owned' === $message) {
33
                    throw new DidNotExistsOrNotOwnedException($statusCode, $message);
34
                } elseif ('DID already reserved or not tested' === $message) {
35
                    throw new DidAlreadyReservedException($statusCode, $message);
36
                }
37
38
                throw new UnknownException($statusCode, $message);
39
            case 410:
40
                throw new CantUseDidAsDestinationException($statusCode, $message);
41
            case 500:
42
                throw new MissingArgumentsException($statusCode, $message);
43
            case 501:
44
                throw new UnknownApiException($statusCode, $message);
45
            default:
46
                throw new UnknownException(-1, $message);
47
        }
48
    }
49
50
    public function __construct(int $statusCode, string $message)
51
    {
52
        parent::__construct($message);
53
        $this->statusCode = $statusCode;
54
    }
55
56
    public function getStatusCode(): int
57
    {
58
        return $this->statusCode;
59
    }
60
}
61