Completed
Push — feature/EVO-9770-doc-version-c... ( 9971a0...81e34e )
by Narcotic
08:19
created

SchemaModel::isVersioning()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
ccs 0
cts 6
cp 0
cc 2
eloc 5
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * Model based on Graviton\RestBundle\Model\DocumentModel.
4
 */
5
6
namespace Graviton\SchemaBundle\Model;
7
8
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * Model based on Graviton\RestBundle\Model\DocumentModel.
13
 *
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
16
 * @link     http://swisscom.ch
17
 */
18
class SchemaModel implements ContainerAwareInterface
19
{
20
    /**
21
     * object
22
     */
23
    private $schema;
24
25
    /**
26
     * @var ContainerInterface
27
     */
28
    private $container;
29
30
    /**
31
     * load some schema info for the model
32
     */
33 4
    public function __construct()
34
    {
35 4
        list(, $bundle, , $model) = explode('\\', get_called_class());
36 4
        $file = __DIR__ . '/../../' . $bundle . '/Resources/config/schema/' . $model . '.json';
37
38 4
        if (!file_exists($file)) {
39
            $reflection = new \ReflectionClass($this);
40
            $file = dirname($reflection->getFileName()).'/../Resources/config/schema/' . $model . '.json';
41
        }
42
43 4
        if (!file_exists($file)) {
44
            // fallback try on model property (this should be available on some generated classes)
45
            if (isset($this->_modelPath)) {
46
                // try to find schema.json relative to the model involved..
47
                $file = dirname($this->_modelPath) . '/../Resources/config/schema/' . $model . '.json';
48
            }
49
50
            if (!file_exists($file)) {
51
                throw new \LogicException('Please create the schema file ' . $file);
52
            }
53
        }
54
55 4
        $this->schema = \json_decode(file_get_contents($file));
56
57 4
        if (is_null($this->schema)) {
58
            throw new \LogicException('The file ' . $file . ' doe not contain valid json');
59
        }
60 4
    }
61
62
    /**
63
     * inject container
64
     *
65
     * @param ContainerInterface $container service container
0 ignored issues
show
Documentation introduced by
Should the type for parameter $container not be null|ContainerInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
66
     *
67
     * @return self
68
     */
69 4
    public function setContainer(ContainerInterface $container = null)
70
    {
71 4
        $this->container = $container;
72
73 4
        return $this;
74
    }
75
76
    /**
77
     * get Title
78
     *
79
     * @return string
80
     */
81
    public function getTitle()
82
    {
83
        return $this->schema->title;
84
    }
85
86
    /**
87
     * get description
88
     *
89
     * @return string Description
90
     */
91
    public function getDescription()
92
    {
93
        return $this->schema->description;
94
    }
95
96
    /**
97
     * get recordOriginModifiable
98
     *
99
     * @return bool
100
     */
101
    public function getRecordOriginModifiable()
0 ignored issues
show
Coding Style introduced by
function getRecordOriginModifiable() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
102
    {
103
        if (isset($this->schema->recordOriginModifiable)) {
104
            return $this->schema->recordOriginModifiable;
105
        }
106
        return true;
107
    }
108
109
    /**
110
     * get isVersioning
111
     *
112
     * @return bool
113
     */
114
    public function isVersioning()
115
    {
116
        $isVersioning = false;
117
        if (isset($this->schema->{'x-versioning'})) {
118
            $isVersioning = $this->schema->{'x-versioning'};
119
        }
120
        return $isVersioning;
121
    }
122
123
    /**
124
     * Returns the bare schema
125
     *
126
     * @return \stdClass Schema
127
     */
128
    public function getSchema()
129
    {
130
        return $this->schema;
131
    }
132
133
    /**
134
     * get title for a given field
135
     *
136
     * @param string $field field name
137
     *
138
     * @return string
139
     */
140
    public function getTitleOfField($field)
141
    {
142
        return $this->getSchemaField($field, 'title');
143
    }
144
145
    /**
146
     * get description for a given field
147
     *
148
     * @param string $field field name
149
     *
150
     * @return string
151
     */
152
    public function getDescriptionOfField($field)
153
    {
154
        return $this->getSchemaField($field, 'description');
155
    }
156
157
    /**
158
     * get property model for embedded field
159
     *
160
     * @param string $mapping name of mapping class
161
     *
162
     * @return $this
163
     */
164
    public function manyPropertyModelForTarget($mapping)
165
    {
166
        // @todo refactor to get rid of container dependency (maybe remove from here)
167
        list($app, $bundle, , $document) = explode('\\', $mapping);
168
        $app = strtolower($app);
169
        $bundle = strtolower(substr($bundle, 0, -6));
170
        $document = strtolower($document);
171
        $propertyService = implode('.', array($app, $bundle, 'model', $document));
172
        $propertyModel = $this->container->get($propertyService);
173
174
        return $propertyModel;
175
    }
176
177
    /**
178
     * get required fields for this object
179
     *
180
     * @return string[]
181
     */
182
    public function getRequiredFields()
183
    {
184
        return $this->schema->required;
185
    }
186
187
    /**
188
     * get a collection of service names that can extref refer to
189
     *
190
     * @param string $field field name
191
     *
192
     * @return array
193
     */
194
    public function getRefCollectionOfField($field)
195
    {
196
        return $this->getSchemaField($field, 'collection', array());
197
    }
198
199
    /**
200
     * get readOnly flag for a given field
201
     *
202
     * @param string $field field name
203
     *
204
     * @return boolean the readOnly flag
205
     */
206
    public function getReadOnlyOfField($field)
0 ignored issues
show
Coding Style introduced by
function getReadOnlyOfField() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
207
    {
208
        return $this->getSchemaField($field, 'readOnly', false);
209
    }
210
211
    /**
212
     * get readOnly flag for a given field
213
     *
214
     * @param string $field field name
215
     *
216
     * @return boolean the readOnly flag
217
     */
218
    public function getRecordOriginExceptionOfField($field)
0 ignored issues
show
Coding Style introduced by
function getRecordOriginExceptionOfField() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
219
    {
220
        return $this->getSchemaField($field, 'recordOriginException', false);
221
    }
222
223
    /**
224
     * get searchable flag for a given field, weight based.
225
     *
226
     * @param string $field field name
227
     *
228
     * @return integer the searchable flag
229
     */
230
    public function getSearchableOfField($field)
231
    {
232
        return (int) $this->getSchemaField($field, 'searchable', 0);
233
    }
234
235
    /**
236
     * tell us if a model what to be exposed using a key as field
237
     *
238
     * @param string $field field that we check for dynamic-key spec
239
     *
240
     * @return boolean
241
     */
242
    public function hasDynamicKey($field)
243
    {
244
        return $this->getSchemaField($field, 'x-dynamic-key', false) !== false;
245
    }
246
247
    /**
248
     * Get field used for setting stringy key value
249
     *
250
     * @param string $field field that we get dynamic-key spec from
251
     *
252
     * @return object
253
     */
254
    public function getDynamicKeySpec($field)
255
    {
256
        return $this->getSchemaField($field, 'x-dynamic-key');
257
    }
258
259
    /**
260
     * Gets the defined document class in shortform from schema
261
     *
262
     * @return string|false either the document class or false it not given
263
     */
264
    public function getDocumentClass()
265
    {
266
        $documentClass = false;
267
        if (isset($this->schema->{'x-documentClass'})) {
268
            $documentClass = $this->schema->{'x-documentClass'};
269
        }
270
        return $documentClass;
271
    }
272
273
    /**
274
     * Get defined constraints on this field (if any)
275
     *
276
     * @param string $field field that we get constraints spec from
277
     *
278
     * @return object
279
     */
280
    public function getConstraints($field)
281
    {
282
        return $this->getSchemaField($field, 'x-constraints', false);
283
    }
284
285
    /**
286
     * Tells us if in this model, the ID can be given on a POST request or not (in the payload).
287
     * This basically depends on if the "id" property is given in the JSON definition or not.
288
     *
289
     * @return bool true if yes, false otherwise
290
     */
291
    public function isIdInPostAllowed()
292
    {
293
        $isAllowed = true;
294
        if (isset($this->schema->{'x-id-in-post-allowed'})) {
295
            $isAllowed = $this->schema->{'x-id-in-post-allowed'};
296
        }
297
        return $isAllowed;
298
    }
299
300
    /**
301
     * get schema field value
302
     *
303
     * @param string $field         field name
304
     * @param string $property      property name
305
     * @param mixed  $fallbackValue fallback value if property isn't set
306
     *
307
     * @return mixed
308
     */
309
    private function getSchemaField($field, $property, $fallbackValue = '')
310
    {
311
        if (isset($this->schema->properties->$field->$property)) {
312
            $fallbackValue = $this->schema->properties->$field->$property;
313
        }
314
315
        return $fallbackValue;
316
    }
317
318
    /**
319
     * get searchable fields for this object
320
     *
321
     * @return string[]
322
     */
323
    public function getSearchableFields()
324
    {
325
        return $this->schema->searchable;
326
    }
327
}
328