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

ActionMapper   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
wmc 11
lcom 0
cbo 6
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C getAction() 0 31 11
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