Completed
Pull Request — master (#62)
by Sebastian
13:50 queued 11:23
created

Graph::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Spatie\SchemaOrg;
4
5
use ReflectionClass;
6
use BadMethodCallException;
7
use InvalidArgumentException;
8
9
/**
10
 * @mixin \Spatie\SchemaOrg\Schema
11
 */
12 View Code Duplication
class Graph extends BaseType
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    /** @var array */
15
    protected $hidden = [];
16
17
    public function __call(string $method, array $arguments)
18
    {
19
        if (is_callable([Schema::class, $method])) {
20
            $type = (new ReflectionClass(Schema::class))->getMethod($method)->getReturnType();
21
22
            $schema = $this->getOrCreate($type);
23
24
            if (isset($arguments[0]) && is_callable($arguments[0])) {
25
                call_user_func($arguments[0], $schema, $this);
26
27
                return $this;
28
            }
29
30
            return $schema;
31
        }
32
33
        throw new BadMethodCallException(sprintf('The method "%" does not exist on class "%s".', $method, get_class($this)));
34
    }
35
36
    public function add(Type $schema): self
37
    {
38
        $type = get_class($schema);
39
        
40
        if ($this->has($type)) {
41
            throw new InvalidArgumentException(sprintf('The graph already has an item of type "%s".', $type));
42
        }
43
44
        return $this->set($schema);
45
    }
46
47
    public function has(string $type): bool
48
    {
49
        return $this->offsetExists($type);
50
    }
51
52
    public function set(Type $schema)
53
    {
54
        return $this->setProperty(get_class($schema), $schema);
55
    }
56
57
    public function get(string $type): Type
58
    {
59
        if (! $this->has($type)) {
60
            throw new InvalidArgumentException(sprintf('The graph does not have an item of type "%s".', $type));
61
        }
62
63
        return $this->getProperty($type);
64
    }
65
66
    public function getOrCreate(string $type): Type
67
    {
68
        if (! is_subclass_of($type, Type::class)) {
69
            throw new InvalidArgumentException(sprintf('The given type "%s" is not an instance of "%s".', $type, Type::class));
70
        }
71
72
        if (! $this->has($type)) {
73
            $this->set(new $type());
74
        }
75
76
        return $this->get($type);
77
    }
78
79
    public function hide(string $type): self
80
    {
81
        $this->hidden[$type] = true;
82
83
        return $this;
84
    }
85
86
    public function show(string $type): self
87
    {
88
        $this->hidden[$type] = false;
89
90
        return $this;
91
    }
92
93
    public function toArray(): array
94
    {
95
        $properties = $this->getProperties();
96
        
97
        foreach ($this->hidden as $type => $hide) {
98
            if ($hide) {
99
                unset($properties[$type]);
100
            }
101
        }
102
103
        return [
104
            '@context' => $this->getContext(),
105
            '@graph' => $this->serializeProperty(array_values($properties)),
106
        ];
107
    }
108
}
109