Passed
Push — master ( c80e12...1c5165 )
by Kirill
04:32
created

RelationProvider::join()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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\Query;
14
15
/**
16
 * Trait RelationProvider
17
 * @mixin Query
18
 */
19
trait RelationProvider
20
{
21
    /**
22
     * @param string|array ...$relations
23
     * @return Query|$this|self
24
     */
25
    public function with(...$relations): self
26
    {
27 1
        return $this->addRelation(function(string $field, \Closure $inner = null) {
28
            // TODO
29
            // return new Relation($field, $this, $inner);
30 1
            return new Join($field, $this, Join::TYPE_LEFT_JOIN, $inner);
31 1
        }, ...$relations);
32
    }
33
34
    /**
35
     * @param string|array ...$relations
36
     * @return Query|$this|self
37
     */
38
    public function join(...$relations): self
39
    {
40
        return $this->addRelation(function(string $field, \Closure $inner = null) {
41
            return new Join($field, $this, Join::TYPE_JOIN, $inner);
42
        }, ...$relations);
43
    }
44
45
    /**
46
     * @param string|array ...$relations
47
     * @return Query|$this|self
48
     */
49
    public function leftJoin(...$relations): self
50
    {
51 1
        return $this->addRelation(function(string $field, \Closure $inner = null) {
52 1
            return new Join($field, $this, Join::TYPE_LEFT_JOIN, $inner);
53 1
        }, ...$relations);
54
    }
55
56
    /**
57
     * @param string|array ...$relations
58
     * @return Query|$this|self
59
     */
60
    public function innerJoin(...$relations): self
61
    {
62
        return $this->addRelation(function(string $field, \Closure $inner = null) {
63
            return new Join($field, $this, Join::TYPE_INNER_JOIN, $inner);
64
        }, ...$relations);
65
    }
66
67
    /**
68
     * @param \Closure $onCreate
69
     * @param string|array ...$relations
70
     * @return Query|$this|self
71
     */
72 2
    private function addRelation(\Closure $onCreate, ...$relations): self
73
    {
74 2
        foreach ($relations as $relation) {
75 2
            if (\is_string($relation)) {
76 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...
77 2
                continue;
78
            }
79
80
            if (\is_array($relation)) {
81
                foreach ($relation as $rel => $sub) {
82
                    \assert(\is_string($rel) && $sub instanceof \Closure);
83
84
                    $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...
85
                }
86
                continue;
87
            }
88
89
            $error = 'Relation should be string ("relation_name") '.
90
                'or array (["relation" => function]), ' .
91
                'but %s given';
92
93
            throw new \InvalidArgumentException(\sprintf($error, \gettype($relation)));
94
        }
95
96 2
        return $this;
97
    }
98
}
99