Relationship::preSave()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2014-2018 James Ekow Abaka Ainooson
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace ntentan\nibii;
28
29
/**
30
 * Relationships provide queries for fetching data from related models when using lazy loading.
31
 */
32
abstract class Relationship
33
{
34
    /**
35
     * Constant for the belongs-to or one-to many relation ship.
36
     */
37
    const BELONGS_TO = 'BelongsTo';
38
39
    const HAS_MANY = 'HasMany';
40
    const MANY_HAVE_MANY = 'ManyHaveMany';
41
42
    protected $options = [];
43
    protected $type;
44
    protected $setupName;
45
    protected $setupTable;
46
    protected $setupSchema;
47
    protected $setupPrimaryKey;
48
49
    private $setup = false;
50
    private $query;
51
    protected $queryPrepared = false;
52
53 28
    public function setOptions($options)
54
    {
55 28
        $this->options = $options;
56 28
    }
57
58 12
    public function getQuery()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
59
    {
60 12
        if (!$this->query) {
61 12
            $this->query = new QueryParameters();
62
        }
63
64 12
        return $this->query;
65
    }
66
67
    public function getOptions()
68
    {
69
        return $this->options;
70
    }
71
72
    /**
73
     * Gets an instance of the related model accessed through this class.
74
     *
75
     * @return RecordWrapper
76
     */
77 12
    public function getModelInstance()
78
    {
79 12
        if (!$this->setup) {
80 12
            $this->runSetup();
81 12
            $this->setup = true;
82
        }
83
84 12
        return ORMContext::getInstance()->getModelFactory()->createModel($this->options['model'], $this->type);
85
    }
86
87 28
    public function setup($name, $schema, $table, $primaryKey)
88
    {
89 28
        $this->setupName = $name;
90 28
        $this->setupTable = $table;
91 28
        $this->setupPrimaryKey = $primaryKey;
92 28
        $this->setupSchema = $schema;
93 28
    }
94
95
    public function preSave(&$wrapper, $value)
96
    {
97
    }
98
99
    public function postSave(&$wrapper)
100
    {
101
    }
102
103
    abstract public function prepareQuery($data);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
104
105
    abstract public function runSetup();
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
106
}
107