Resource   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 3
b 0
f 0
dl 0
loc 39
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 19 6
A __set() 0 3 1
A __unset() 0 3 1
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