Completed
Push — master ( 5e61a0...2e1bfd )
by Michael
04:48 queued 02:16
created

WrapsInResource   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setResource() 0 4 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 3
    public function setResource(string $resource, string $resource_collection = null)
17
    {
18 3
        $this->resource = $resource;
19 3
        $this->resource_collection = $resource_collection;
20 3
    }
21
22 5
    private function wrapInResource($value)
23
    {
24 5
        if (!$this->resource) {
25 1
            throw new Exception("You must first set a resource class");
26
        }
27
28 4
        if ($value instanceof Model) {
29 1
            $resource_class = $this->resource;
30 1
            return $resource_class::make($value);
31
        }
32
33 3
        if ($this->resource_collection) {
34 1
            $resource_class = $this->resource_collection;
35 1
            return $resource_class::make($value);
36
        }
37
38 2
        $resource_class = $this->resource;
39
40 2
        return $resource_class::collection($value);
41
    }
42
}
43