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

RelationProvider::join()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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