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

PromiseOne::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 one entity and resolves the result via ORM heap or entity repository.
15
 */
16
class PromiseOne implements PromiseInterface
17
{
18
    /** @var ORMInterface|null @internal */
19
    private $orm;
20
21
    /** @var string|null */
22
    private $target;
23
24
    /** @var array */
25
    private $scope;
26
27
    /** @var mixed */
28
    private $resolved;
29
30
    /**
31
     * @param ORMInterface $orm
32
     * @param string       $target
33
     * @param array        $scope
34
     */
35
    public function __construct(ORMInterface $orm, string $target, array $scope)
36
    {
37
        $this->orm = $orm;
38
        $this->target = $target;
39
        $this->scope = $scope;
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function __loaded(): bool
46
    {
47
        return empty($this->orm);
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function __role(): string
54
    {
55
        return $this->target;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->target could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function __scope(): array
62
    {
63
        return $this->scope;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function __resolve()
70
    {
71
        if (!is_null($this->orm)) {
72
            if (count($this->scope) !== 1) {
73
                $this->resolved = $this->orm->getRepository($this->target)->findOne($this->scope);
74
            } else {
75
                $key = key($this->scope);
76
                $value = $this->scope[$key];
77
78
                $this->resolved = $this->orm->get($this->target, $key, $value, true);
0 ignored issues
show
Bug introduced by
It seems like $this->target can also be of type null; however, parameter $role of Cycle\ORM\ORMInterface::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

78
                $this->resolved = $this->orm->get(/** @scrutinizer ignore-type */ $this->target, $key, $value, true);
Loading history...
79
            }
80
81
            $this->orm = null;
82
        }
83
84
        return $this->resolved;
85
    }
86
}