WrapsInResource::wrapInResource()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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