Passed
Push — 2.x ( 230dc9...9ce5e6 )
by Maxim
16:15
created

Repository::forUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Select;
6
7
use Cycle\ORM\RepositoryInterface;
8
use Cycle\ORM\Select;
9
10
/**
11
 * Repository provides ability to load entities and construct queries.
12
 *
13
 * @template TEntity of object
14
 *
15
 * @implements RepositoryInterface<TEntity>
16
 */
17
class Repository implements RepositoryInterface
18 910
{
19
    /**
20
     * Create repository linked to one specific selector.
21
     *
22
     * @param Select<TEntity> $select
23
     */
24
    public function __construct(
25
        /** @readonly */
26 24
        protected Select $select,
27
    ) {
28 24
    }
29
30
    /**
31 192
     * Repositories are always immutable by default.
32
     */
33 192
    public function __clone()
34
    {
35
        $this->select = clone $this->select;
36 614
    }
37
38 614
    public function findByPK($id): ?object
39
    {
40
        return $this->select()->wherePK($id)->fetchOne();
41 56
    }
42
43 56
    public function findOne(array $scope = []): ?object
44
    {
45
        return $this->select()->fetchOne($scope);
46
    }
47
48
    public function findAll(array $scope = [], array $orderBy = []): iterable
49 854
    {
50
        return $this->select()->where($scope)->orderBy($orderBy)->fetchAll();
51 854
    }
52
53
    /**
54
     * Get selector associated with the repository.
55
     *
56
     * @return Select<TEntity>
57
     */
58
    public function select(): Select
59
    {
60
        return clone $this->select;
61
    }
62
63
    public function forUpdate(): static
64
    {
65
        $repository = clone $this;
66
        $repository->select->forUpdate();
67
68
        return $repository;
69
    }
70
}
71