Completed
Push — master ( 145f77...f73370 )
by James Ekow Abaka
04:01
created

ManyHaveManyRelationship::runSetup()   B

Complexity

Conditions 6
Paths 32

Size

Total Lines 28
Code Lines 17

Duplication

Lines 11
Ratio 39.29 %

Code Coverage

Tests 16
CRAP Score 6.0073

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 28
ccs 16
cts 17
cp 0.9412
rs 8.439
cc 6
eloc 17
nc 32
nop 0
crap 6.0073
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\Nibii;
30
use ntentan\utils\Text;
31
use ntentan\panie\InjectionContainer;
32
33
class ManyHaveManyRelationship extends \ntentan\nibii\Relationship
34
{
35
    protected $type = self::MANY_HAVE_MANY;
36
    
37 4
    private function getJunctionModel()
38
    {
39 4
        return InjectionContainer::resolve($this->options['junction_model']);
40
    }
41
    
42 4
    public function getQuery($data)
43
    {
44 4
        $junctionModel = $this->getJunctionModel();
45 4
        $filter = $junctionModel->fields($this->options['junction_foreign_key'])
46 4
            ->filterBy($this->options['junction_local_key'], $data[$this->options['local_key']])
47 4
            ->fetch();
48 4
        $foreignKeys = [];
49 4
        foreach($filter->toArray() as $foreignItem) {
50 4
            $foreignKeys[] = $foreignItem[$this->options['junction_foreign_key']];
51
        }
52 4
        $query = (new \ntentan\nibii\QueryParameters($this->getModelInstance()))
53 4
            ->addFilter($this->options['foreign_key'], $foreignKeys);
54 4
        return $query;
55
    }
56
57 4
    public function runSetup()
58
    {
59 4
        if(isset($this->options['through'])) {
60
            $junctionModelName = $this->options['through'];
61
        } else {
62 4
            $junctionModelName = Nibii::joinModels($this->setupName, $this->options['model']);
63
        }
64 4
        $this->options['junction_model'] = $junctionModelName;
65
        
66 4
        $foreignModel = Nibii::load($this->options['model']);
67 4
        if($this->options['foreign_key'] == null) {
68 4
            $this->options['foreign_key'] = $foreignModel->getDescription()->getPrimaryKey()[0];
69
        } 
70
        
71 4 View Code Duplication
        if($this->options['local_key'] == null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72 4
            $this->options['local_key'] = $this->setupPrimaryKey[0];
73
        }
74
        
75 4 View Code Duplication
        if(!isset($this->options['junction_local_key'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76 4
            $this->options['junction_local_key'] = 
77 4
                Text::singularize($this->setupTable) . '_id';
78
        }
79
        
80 4 View Code Duplication
        if(!isset($this->options['junction_foreign_key'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81 4
            $this->options['junction_foreign_key'] = 
82 4
                Text::singularize($foreignModel->getTable()) . '_id';
83
        }
84 4
    }
85
}
86