Completed
Push — master ( e791a2...805c07 )
by Michael
01:05
created

WrapsInResource::wrapInResource()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 4
nop 1
1
<?php
2
3
namespace Mblarsen\LaravelRepository\Traits;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Model;
7
8
trait WrapsInResource
9
{
10
    public function setResource(string $resource, string $resource_collection = null)
11
    {
12
        $this->resource = $resource;
0 ignored issues
show
Bug introduced by
The property resource does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
13
        $this->resource_collection = $resource_collection;
0 ignored issues
show
Bug introduced by
The property resource_collection does not seem to exist. Did you mean resource?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
14
    }
15
16
    public function wrapInResource($value)
17
    {
18
        if (!$this->resource) {
19
            throw new Exception("You must first set a resource class");
20
        }
21
22
        if ($value instanceof Model) {
23
            $resource_class = $this->resource;
24
            return $resource_class::make($value);
25
        }
26
27
        if ($this->resource_collection) {
0 ignored issues
show
Bug introduced by
The property resource_collection does not seem to exist. Did you mean resource?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
28
            $resource_class = $this->resource_collection;
0 ignored issues
show
Bug introduced by
The property resource_collection does not seem to exist. Did you mean resource?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
29
            return $resource_class::make($value);
30
        }
31
32
        $resource_class = $this->resource;
33
34
        return $resource_class::collection($value);
35
    }
36
}
37