WrapsInResource   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 16
c 3
b 0
f 1
dl 0
loc 38
ccs 15
cts 15
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setResource() 0 6 1
A wrapInResource() 0 19 4
1
<?php
2
3
namespace Mblarsen\LaravelRepository\Traits;
4
5
use Exception;
6
use Illuminate\Database\Eloquent\Model;
7
8
trait WrapsInResource
9
{
10
    /** @var string */
11
    protected $resource;
12
13
    /** @var string */
14
    protected $resource_collection;
15
16
    /**
17
     * @return $this
18
     */
19 3
    public function setResource(string $resource, string $resource_collection = null)
20
    {
21 3
        $this->resource = $resource;
22 3
        $this->resource_collection = $resource_collection;
23
24 3
        return $this;
25
    }
26
27 5
    private function wrapInResource($value)
28
    {
29 5
        if (!$this->resource) {
30 1
            throw new Exception("You must first set a resource class");
31
        }
32
33 4
        if ($value instanceof Model) {
34 1
            $resource_class = $this->resource;
35 1
            return $resource_class::make($value);
36
        }
37
38 3
        if ($this->resource_collection) {
39 1
            $resource_class = $this->resource_collection;
40 1
            return $resource_class::make($value);
41
        }
42
43 2
        $resource_class = $this->resource;
44
45 2
        return $resource_class::collection($value);
46
    }
47
}
48