Create::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 9.4285
nc 1
cc 1
eloc 9
nop 3
1
<?php
2
3
4
namespace Machdas\Action\Card;
5
6
use Machdas\Action;
7
use Machdas\Model\Card;
8
use Respect\Validation\Exceptions\NestedValidationException;
9
use Slim\Http\Request;
10
use Slim\Http\Response;
11
12
class Create extends Action\AbstractImpl
13
{
14
15
    /**
16
     * @param Request $request
17
     * @param Response $response
18
     * @param array $args
19
     * @return Response
20
     * @throws NestedValidationException
21
     */
22
    public function run(Request $request, Response $response, array $args) : Response
23
    {
24
        // create model
25
        $model       = new Card();
26
        $model->name = $request->getParsedBodyParam('name', false);
27
28
        // validation
29
        Card::validators()['name']->assert($model->name);
30
31
        // create
32
        $model->saveOrFail();
33
34
        // response
35
        return $response
36
            ->withStatus(201)
37
            ->withHeader('Location', sprintf('/cards/%s', $model->id))
38
            ->withJson($model);
39
    }
40
}
41