Completed
Push — dev ( 5275ef...f292cf )
by James Ekow Abaka
01:48
created

BelongsToRelationship::postSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 1
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\relationships;
28
29
use ntentan\nibii\exceptions\FieldNotFoundException;
30
use ntentan\nibii\ORMContext;
31
use ntentan\nibii\Relationship;
32
use ntentan\utils\Text;
33
34
/**
35
 * Represents a one to many belongs to relationship.
36
 */
37
class BelongsToRelationship extends Relationship
38
{
39
    protected $type = self::BELONGS_TO;
40
    private $relatedData;
41
42 2
    public function prepareQuery($data)
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...
43
    {
44 2
        if (!array_key_exists($this->options['local_key'], $data)) {
45
            throw new FieldNotFoundException("Field {$this->options['local_key']} not found for belongs to relationship query.");
46
        }
47 2
        if (!isset($data[$this->options['local_key']])) {
48
            return;
49
        }
50 2
        $query = $this->getQuery();
51 2 View Code Duplication
        if ($this->queryPrepared) {
52 2
            $query->setBoundData($this->options['foreign_key'], $data[$this->options['local_key']]);
53
        } else {
54 2
            $query->setTable($this->getModelInstance()->getDBStoreInformation()['quoted_table'])
55 2
                ->addFilter($this->options['foreign_key'], $data[$this->options['local_key']])
56 2
                ->setFirstOnly(true);
57 2
            $this->queryPrepared = true;
58
        }
59
60 2
        return $query;
61
    }
62
63 2
    public function runSetup()
64
    {
65 2
        $model = ORMContext::getInstance()->getModelFactory()->createModel($this->options['model'], self::BELONGS_TO);
66 2
        $table = $model->getDBStoreInformation()['table'];
67 2
        if ($this->options['foreign_key'] == null) {
68 2
            $this->options['foreign_key'] = $model->getDescription()->getPrimaryKey()[0];
69
        }
70 2
        if ($this->options['local_key'] == null) {
71 2
            $this->options['local_key'] = Text::singularize($table).'_id';
72
        }
73 2
    }
74
75
    public function preSave(&$wrapper, $value)
76
    {
77
        $value->save();
78
        $wrapper[$this->options['local_key']] = $value[$this->options['foreign_key']];
79
        unset($wrapper[$this->options['model']]);
80
    }
81
82
    public function postSave(&$wrapper)
83
    {
84
        $wrapper[$this->options['model']] = $this->relatedData;
85
    }
86
}
87