Context::getStateMachine()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Finite;
4
5
use Finite\Factory\FactoryInterface;
6
use Finite\StateMachine\StateMachine;
7
8
/**
9
 * The Finite context.
10
 * It provides easy ways to deal with Stateful objects, and factory.
11
 *
12
 * @author Yohan Giarelli <[email protected]>
13
 */
14
class Context
15
{
16
    /**
17
     * @var FactoryInterface
18
     */
19
    protected $factory;
20
21
    /**
22
     * @param FactoryInterface $factory
23
     */
24 60
    public function __construct(FactoryInterface $factory)
25
    {
26 60
        $this->factory = $factory;
27 60
    }
28
29
    /**
30
     * @param object $object
31
     * @param string $graph
32
     *
33
     * @return string
34
     */
35 10
    public function getState($object, $graph = 'default')
36
    {
37 10
        return $this->getStateMachine($object, $graph)->getCurrentState()->getName();
38
    }
39
40
    /**
41
     * @param object $object
42
     * @param string $graph
43
     * @param bool   $asObject
44
     *
45
     * @return array<string>
46
     */
47 15
    public function getTransitions($object, $graph = 'default', $asObject = false)
48
    {
49 15
        if (!$asObject) {
50 10
            return $this->getStateMachine($object, $graph)->getCurrentState()->getTransitions();
51
        }
52
53 5
        $stateMachine = $this->getStateMachine($object, $graph);
54
55 5
        return array_map(
56 5
            function ($transition) use ($stateMachine) {
57 5
                return $stateMachine->getTransition($transition);
58 5
            },
59 5
            $stateMachine->getCurrentState()->getTransitions()
60 3
        );
61
    }
62
63
    /**
64
     * @param object $object
65
     * @param string $graph
66
     *
67
     * @return array<string>
68
     */
69 10
    public function getProperties($object, $graph = 'default')
70
    {
71 10
        return $this->getStateMachine($object, $graph)->getCurrentState()->getProperties();
72
    }
73
74
    /**
75
     * @param object $object
76
     * @param string $property
77
     * @param string $graph
78
     *
79
     * @return bool
80
     */
81 10
    public function hasProperty($object, $property, $graph = 'default')
82
    {
83 10
        return $this->getStateMachine($object, $graph)->getCurrentState()->has($property);
84
    }
85
86
    /**
87
     * @param object $object
88
     * @param string $graph
89
     *
90
     * @return StateMachine
91
     */
92 60
    public function getStateMachine($object, $graph = 'default')
93
    {
94 60
        return $this->getFactory()->get($object, $graph);
95
    }
96
97
    /**
98
     * @return FactoryInterface
99
     */
100 60
    public function getFactory()
101
    {
102 60
        return $this->factory;
103
    }
104
}
105