Passed
Push — master ( 0ea13e...268b05 )
by Anton
01:37
created

PromiseMany::setConstrain()   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 declare(strict_types=1);
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Cycle\ORM\Promise;
10
11
use Cycle\ORM\ORMInterface;
12
13
/**
14
 * Promises the selection of the
15
 */
16
class PromiseMany implements PromiseInterface
17
{
18
    /** @var ORMInterface @internal */
19
    private $orm;
20
21
    /** @var string */
22
    private $target;
23
24
    /** @var array */
25
    private $query = [];
26
27
    /** @var array */
28
    private $where = [];
29
30
    /** @var array */
31
    private $resolved = [];
32
33
    /**
34
     * @param ORMInterface $orm
35
     * @param string       $target
36
     * @param array        $query
37
     * @param array        $where
38
     */
39
    public function __construct(ORMInterface $orm, string $target, array $query = [], array $where = [])
40
    {
41
        $this->orm = $orm;
42
        $this->target = $target;
43
        $this->query = $query;
44
        $this->where = $where;
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function __loaded(): bool
51
    {
52
        return empty($this->orm);
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function __role(): string
59
    {
60
        return $this->target;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function __scope(): array
67
    {
68
        return $this->query;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function __resolve()
75
    {
76
        if (is_null($this->orm)) {
77
            return $this->resolved;
78
        }
79
80
        foreach ($this->orm->getRepository($this->target)->findAll($this->query + $this->where) as $item) {
81
            $this->resolved[] = $item;
82
        }
83
        $this->orm = null;
84
85
        return $this->resolved;
86
    }
87
}