Maybe::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\Proxy;
4
5
use Stratadox\Specification\Contract\Satisfiable;
6
7
/**
8
 * Maybe. Indicates that this proxy factory is a maybe, it depends on the
9
 * constraint being satisfied with the known data.
10
 *
11
 * @author Stratadox
12
 */
13
final class Maybe implements Choice
14
{
15
    private $factory;
16
    private $constraint;
17
18
    private function __construct(ProxyFactory $factory, Satisfiable $constraint)
19
    {
20
        $this->factory = $factory;
21
        $this->constraint = $constraint;
22
    }
23
24
    /**
25
     * Creates a new maybe situation for the proxy factory.
26
     *
27
     * @param ProxyFactory $factory    The factory to maybe use.
28
     * @param Satisfiable  $constraint The constraint that has to be met.
29
     * @return Choice                  The new maybe factory.
30
     */
31
    public static function the(
32
        ProxyFactory $factory,
33
        Satisfiable $constraint
34
    ): Choice {
35
        return new self($factory, $constraint);
36
    }
37
38
    /** @inheritdoc */
39
    public function shouldUseFor(array $data): bool
40
    {
41
        return $this->constraint->isSatisfiedBy($data);
42
    }
43
44
    /** @inheritdoc */
45
    public function create(array $knownData = []): Proxy
46
    {
47
        return $this->factory->create($knownData);
48
    }
49
}
50