Completed
Push — master ( 6dc48d...d8834f )
by Sebastian
02:14
created

ActionMapper::getAction()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 7.551
c 0
b 0
f 0
cc 7
eloc 15
nc 7
nop 2
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
        if (!$resource->supports($requestMethod)) {
30
            throw new UnsupportedRequestMethodException();
31
        }
32
33
        /** @var RestResource|SupportsDeleteRequests|SupportsGetRequests|SupportsPatchRequests|SupportsPostRequests|SupportsPutRequests $resource */
34
        switch ($requestMethod) {
35
            case new DeleteRequestMethod():
36
                return $resource->getDeleteCommand();
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...
37
            case new GetRequestMethod():
38
                return $resource->getQuery();
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...
39
            case new PatchRequestMethod():
40
                return $resource->getPatchCommand();
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...
41
            case new PostRequestMethod():
42
                return $resource->getPostCommand();
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...
43
            case new PutRequestMethod():
44
                return $resource->getPutCommand();
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...
45
        }
46
        throw new UnsupportedRequestMethodException();
47
    }
48
}
49