Completed
Pull Request — master (#62)
by Tom
14:39
created

Graph::getOrCreate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Spatie\SchemaOrg;
4
5
use ReflectionClass;
6
use BadMethodCallException;
7
use InvalidArgumentException;
8
9
/**
10
 * @mixin 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)
37
    {
38
        $type = get_class($schema);
39
        if ($this->has($type)) {
40
            throw new InvalidArgumentException(sprintf('The graph already has an item of type "%s".', $type));
41
        }
42
43
        return $this->set($schema);
44
    }
45
46
    public function has(string $type): bool
47
    {
48
        return $this->offsetExists($type);
49
    }
50
51
    public function set(Type $schema)
52
    {
53
        return $this->setProperty(get_class($schema), $schema);
54
    }
55
56
    public function get(string $type): Type
57
    {
58
        if (! $this->has($type)) {
59
            throw new InvalidArgumentException(sprintf('The graph does not have an item of type "%s".', $type));
60
        }
61
62
        return $this->getProperty($type);
63
    }
64
65
    public function getOrCreate(string $type): Type
66
    {
67
        if (!is_subclass_of($type, Type::class)) {
68
            throw new InvalidArgumentException(sprintf('The given type "%s" is not an instance of "%s".', $type, Type::class));
69
        }
70
71
        if (!$this->has($type)) {
72
            $this->set(new $type());
73
        }
74
75
        return $this->get($type);
76
    }
77
78
    public function hide(string $type)
79
    {
80
        $this->hidden[$type] = true;
81
82
        return $this;
83
    }
84
85
    public function show(string $type)
86
    {
87
        $this->hidden[$type] = false;
88
89
        return $this;
90
    }
91
92
    public function toArray(): array
93
    {
94
        $properties = $this->getProperties();
95
        foreach ($this->hidden as $type => $hide) {
96
            if ($hide) {
97
                unset($properties[$type]);
98
            }
99
        }
100
101
        return [
102
            '@context' => $this->getContext(),
103
            '@graph' => $this->serializeProperty(array_values($properties)),
104
        ];
105
    }
106
}
107