Completed
Push — master ( ae2b3a...9c69f7 )
by Sebastian
03:09
created

ActionMapper::getAction()   C

Complexity

Conditions 11
Paths 11

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
rs 5.2653
cc 11
eloc 23
nc 11
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
namespace Kartenmacherei\RestFramework;
3
4
use Kartenmacherei\RestFramework\Action\Action;
5
use Kartenmacherei\RestFramework\Request\Method\DeleteRequestMethod;
6
use Kartenmacherei\RestFramework\Request\Method\GetRequestMethod;
7
use Kartenmacherei\RestFramework\Request\Method\PatchRequestMethod;
8
use Kartenmacherei\RestFramework\Request\Method\PostRequestMethod;
9
use Kartenmacherei\RestFramework\Request\Method\PutRequestMethod;
10
use Kartenmacherei\RestFramework\Request\Method\RequestMethod;
11
use Kartenmacherei\RestFramework\Request\Method\UnsupportedRequestMethodException;
12
use Kartenmacherei\RestFramework\RestResource\RestResource;
13
use Kartenmacherei\RestFramework\RestResource\SupportsDeleteRequests;
14
use Kartenmacherei\RestFramework\RestResource\SupportsGetRequests;
15
use Kartenmacherei\RestFramework\RestResource\SupportsPatchRequests;
16
use Kartenmacherei\RestFramework\RestResource\SupportsPostRequests;
17
use Kartenmacherei\RestFramework\RestResource\SupportsPutRequests;
18
19
class ActionMapper
20
{
21
    /**
22
     * @param RequestMethod $requestMethod
23
     * @param RestResource $resource
24
     * @return Action
25
     * @throws UnsupportedRequestMethodException
26
     */
27
    public function getAction(RequestMethod $requestMethod, RestResource $resource): Action
28
    {
29
        switch ($requestMethod) {
30
            case new DeleteRequestMethod():
31
                if (!$resource instanceof SupportsDeleteRequests) {
32
                    throw new UnsupportedRequestMethodException();
33
                }
34
                return $resource->getDeleteCommand();
35
            case new GetRequestMethod():
36
                if (!$resource instanceof SupportsGetRequests) {
37
                    throw new UnsupportedRequestMethodException();
38
                }
39
                return $resource->getQuery();
40
            case new PatchRequestMethod():
41
                if (!$resource instanceof SupportsPatchRequests) {
42
                    throw new UnsupportedRequestMethodException();
43
                }
44
                return $resource->getPatchCommand();
45
            case new PostRequestMethod():
46
                if (!$resource instanceof SupportsPostRequests) {
47
                    throw new UnsupportedRequestMethodException();
48
                }
49
                return $resource->getPostCommand();
50
            case new PutRequestMethod():
51
                if (!$resource instanceof SupportsPutRequests) {
52
                    throw new UnsupportedRequestMethodException();
53
                }
54
                return $resource->getPutCommand();
55
        }
56
        throw new UnsupportedRequestMethodException();
57
    }
58
}
59