Completed
Push — master ( 1a67d2...dfab50 )
by Neomerx
11:35 queued 22s
created

JsonSchemas   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 103
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A hasRelationshipSchema() 0 15 3
A getRelationshipSchema() 0 14 2
A getModelRelationshipSchema() 0 9 1
A getModelSchemas() 0 4 1
A createSchemaFromCallable() 0 7 1
A createSchemaFromClassName() 0 6 1
1
<?php namespace Limoncello\Flute\Schema;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Contracts\Data\ModelSchemaInfoInterface;
20
use Limoncello\Flute\Contracts\Schema\JsonSchemasInterface;
21
use Limoncello\Flute\Contracts\Schema\SchemaInterface;
22
use Limoncello\Flute\Exceptions\InvalidSchemaFactoryException;
23
use Neomerx\JsonApi\Contracts\Schema\SchemaFactoryInterface;
24
use Neomerx\JsonApi\Contracts\Schema\SchemaInterface as JsonSchemaInterface;
25
use Neomerx\JsonApi\Schema\Container;
26
27
/**
28
 * @package Limoncello\Flute
29
 */
30
class JsonSchemas extends Container implements JsonSchemasInterface
31
{
32
    /**
33
     * @var ModelSchemaInfoInterface
34
     */
35
    private $modelSchemas;
36
37
    /**
38
     * @param SchemaFactoryInterface   $factory
39
     * @param array                    $schemas
40
     * @param ModelSchemaInfoInterface $modelSchemas
41
     */
42 48
    public function __construct(SchemaFactoryInterface $factory, array $schemas, ModelSchemaInfoInterface $modelSchemas)
43
    {
44 48
        parent::__construct($factory, $schemas);
45 48
        $this->modelSchemas = $modelSchemas;
46
    }
47
48
    /**
49
     * @inheritdoc
50
     */
51 5
    public function hasRelationshipSchema(string $schemaClass, string $relationshipName): bool
52
    {
53 5
        assert(
54 5
            class_exists($schemaClass) === true &&
55 5
            in_array(SchemaInterface::class, class_implements($schemaClass)) === true
56
        );
57
58
        /** @var SchemaInterface $schemaClass */
59
60 5
        $hasRel = $schemaClass::getMappings()[SchemaInterface::SCHEMA_RELATIONSHIPS][$relationshipName] ?? false;
61
62 5
        assert($hasRel === false || $this->getRelationshipSchema($schemaClass, $relationshipName) !== null);
0 ignored issues
show
Documentation introduced by
$schemaClass is of type object<Limoncello\Flute\...Schema\SchemaInterface>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
63
64 5
        return $hasRel !== false;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 10
    public function getRelationshipSchema(string $schemaClass, string $relationshipName): SchemaInterface
71
    {
72 10
        assert(
73 10
            class_exists($schemaClass) === true &&
74 10
            in_array(SchemaInterface::class, class_implements($schemaClass)) === true
75
        );
76
77
        /** @var SchemaInterface $schemaClass */
78
79 10
        $modelRelName = $schemaClass::getMappings()[SchemaInterface::SCHEMA_RELATIONSHIPS][$relationshipName];
80 10
        $targetSchema = $this->getModelRelationshipSchema($schemaClass::MODEL, $modelRelName);
81
82 10
        return $targetSchema;
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 10
    public function getModelRelationshipSchema(string $modelClass, string $relationshipName): SchemaInterface
89
    {
90 10
        $reverseModelClass = $this->getModelSchemas()->getReverseModelClass($modelClass, $relationshipName);
91
92
        /** @var SchemaInterface $targetSchema */
93 10
        $targetSchema = $this->getSchemaByType($reverseModelClass);
94
95 10
        return $targetSchema;
96
    }
97
98
    /**
99
     * @return ModelSchemaInfoInterface
100
     */
101 26
    protected function getModelSchemas(): ModelSchemaInfoInterface
102
    {
103 26
        return $this->modelSchemas;
104
    }
105
106
    /** @noinspection PhpMissingParentCallCommonInspection
107
     * @param callable $callable
108
     *
109
     * @codeCoverageIgnore
110
     *
111
     * @return SchemaInterface
112
     */
113
    protected function createSchemaFromCallable(callable $callable): JsonSchemaInterface
114
    {
115
        assert($callable);
116
117
        // callable as Schema factory is not supported.
118
        throw new InvalidSchemaFactoryException();
119
    }
120
121
    /** @noinspection PhpMissingParentCallCommonInspection
122
     * @param string $className
123
     *
124
     * @return SchemaInterface
125
     */
126 26
    protected function createSchemaFromClassName(string $className): JsonSchemaInterface
127
    {
128 26
        $schema = new $className($this->getFactory(), $this->getModelSchemas());
129
130 26
        return $schema;
131
    }
132
}
133