Passed
Push — master ( 429ea4...522764 )
by Anton
01:43
created

Repository::select()   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 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
namespace Cycle\ORM\Select;
11
12
use Cycle\ORM\RepositoryInterface;
13
use Cycle\ORM\Select;
14
15
/**
16
 * Repository provides ability to load entities and construct queries.
17
 */
18
class Repository implements RepositoryInterface
19
{
20
    /** @var Select */
21
    protected $selector;
22
23
    /**
24
     * Create repository linked to one specific selector.
25
     *
26
     * @param Select $selector
27
     */
28
    public function __construct(Select $selector)
29
    {
30
        $this->selector = $selector;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function findByPK($id)
37
    {
38
        return $this->select()->wherePK($id)->fetchOne();
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function findOne(array $scope = [])
45
    {
46
        return $this->select()->where($scope)->fetchOne();
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function findAll(array $scope = [], array $orderBy = []): iterable
53
    {
54
        return $this->select()->where($scope)->orderBy($orderBy)->fetchAll();
55
    }
56
57
    /**
58
     * Get selector associated with the repository.
59
     *
60
     * @return Select|iterable
61
     */
62
    public function select(): Select
63
    {
64
        return clone $this->selector;
65
    }
66
67
    /**
68
     * Repositories are always immutable by default.
69
     */
70
    public function __clone()
71
    {
72
        $this->selector = clone $this->selector;
73
    }
74
}