Completed
Branch master (9645b9)
by Neomerx
02:19
created

BaseRelationshipData   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 61
c 0
b 0
f 0
ccs 15
cts 15
cp 1
rs 10
wmc 3
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A createParsedResource() 0 13 1
A createParsedIdentifier() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Neomerx\JsonApi\Parser\RelationshipData;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
22
use Neomerx\JsonApi\Contracts\Parser\IdentifierInterface as ParserIdentifierInterface;
23
use Neomerx\JsonApi\Contracts\Parser\PositionInterface;
24
use Neomerx\JsonApi\Contracts\Parser\RelationshipDataInterface;
25
use Neomerx\JsonApi\Contracts\Parser\ResourceInterface;
26
use Neomerx\JsonApi\Contracts\Schema\IdentifierInterface as SchemaIdentifierInterface;
27
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
28
29
/**
30
 * @package Neomerx\JsonApi
31
 */
32
abstract class BaseRelationshipData implements RelationshipDataInterface
33
{
34
    /**
35
     * @var FactoryInterface
36
     */
37
    private $factory;
38
39
    /**
40
     * @var SchemaContainerInterface
41
     */
42
    private $schemaContainer;
43
44
    /**
45
     * @var PositionInterface
46
     */
47
    private $position;
48
49
    /**
50
     * @param FactoryInterface         $factory
51
     * @param SchemaContainerInterface $schemaContainer
52
     * @param PositionInterface        $position
53
     */
54 37
    public function __construct(
55
        FactoryInterface $factory,
56
        SchemaContainerInterface $schemaContainer,
57
        PositionInterface $position
58
    ) {
59 37
        $this->factory         = $factory;
60 37
        $this->schemaContainer = $schemaContainer;
61 37
        $this->position        = $position;
62 37
    }
63
64
    /**
65
     * @param mixed $resource
66
     *
67
     * @return ResourceInterface
68
     */
69 29
    protected function createParsedResource($resource): ResourceInterface
70
    {
71 29
        assert(
72 29
            $this->schemaContainer->hasSchema($resource),
73 29
            'No Schema found for resource `' . get_class($resource) . '`.'
74
        );
75
76 29
        return $this->factory->createParsedResource(
77 29
            $this->position,
78 29
            $this->schemaContainer,
79 29
            $resource
80
        );
81
    }
82
83
    /**
84
     * @param SchemaIdentifierInterface $identifier
85
     *
86
     * @return ResourceInterface
87
     */
88 1
    protected function createParsedIdentifier(SchemaIdentifierInterface $identifier): ParserIdentifierInterface
89
    {
90 1
        return $this->factory->createParsedIdentifier($this->position, $identifier);
91
    }
92
}
93