Completed
Push — master ( 31a9ba...a5eb40 )
by Sebastian
04:58
created

ActionMapper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 8
dl 0
loc 35
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C getAction() 0 26 7
1
<?php
2
namespace Kartenmacherei\RestFramework;
3
4
use Kartenmacherei\RestFramework\Action\Action;
5
use Kartenmacherei\RestFramework\Request\DeleteRequest;
6
use Kartenmacherei\RestFramework\Request\GetRequest;
7
use Kartenmacherei\RestFramework\Request\Method\DeleteRequestMethod;
8
use Kartenmacherei\RestFramework\Request\Method\GetRequestMethod;
9
use Kartenmacherei\RestFramework\Request\Method\PatchRequestMethod;
10
use Kartenmacherei\RestFramework\Request\Method\PostRequestMethod;
11
use Kartenmacherei\RestFramework\Request\Method\PutRequestMethod;
12
use Kartenmacherei\RestFramework\Request\Method\UnsupportedRequestMethodException;
13
use Kartenmacherei\RestFramework\Request\PatchRequest;
14
use Kartenmacherei\RestFramework\Request\PostRequest;
15
use Kartenmacherei\RestFramework\Request\PutRequest;
16
use Kartenmacherei\RestFramework\Request\Request;
17
use Kartenmacherei\RestFramework\RestResource\RestResource;
18
use Kartenmacherei\RestFramework\RestResource\SupportsDeleteRequests;
19
use Kartenmacherei\RestFramework\RestResource\SupportsGetRequests;
20
use Kartenmacherei\RestFramework\RestResource\SupportsPatchRequests;
21
use Kartenmacherei\RestFramework\RestResource\SupportsPostRequests;
22
use Kartenmacherei\RestFramework\RestResource\SupportsPutRequests;
23
24
class ActionMapper
25
{
26
    /**
27
     * @param Request $request
28
     * @param RestResource $resource
29
     * @return Action
30
     * @throws UnsupportedRequestMethodException
31
     */
32
    public function getAction(Request $request, RestResource $resource): Action
33
    {
34
        if (!$resource->supports($request->getMethod())) {
35
            throw new UnsupportedRequestMethodException();
36
        }
37
38
        /** @var RestResource|SupportsDeleteRequests|SupportsGetRequests|SupportsPatchRequests|SupportsPostRequests|SupportsPutRequests $resource */
39
        switch ($request->getMethod()) {
40
            case new DeleteRequestMethod():
41
                /** @var DeleteRequest $request */
42
                return $resource->getDeleteCommand($request);
0 ignored issues
show
Bug introduced by
The method getDeleteCommand does only exist in Kartenmacherei\RestFrame...\SupportsDeleteRequests, but not in Kartenmacherei\RestFrame...rce\SupportsPutRequests.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
43
            case new GetRequestMethod():
44
                /** @var GetRequest $request */
45
                return $resource->getQuery($request);
0 ignored issues
show
Bug introduced by
The method getQuery does only exist in Kartenmacherei\RestFrame...rce\SupportsGetRequests, but not in Kartenmacherei\RestFrame...rce\SupportsPutRequests.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
46
            case new PatchRequestMethod():
47
                /** @var PatchRequest $request */
48
                return $resource->getPatchCommand($request);
0 ignored issues
show
Bug introduced by
The method getPatchCommand does only exist in Kartenmacherei\RestFrame...e\SupportsPatchRequests, but not in Kartenmacherei\RestFrame...rce\SupportsPutRequests.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
49
            case new PostRequestMethod():
50
                /** @var PostRequest $request */
51
                return $resource->getPostCommand($request);
0 ignored issues
show
Bug introduced by
The method getPostCommand does only exist in Kartenmacherei\RestFrame...ce\SupportsPostRequests, but not in Kartenmacherei\RestFrame...rce\SupportsPutRequests.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
52
            case new PutRequestMethod():
53
                /** @var PutRequest $request */
54
                return $resource->getPutCommand($request);
0 ignored issues
show
Bug introduced by
The method getPutCommand does only exist in Kartenmacherei\RestFrame...rce\SupportsPutRequests, but not in Kartenmacherei\RestFrame...ce\SupportsPostRequests.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
55
        }
56
        throw new UnsupportedRequestMethodException();
57
    }
58
}
59