Resource   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 27
eloc 50
c 1
b 0
f 0
dl 0
loc 148
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtensions() 0 3 1
B addExtension() 0 17 7
A getId() 0 3 1
A getMeta() 0 3 1
B deserializeCommonAttributes() 0 19 8
A __construct() 0 5 1
A getSchemas() 0 3 1
A getExternalId() 0 3 1
A serializeObject() 0 24 6
1
<?php
2
3
/*
4
 * This file is part of the tmilos/scim-schema package.
5
 *
6
 * (c) Milos Tomic <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Tmilos\ScimSchema\Model;
13
14
abstract class Resource extends SchemaBase implements SerializableInterface, DeserializableInterface
15
{
16
    /** @var string */
17
    protected $id;
18
19
    /** @var string */
20
    protected $schemaId;
21
22
    /** @var string */
23
    protected $externalId;
24
25
    /** @var Meta */
26
    protected $meta;
27
28
    /** @var array|ResourceExtension[] schemaId => extension */
29
    private $extensions = [];
30
31
    /**
32
     * @param string $id
33
     */
34
    public function __construct($id)
35
    {
36
        $this->id = $id;
37
        $this->schemaId = $this->getSchemaId();
38
        $this->meta = new Meta($this->getResourceType());
39
    }
40
41
    /**
42
     * @return string
43
     */
44
    abstract public function getResourceType();
45
46
    /**
47
     * @return \string[]
48
     */
49
    public function getSchemas()
50
    {
51
        return array_merge([static::getSchemaId()], array_keys($this->extensions));
0 ignored issues
show
Bug Best Practice introduced by
The method Tmilos\ScimSchema\Model\SchemaBase::getSchemaId() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        return array_merge([static::/** @scrutinizer ignore-call */ getSchemaId()], array_keys($this->extensions));
Loading history...
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getId()
58
    {
59
        return $this->id;
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    public function getExternalId()
66
    {
67
        return $this->externalId;
68
    }
69
70
    /**
71
     * @return Meta
72
     */
73
    public function getMeta()
74
    {
75
        return $this->meta;
76
    }
77
78
    /**
79
     * @return array|ResourceExtension[] schemaId => extension
80
     */
81
    public function getExtensions()
82
    {
83
        return $this->extensions;
84
    }
85
86
    /**
87
     * @param array|ResourceExtension $extension
88
     * @param string                  $schemaId
89
     */
90
    public function addExtension($extension, $schemaId = null)
91
    {
92
        if (!($extension instanceof ResourceExtension || is_array($extension))) {
0 ignored issues
show
introduced by
The condition is_array($extension) is always true.
Loading history...
93
            throw new \InvalidArgumentException('Resource extension must extend ResourceExtension or be an array');
94
        }
95
96
        if ($extension instanceof ResourceExtension) {
97
            $schemaId = $extension->getSchemaId();
98
        } elseif (is_array($extension) && !$schemaId) {
99
            throw new \InvalidArgumentException('SchemaId must be specified if extension is added as array');
100
        }
101
102
        if ($schemaId === static::getSchemaId()) {
0 ignored issues
show
Bug Best Practice introduced by
The method Tmilos\ScimSchema\Model\SchemaBase::getSchemaId() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

102
        if ($schemaId === static::/** @scrutinizer ignore-call */ getSchemaId()) {
Loading history...
103
            throw new \InvalidArgumentException(sprintf('Resource already has such main schema "%s"', $schemaId));
104
        }
105
106
        $this->extensions[$schemaId] = $extension;
107
    }
108
109
    /**
110
     * @return array
111
     */
112
    public function serializeObject()
113
    {
114
        $result = [
115
            'schemas' => $this->getSchemas(),
116
            'id' => $this->getId(),
117
        ];
118
        if ($this->getExternalId()) {
119
            $result['externalId'] = $this->getExternalId();
120
        }
121
        $result['meta'] = $this->getMeta()->serializeObject();
122
        foreach ($this->extensions as $schemaId => $extension) {
123
            if (!$extension) {
124
                continue;
125
            }
126
            if ($extension instanceof ResourceExtension) {
127
                $result[$schemaId] = $extension->serializeObject();
0 ignored issues
show
Bug introduced by
The method serializeObject() does not exist on Tmilos\ScimSchema\Model\ResourceExtension. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
                /** @scrutinizer ignore-call */ 
128
                $result[$schemaId] = $extension->serializeObject();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
            } elseif (is_array($extension)) {
129
                $result[$schemaId] = $extension;
130
            } else {
131
                throw new \InvalidArgumentException('Resource extension must extend ResourceExtension or be an array');
132
            }
133
        }
134
135
        return $result;
136
    }
137
138
    /**
139
     * @param array $data
140
     *
141
     * @return static
142
     */
143
    protected static function deserializeCommonAttributes(array $data)
144
    {
145
        $result = new static(isset($data['id']) ? $data['id'] : null);
146
        if ($result->getResourceType() != $data['meta']['resourceType']) {
147
            throw new \RuntimeException(sprintf('Error deserializing resource type "%s" into class "%s"', $data['meta']['resourceType'], get_class($result)));
148
        }
149
        $result->meta = Meta::deserializeObject($data['meta']);
150
        if (isset($data['externalId'])) {
151
            $result->externalId = $data['externalId'];
152
        }
153
        if (isset($data['schemas'])) {
154
            foreach ($data['schemas'] as $schemaId) {
155
                if ($schemaId != $result->getSchemaId()) {
156
                    $result->extensions[$schemaId] = isset($data[$schemaId]) ? $data[$schemaId] : null;
157
                }
158
            }
159
        }
160
161
        return $result;
162
    }
163
}
164