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

RestResource::getMethodMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
namespace Kartenmacherei\RestFramework\RestResource;
3
4
use Kartenmacherei\RestFramework\Request\Method\DeleteRequestMethod;
5
use Kartenmacherei\RestFramework\Request\Method\GetRequestMethod;
6
use Kartenmacherei\RestFramework\Request\Method\PatchRequestMethod;
7
use Kartenmacherei\RestFramework\Request\Method\PostRequestMethod;
8
use Kartenmacherei\RestFramework\Request\Method\PutRequestMethod;
9
use Kartenmacherei\RestFramework\Request\Method\RequestMethod;
10
11
abstract class RestResource
12
{
13
    /**
14
     * @param RequestMethod $method
15
     * @return bool
16
     */
17
    public function supports(RequestMethod $method): bool
18
    {
19
        return in_array($method, $this->getSupportedMethods());
20
    }
21
    
22
    /**
23
     * @return RequestMethod[]
24
     */
25
    public function getSupportedMethods(): array
26
    {
27
        $implementedInterfaces = class_implements($this);
28
        return array_values(array_intersect_key($this->getMethodMap(), $implementedInterfaces));
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    private function getMethodMap(): array 
35
    {
36
        return [
37
            SupportsDeleteRequests::class => new DeleteRequestMethod(),
38
            SupportsGetRequests::class => new GetRequestMethod(),
39
            SupportsPatchRequests::class => new PatchRequestMethod(),
40
            SupportsPostRequests::class => new PostRequestMethod(),
41
            SupportsPutRequests::class => new PutRequestMethod()
42
        ];        
43
    }
44
}
45