Transformer::resource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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
 *
22
 * @property string $id
23
 * @property string $username
24
 * @property string $email
25
 * @property string $name
26
 * @property string $avatar
27
 * @property string $provider
28
 */
29
abstract class Transformer extends JsonResource implements Transformable
30
{
31
    use IncludesRelations, HandlesLimit;
32
33
    public $include = [];
34
35
    public $available = [];
36
37
    public $limit = -1;
38
39
    public function __construct($resource, $relations = [])
40
    {
41
        if (! ($resource instanceof Model)) {
42
            throw new Exception('Object passed to the transformer resource method is not a eloquent model', 500);
43
        }
44
        $this->resource = $resource;
45
        $relations = is_array($relations) ? $relations : [];
46
        parent::__construct(self::loadRelations($resource, $relations));
47
    }
48
49
    public static function resource($model, array $relations = []): self
50
    {
51
        return new static($model, $relations);
52
    }
53
54
    public static function collection($resource, array $relations = [])
55
    {
56
        if (! ($resource instanceof Collection)) {
57
            throw new Exception('Object passed to the transformer collection method is not a collection', 500);
58
        }
59
        $resource = self::processLimit($resource);
60
        $resource = self::loadRelations($resource, $relations);
61
62
        return new AnonymousTransformerCollection($resource, static::class);
63
    }
64
65
    public function serialize()
66
    {
67
        return json_decode(json_encode($this->jsonSerialize()), true);
68
    }
69
70
    public function toArray($request)
71
    {
72
        if (! method_exists($this, 'transformResource')) {
73
            throw new \Exception('transformResource method not set on '.static::class, 500);
74
        }
75
76
        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

76
        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...
77
    }
78
}
79