Passed
Pull Request — 1.x (#300)
by
unknown
02:12
created

PromiseMany::setOrderBy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Cycle DataMapper ORM
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\ORM\Promise;
13
14
use Cycle\ORM\Iterator;
15
use Cycle\ORM\ORMInterface;
16
use Cycle\ORM\Select;
17
18
/**
19
 * Promises the selection of the
20
 */
21
final class PromiseMany implements PromiseInterface
22
{
23
    /** @var ORMInterface @internal */
24
    private $orm;
25
26
    /** @var string */
27
    private $target;
28
29
    /** @var array */
30
    private $query = [];
31
32
    /** @var array */
33
    private $where = [];
34
35
    /** @var array */
36
    private $orderBy = [];
37
38
    /** @var array */
39
    private $resolved = [];
40
41
    /**
42
     * @param ORMInterface $orm
43
     * @param string       $target
44
     * @param array        $query
45
     * @param array        $where
46
     */
47
    public function __construct(
48
        ORMInterface $orm,
49
        string $target,
50
        array $query = [],
51
        array $where = [],
52
        array $orderBy = []
53
    ) {
54
        $this->orm = $orm;
55
        $this->target = $target;
56
        $this->query = $query;
57
        $this->where = $where;
58
        $this->orderBy = $orderBy;
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function __loaded(): bool
65
    {
66
        return $this->orm === null;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function __role(): string
73
    {
74
        return $this->target;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function __scope(): array
81
    {
82
        return $this->query;
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function __resolve()
89
    {
90
        if ($this->orm === null) {
91
            return $this->resolved;
92
        }
93
94
        if ($this->query === []) {
95
            // nothing to proxy to
96
            $this->orm = null;
97
98
            return [];
99
        }
100
101
        $select = new Select($this->orm, $this->target);
102
        $select->scope($this->orm->getSource($this->target)->getConstrain());
0 ignored issues
show
Deprecated Code introduced by
The function Cycle\ORM\Select\SourceInterface::getConstrain() has been deprecated: Will be renamed to `getScope` in the Cycle ORM v2. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

102
        $select->scope(/** @scrutinizer ignore-deprecated */ $this->orm->getSource($this->target)->getConstrain());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
103
        $select->andWhere($this->query + $this->where);
104
        $select->orderBy($this->orderBy);
105
106
        $iterator = new Iterator($this->orm, $this->target, $select->fetchData(), true);
107
108
        $this->resolved = \iterator_to_array($iterator, false);
109
        $this->orm = null;
110
111
        return $this->resolved;
112
    }
113
}
114