AbstractImpl::__invoke()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
nc 3
cc 3
eloc 9
nop 3
1
<?php
2
3
namespace Machdas\Action;
4
5
use Illuminate\Database\Eloquent\ModelNotFoundException;
6
use Machdas\Action;
7
use Respect\Validation\Exceptions\NestedValidationException;
8
use Slim\Exception\NotFoundException;
9
use Slim\Http\Request;
10
use Slim\Http\Response;
11
12
abstract class AbstractImpl implements Action
13
{
14
15
    /**
16
     * @param Request $request
17
     * @param Response $response
18
     * @param array $args
19
     * @return Response
20
     * @throws NestedValidationException
21
     * @throws ModelNotFoundException
22
     */
23
    abstract public function run(Request $request, Response $response, array $args) : Response;
24
25
    /**
26
     * @param Request $request
27
     * @param Response $response
28
     * @param array $args
29
     * @return Response
30
     * @throws NotFoundException
31
     */
32
    public function __invoke(Request $request, Response $response, array $args) : Response
33
    {
34
        try {
35
            return $this->run($request, $response, $args);
36
        } catch (NestedValidationException $e) {
37
            // validation error
38
            return $response
39
                ->withStatus(400)
40
                ->withJson(['message' => $e->getFullMessage()]);
41
        } catch (ModelNotFoundException $e) {
42
            throw new NotFoundException($request, $response);
43
        }
44
    }
45
}
46