Completed
Push — develop ( 8632b6...107510 )
by Alex
02:03
created

JsonApiController::transformRecord()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
nc 2
cc 2
eloc 8
nop 3
1
<?php
2
3
namespace Huntie\JsonApi\Http\Controllers;
4
5
use DB;
6
use Huntie\JsonApi\Support\JsonApiErrors;
7
use Huntie\JsonApi\Http\JsonApiResponse;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Http\Request;
10
use Illuminate\Routing\Controller;
11
12
abstract class JsonApiController extends Controller
13
{
14
    use JsonApiErrors;
15
16
    /**
17
     * Return the Eloquent Model for the resource.
18
     *
19
     * @return Model
20
     */
21
    abstract protected function getModel();
22
23
    /**
24
     * Return the type name of the resource.
25
     *
26
     * @return string
27
     */
28
    protected function getModelType()
29
    {
30
        return $this->getModel()->getTable();
31
    }
32
33
    /**
34
     * Return a listing of the resource.
35
     *
36
     * @param Request $request
37
     *
38
     * @return JsonApiResponse
39
     */
40
    public function indexAction(Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        $data = [];
43
        $records = $this->getModel()->all();
44
45
        foreach ($records as $record) {
46
            $data[] = $this->transformRecord($record);
47
        }
48
49
        return new JsonApiResponse(compact('data'));
50
    }
51
52
    /**
53
     * Return a specified record.
54
     *
55
     * @param Request   $request
56
     * @param Model|int $record
57
     *
58
     * @return JsonApiResponse
59
     */
60
    public function showAction(Request $request, $record)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
    {
62
        $record = $record instanceof Model ? $record : $this->findModelInstance($record);
63
        $data = $this->transformRecord($record);
0 ignored issues
show
Bug introduced by
It seems like $record defined by $record instanceof \Illu...dModelInstance($record) on line 62 can also be of type object<Huntie\JsonApi\Http\JsonApiResponse>; however, Huntie\JsonApi\Http\Cont...ller::transformRecord() does only seem to accept object<Illuminate\Database\Eloquent\Model>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
64
65
        return new JsonApiResponse(compact('data'));
66
    }
67
68
    /**
69
     * Return an instance of the resource by primary key.
70
     *
71
     * @param mixed $key
72
     *
73
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
74
     *
75
     * @return Model|JsonApiResponse
76
     */
77
    protected function findModelInstance($key)
78
    {
79
        return $this->getModel()->findOrFail($key);
80
    }
81
82
    /**
83
     * Transform a model instance into a JSON API object.
84
     *
85
     * @param Model      $record
86
     * @param array|null $include Relations to include
87
     * @param array|null $fields  Field names of attributes to limit to
88
     *
89
     * @return array
90
     */
91
    protected function transformRecord($record, array $include = [], array $fields = [])
0 ignored issues
show
Unused Code introduced by
The parameter $include is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
92
    {
93
        $attributes = $record->toArray();
94
95
        if ($fields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
96
            $attributes = array_only($attributes, $fields);
97
        }
98
99
        return [
100
            'type' => $record->getTable(),
101
            'id' => $record->id,
102
            'attributes' => array_except($attributes, ['id']),
103
        ];
104
    }
105
}
106