Completed
Push — api/develop ( d18cde...f05f55 )
by Bertrand
08:06
created

BaseTransformer   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 46
ccs 0
cts 14
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B transformCollection() 0 25 3
1
<?php
2
3
/**
4
 * This file is part of the HRis Software package.
5
 *
6
 * HRis - Human Resource and Payroll System
7
 *
8
 * @link http://github.com/HB-Co/HRis
9
 */
10
namespace HRis\Api\Transformers;
11
12
use League\Fractal\TransformerAbstract;
13
14
class BaseTransformer extends TransformerAbstract
15
{
16
    /**
17
     * @var array
18
     */
19
    private $validParams = ['order'];
20
21
    /**
22
     * Base Collection transformer with params.
23
     *
24
     * @param $model
25
     * @param $transformer
26
     * @param $params
27
     *
28
     * @return \League\Fractal\Resource\Collection
29
     *
30
     * @throws \Exception
31
     *
32
     * @author Bertrand Kintanar <[email protected]>
33
     */
34
    public function transformCollection($model, $transformer, $params)
35
    {
36
        if ($params === null) {
37
            return $this->collection($model, $transformer);
38
        }
39
40
        // Optional params validation
41
        $usedParams = array_keys(iterator_to_array($params));
42
        if ($invalidParams = array_diff($usedParams, $this->validParams)) {
0 ignored issues
show
Unused Code introduced by
$invalidParams 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...
43
            throw new \Exception(sprintf(
44
                'Invalid param(s): "%s". Valid param(s): "%s"',
45
                implode(',', $usedParams),
46
                implode(',', $this->validParams)
47
            ));
48
        }
49
50
        // Processing
51
        list($orderCol, $orderBy) = $params->get('order');
52
53
        $model = $model
54
            ->orderBy($orderCol, $orderBy)
55
            ->get();
56
57
        return $this->collection($model, $transformer);
58
    }
59
}
60