PropertyObjectProxyFactory::build()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dflydev\FiniteStateMachine\ObjectProxy;
6
7
use Dflydev\FiniteStateMachine\Contracts\ObjectProxy;
8
use Dflydev\FiniteStateMachine\Contracts\ObjectProxyFactory;
9
use ReflectionClass;
10
use ReflectionProperty;
11
12
class PropertyObjectProxyFactory implements ObjectProxyFactory
13
{
14
    private string $defaultPropertyName;
15
16 21
    public function __construct(string $defaultPropertyName = 'state')
17
    {
18 21
        $this->defaultPropertyName = $defaultPropertyName;
19 21
    }
20
21 19
    public function build(object $object, array $options): ObjectProxy
22
    {
23 19
        $property = new ReflectionProperty($object, $options['property_path'] ?? $this->defaultPropertyName);
24 19
        $property->setAccessible(true);
25
26 19
        return new PropertyObjectProxy(
27 19
            $object,
28
            $property
29
        );
30
    }
31
32 19
    public function supports(object $object, array $options): bool
33
    {
34 19
        $class = new ReflectionClass($object);
35
36 19
        return $class->hasProperty($options['property_path'] ?? $this->defaultPropertyName);
37
    }
38
}
39