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

WrapsInResource::setResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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