Response   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 1 Features 1
Metric Value
eloc 17
dl 0
loc 105
ccs 25
cts 25
cp 1
rs 10
c 4
b 1
f 1
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setResource() 0 3 1
A resource() 0 3 1
A toCollect() 0 8 2
A setCollection() 0 3 1
A collection() 0 6 1
A __construct() 0 4 1
A make() 0 7 2
A toResource() 0 3 1
1
<?php
2
3
4
namespace Hell\Vephar;
5
6
use Illuminate\Support\Arr;
7
8
/**
9
 * Class Response
10
 * @package Hell\Vephar
11
 */
12
class Response
13
{
14
15
    /**
16
     * @var
17
     */
18
    protected $resource;
19
20
    /**
21
     * @var
22
     */
23
    protected $collection;
24
25
26
    /**
27
     * Response constructor.
28
     * @param string $resource
29
     * @param string $collection
30
     */
31 21
    public function __construct(string $resource = Resource::class, string $collection = Collection::class)
32
    {
33 21
        $this->setResource($resource);
34 21
        $this->setCollection($collection);
35 21
    }
36
37
    /**
38
     * @param string $resource
39
     */
40 21
    public function setResource(string $resource): void
41
    {
42 21
        $this->resource = $resource;
43 21
    }
44
45
46
    /**
47
     * @param string $collection
48
     */
49 21
    public function setCollection(string $collection): void
50
    {
51 21
        $this->collection = $collection;
52 21
    }
53
54
55
    /**
56
     * @param array $data
57
     * @param string $resource
58
     * @param string $collection
59
     * @return mixed
60
     */
61 15
    public static function collection(
62
        $data = [],
63
        string $resource = Resource::class,
64
        string $collection = Collection::class
65
    ) {
66 15
        return (new Response($resource, $collection))->make($data);
67
    }
68
69
70
    /**
71
     * @param array $data
72
     * @return mixed
73
     */
74 9
    protected function toCollect($data = [])
75
    {
76 9
        $items = [];
77 9
        foreach ($data as $item) {
78 9
            $items[] = new $this->resource($item);
79
        }
80
81 9
        return new $this->collection($items);
82
    }
83
84
85
    /**
86
     * @param array $data
87
     * @param string $resource
88
     * @return mixed
89
     */
90 21
    public static function resource($data = [], string $resource = Resource::class)
91
    {
92 21
        return (new Response($resource))->make($data);
93
    }
94
95
96
    /**
97
     * @param $data
98
     * @return mixed
99
     */
100 21
    protected function toResource($data = [])
101
    {
102 21
        return new $this->resource($data);
103
    }
104
105
106
    /**
107
     * @param $data
108
     * @return mixed
109
     */
110 21
    public function make($data)
111
    {
112 21
        if (Arr::isAssoc((array)$data)) {
113 21
            return $this->toResource((array)$data);
114
        }
115
116 9
        return $this->toCollect($data);
117
    }
118
}
119