Completed
Push — master ( ad594c...e358da )
by Neomerx
02:28
created

RelationshipStorage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php namespace Limoncello\Models;
2
3
/**
4
 * Copyright 2015-2016 [email protected] (www.neomerx.com)
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\Models\Contracts\FactoryInterface;
20
use Limoncello\Models\Contracts\RelationshipStorageInterface;
21
22
/**
23
 * @package Limoncello\Models
24
 */
25
class RelationshipStorage implements RelationshipStorageInterface
26
{
27
    /** Internal data index */
28
    const IDX_DATA = 0;
29
30
    /** Internal data index */
31
    const IDX_TYPE = 1;
32
33
    /**
34
     * @var array
35
     */
36
    private $relationships;
37
38
    /** @var FactoryInterface */
39
    private $factory;
40
41
    /**
42
     * @param FactoryInterface $factory
43
     */
44 2
    public function __construct(FactoryInterface $factory)
45
    {
46 2
        $this->factory = $factory;
47 2
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52 2
    public function addToOneRelationship($model, $relationship, $one)
53
    {
54 2
        $uniqueId = spl_object_hash($model);
55 2
        if (isset($this->relationships[$uniqueId][$relationship]) === false) {
56 2
            $this->relationships[$uniqueId][$relationship] = [
57 2
                self::IDX_DATA => $this->factory->createPaginatedData($one),
58 2
                self::IDX_TYPE => self::RELATIONSHIP_TYPE_TO_ONE,
59
            ];
60
61 2
            return true;
62
        }
63
64 1
        return false;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 1
    public function addToManyRelationship($model, $relationship, array $many, $hasMore, $offset, $size)
71
    {
72 1
        $uniqueId = spl_object_hash($model);
73 1
        if (isset($this->relationships[$uniqueId][$relationship]) === false) {
74 1
            $this->relationships[$uniqueId][$relationship] = [
75 1
                self::IDX_DATA => $this->factory->createPaginatedData($many, true, $hasMore, $offset, $size),
76 1
                self::IDX_TYPE => self::RELATIONSHIP_TYPE_TO_MANY,
77
            ];
78
79 1
            return true;
80
        }
81
82 1
        return false;
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 2
    public function hasRelationship($model, $relationship)
89
    {
90 2
        return $this->getRelationshipType($model, $relationship) !== self::RELATIONSHIP_TYPE_NONE;
91
    }
92
93
    /**
94
     * @inheritdoc
95
     */
96 2
    public function getRelationshipType($model, $relationship)
97
    {
98 2
        $result = self::RELATIONSHIP_TYPE_NONE;
99
100 2
        $uniqueId = spl_object_hash($model);
101 2
        if (isset($this->relationships[$uniqueId][$relationship][self::IDX_TYPE]) === true) {
102 2
            $result = $this->relationships[$uniqueId][$relationship][self::IDX_TYPE];
103 2
        }
104
105 2
        return $result;
106
    }
107
108
    /**
109
     * @inheritdoc
110
     */
111 2
    public function getRelationship($model, $relationship)
112
    {
113 2
        $uniqueId = spl_object_hash($model);
114 2
        $result = $this->relationships[$uniqueId][$relationship][self::IDX_DATA];
115
116 2
        return $result;
117
    }
118
}
119