GetAllTasks   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 38
c 0
b 0
f 0
wmc 3
lcom 1
cbo 7
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B run() 0 28 3
1
<?php
2
3
4
namespace Machdas\Action\Card;
5
6
use Machdas\Action;
7
use Machdas\Model\Card;
8
use Machdas\Model\Task;
9
use Machdas\Utils\DatabaseUtils;
10
use Slim\Http\Request;
11
use Slim\Http\Response;
12
13
class GetAllTasks extends Action\AbstractImpl
14
{
15
16
    /**
17
     * @param Request $request
18
     * @param Response $response
19
     * @param array $args
20
     * @return Response
21
     */
22
    public function run(Request $request, Response $response, array $args) : Response
23
    {
24
        // get card
25
        /* @var Card $card */
26
        $card = Card::query()->findOrFail($args['id']);
27
28
        /** @noinspection PhpUndefinedMethodInspection */
29
        $builder = Task::query()->where('cardId', '=', $card->id);
30
31
        // sorting
32
        if (!is_null($request->getParam('order-by'))) {
33
            list($attribute, $direction) = explode(',', $request->getParam('order-by'));
34
            $direction = DatabaseUtils::ensureOrderDirection($direction);
35
36
            // check if attribute is valid
37
            if (!in_array($attribute, ['priority', 'name', 'isDone'])) {
38
                $attribute = 'id';
39
            }
40
41
            /** @noinspection PhpUndefinedMethodInspection */
42
            $builder->orderBy($attribute, $direction);
43
        } else {
44
            /** @noinspection PhpUndefinedMethodInspection */
45
            $builder->orderBy('id', 'desc');
46
        }
47
48
        return $response->withJson($builder->get());
49
    }
50
}
51