Resource::create()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 14
c 2
b 0
f 0
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.2222
cc 6
nc 9
nop 4
crap 6
1
<?php
2
namespace Akkroo;
3
4
use InvalidArgumentException;
5
6
class Resource extends Result
7
{
8
    /**
9
     * Create a resource object
10
     * @param  string $resourceName Name of the resource (i.e. events, records)
11
     * @param  array  $data         Recource data
12
     * @param  array  $params       Resource parameters
13
     * @param  array  $meta         Resource metadata
14
     * @return Event|Record|Collection
15
     */
16 13
    public static function create($resourceName, $data, $params = [], $meta = [])
17
    {
18
        switch ($resourceName) {
19 13
            case 'events':
20 8
                $resourceClass = Event::class;
21 8
                $createCollection = empty($params['id']) && empty($data['id']);
22 8
                break;
23 5
            case 'records':
24 4
                $resourceClass = Record::class;
25 4
                $createCollection = empty($params['id']) && empty($data['id']);
26 4
                break;
27
            default:
28 1
                throw new InvalidArgumentException(sprintf('Unknown resource "%s"', $resourceName));
29
        }
30 12
        if ($createCollection) {
31
            // We have a collection
32 5
            return (new Collection($data, $resourceClass))->withMeta($meta);
33
        }
34 7
        return new $resourceClass($data);
35
    }
36
37 1
    public function __set(string $name, $value)
38
    {
39 1
        $this->data[$name] = $value;
40 1
    }
41
42 1
    public function __unset(string $name)
43
    {
44 1
        unset($this->data[$name]);
45 1
    }
46
}
47