Passed
Push — master ( 802ddf...d5fac0 )
by Arthur
13:53
created

Transformer::collection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 29.10.18
6
 * Time: 09:35.
7
 */
8
9
namespace Foundation\Abstracts\Transformers;
10
11
use Foundation\Contracts\Transformable;
12
use Foundation\Exceptions\Exception;
13
use Illuminate\Database\Eloquent\Collection;
14
use Illuminate\Database\Eloquent\Model;
15
use Illuminate\Http\Resources\Json\JsonResource;
16
17
/**
18
 * Class Transformer.
19
 *
20
 * @method  array  transformResource
21
 * @property string $id
22
 * @property string $username
23
 * @property string $email
24
 * @property string $name
25
 * @property string $avatar
26
 * @property string $provider
27
 */
28
abstract class Transformer extends JsonResource implements Transformable
29
{
30
    use IncludesRelations, HandlesLimit;
31
32
    public $include = [];
33
34
    public $available = [];
35
36
    public $limit = -1;
37
38 21
    public function __construct($resource, $relations = [])
39
    {
40 21
        if (!($resource instanceof Model))
41
            throw new Exception('Object passed to the transformer resource method is not a eloquent model', 500);
42 21
        $this->resource = $resource;
43 21
        $relations = is_array($relations) ? $relations : [];
44 21
        parent::__construct(self::loadRelations($resource, $relations));
45 21
    }
46
47 13
    public static function resource($model, array $relations = []): self
48
    {
49 13
        return new static($model, $relations);
50
    }
51
52 10
    public static function collection($resource, array $relations = [])
53
    {
54 10
        if (!($resource instanceof Collection))
55
            throw new Exception('Object passed to the transformer collection method is not a collection', 500);
56
57 10
        $resource = self::processLimit($resource);
58 10
        $resource = self::loadRelations($resource, $relations);
59 10
        return new AnonymousTransformerCollection($resource, static::class);
60
    }
61
62
63 4
    public function serialize()
64
    {
65 4
        return json_decode(json_encode($this->jsonSerialize()), true);
66
    }
67
68 20
    public function toArray($request)
69
    {
70 20
        if (!method_exists($this, 'transformResource'))
71
            throw new \Exception("transformResource method not set on " . static::class, 500);
72 20
        return array_merge($this->transformResource($this->resource), $this->includeRelations());
0 ignored issues
show
Unused Code introduced by
The call to Foundation\Abstracts\Tra...er::transformResource() has too many arguments starting with $this->resource. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        return array_merge($this->/** @scrutinizer ignore-call */ transformResource($this->resource), $this->includeRelations());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
73
    }
74
}
75