PropertyObjectProxyFactory   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 10
dl 0
loc 25
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A build() 0 8 1
A supports() 0 5 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