Completed
Push — master ( eef030...971488 )
by Pavel
02:06
created

RestActionAbstract::dispatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 2
cts 4
cp 0.5
crap 2.5
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Pz\Doctrine\Rest\Action;
2
3
use Pz\Doctrine\Rest\RestRepository;
4
use Pz\Doctrine\Rest\RestRequest;
5
use Pz\Doctrine\Rest\RestResponse;
6
use Pz\Doctrine\Rest\RestResponseFactory;
7
8
abstract class RestActionAbstract
9
{
10
    /**
11
     * @var RestRepository
12
     */
13
    protected $repository;
14
15
    /**
16
     * @var RestResponseFactory
17
     */
18
    protected $response;
19
20
    /**
21
     * @param RestRequest $request
22
     *
23
     * @return RestResponse
24
     */
25
    abstract protected function handle(RestRequest $request);
26
27
    /**
28
     * RestActionAbstract constructor.
29
     *
30
     * @param RestRepository      $repository
31
     * @param RestResponseFactory $response
32
     */
33 6
    public function __construct(RestRepository $repository, RestResponseFactory $response)
34
    {
35 6
        $this->repository = $repository;
36 6
        $this->response = $response;
37 6
    }
38
39
    /**
40
     * @param RestRequest $request
41
     *
42
     * @return RestResponse
43
     */
44 6
    public function dispatch(RestRequest $request)
45
    {
46
        try {
47 6
            return $this->handle($request);
48
        } catch (\Exception $e) {
49
            return $this->response()->exception($e);
50
        }
51
    }
52
53
    /**
54
     * @return RestRepository
55
     */
56 6
    public function repository()
57
    {
58 6
        return $this->repository;
59
    }
60
61
    /**
62
     * @return RestResponseFactory
63
     */
64 6
    public function response()
65
    {
66 6
        return $this->response;
67
    }
68
}
69