Completed
Push — master ( 6762cf...75869c )
by James Ekow Abaka
01:37
created

BelongsToRelationship   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 13.33 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 73.08%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 6
loc 45
c 2
b 0
f 0
ccs 19
cts 26
cp 0.7308
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A prepareQuery() 6 19 4
A runSetup() 0 11 3
A preSave() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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