Test Failed
Push — master ( 1845c4...b386f4 )
by Kirill
03:46
created

RelationProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 81
ccs 12
cts 24
cp 0.5
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A with() 0 9 1
A leftJoin() 0 6 1
A innerJoin() 0 6 1
B addRelation() 0 26 6
A join() 0 6 1
1
<?php
2
/**
3
 * This file is part of Hydrogen package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace RDS\Hydrogen\Query;
11
12
use RDS\Hydrogen\Criteria\Join;
13
use RDS\Hydrogen\Criteria\Relation;
14
use RDS\Hydrogen\Query;
15
16
/**
17
 * Trait RelationProvider
18
 * @mixin Query
19
 */
20
trait RelationProvider
21
{
22
    /**
23
     * @param string|array ...$relations
24
     * @return Query|$this|self
25
     */
26
    public function with(...$relations): self
27
    {
28
        // TODO Add temporary fallback to left join
29
        return $this->leftJoin(...$relations);
30
31
        /*return $this->addRelation(function(string $field, \Closure $inner = null) {
32
            return new Relation($this, $field, $inner);
33
        }, ...$relations);*/
34
    }
35
36
    /**
37
     * @param string|array ...$relations
38
     * @return Query|$this|self
39
     */
40
    public function join(...$relations): self
41
    {
42 1
        return $this->addRelation(function(string $field, \Closure $inner = null) {
43 1
            return new Join($this, $field, Join::TYPE_JOIN, $inner);
44 1
        }, ...$relations);
45
    }
46
47
    /**
48
     * @param string|array ...$relations
49
     * @return Query|$this|self
50
     */
51
    public function leftJoin(...$relations): self
52
    {
53 1
        return $this->addRelation(function(string $field, \Closure $inner = null) {
54 1
            return new Join($this, $field, Join::TYPE_LEFT_JOIN, $inner);
55 1
        }, ...$relations);
56
    }
57
58
    /**
59
     * @param string|array ...$relations
60
     * @return Query|$this|self
61
     */
62
    public function innerJoin(...$relations): self
63
    {
64
        return $this->addRelation(function(string $field, \Closure $inner = null) {
65
            return new Join($this, $field, Join::TYPE_INNER_JOIN, $inner);
66
        }, ...$relations);
67
    }
68
69
    /**
70
     * @param \Closure $onCreate
71
     * @param string|array ...$relations
72
     * @return Query|$this|self
73
     */
74 2
    private function addRelation(\Closure $onCreate, ...$relations): self
75
    {
76 2
        foreach ($relations as $relation) {
77 2
            if (\is_string($relation)) {
78 2
                $this->add($onCreate($relation));
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
79 2
                continue;
80
            }
81
82
            if (\is_array($relation)) {
83
                foreach ($relation as $rel => $sub) {
84
                    \assert(\is_string($rel) && $sub instanceof \Closure);
85
86
                    $this->add($onCreate($rel, $sub));
0 ignored issues
show
Bug introduced by
It seems like add() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
87
                }
88
                continue;
89
            }
90
91
            $error = 'Relation should be string ("relation_name") '.
92
                'or array (["relation" => function]), ' .
93
                'but %s given';
94
95
            throw new \InvalidArgumentException(\sprintf($error, \gettype($relation)));
96
        }
97
98 2
        return $this;
99
    }
100
}
101