SortExtension   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 54
ccs 20
cts 22
cp 0.9091
rs 10
wmc 5
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A implementedEvents() 0 6 1
A findEntities() 0 23 4
1
<?php
2
/**
3
 * Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
4
 *
5
 * Licensed under The MIT License
6
 * Redistributions of files must retain the above copyright notice.
7
 *
8
 * @copyright Copyright 2016 - 2018, Cake Development Corporation (http://cakedc.com)
9
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
10
 */
11
12
namespace CakeDC\Api\Service\Action\Extension;
13
14
use CakeDC\Api\Service\Action\Action;
15
use Cake\Event\Event;
16
use Cake\Event\EventListenerInterface;
17
use Cake\ORM\Entity;
18
19
/**
20
 * Class SortExtension
21
 *
22
 * @package CakeDC\Api\Service\Action\Extension
23
 */
24
class SortExtension extends Extension implements EventListenerInterface
25
{
26
27
    /**
28
     * @var array
29
     */
30
    protected $_defaultConfig = [
31
        'sortField' => 'sort',
32
        'directionField' => 'direction',
33
    ];
34
35
    /**
36
     * Returns a list of events this object is implementing. When the class is registered
37
     * in an event manager, each individual method will be associated with the respective event.
38
     *
39
     * @return array
40
     */
41 6
    public function implementedEvents()
42
    {
43
        return [
44 6
            'Action.Crud.onFindEntities' => 'findEntities',
45 6
        ];
46
    }
47
48
    /**
49
     * find entities
50
     *
51
     * @param Event $event An Event instance
52
     * @return Entity
53
     */
54 5
    public function findEntities(Event $event)
55
    {
56 5
        $action = $event->getSubject();
57 5
        $query = $event->getData('query');
58 5
        if ($event->result) {
59
            $query = $event->result;
60
        }
61 5
        $data = $action->getData();
62 5
        $direction = 'asc';
63 5
        $sort = null;
0 ignored issues
show
Unused Code introduced by
$sort is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
65 5
        $directionField = $this->getConfig('directionField');
66 5
        $sortField = $this->getConfig('sortField');
67 5
        if (!empty($data[$directionField])) {
68 2
            $direction = $data[$directionField];
69 2
        }
70 5
        if (!empty($data[$sortField])) {
71 4
            $sort = $data[$sortField];
72 4
            $query->order([$sort => $direction]);
73 4
        }
74
75 5
        return $query;
76
    }
77
}
78