Completed
Pull Request — master (#681)
by Sean
06:17
created

StateMachineFactory::addHandlers()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 14
nc 6
nop 1
1
<?php
2
3
use Finite\State\StateInterface;
4
5
class StateMachineFactory extends Object {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
7
	private static $handlers = [];
8
9
	public function forDNDeployment(DNDeployment $obj) {
10
		$loader = new Finite\Loader\ArrayLoader([
11
			'class'   => 'DNDeployment',
12
			'states'  => [
13
				DNDeployment::STATE_NEW => ['type' => StateInterface::TYPE_INITIAL],
14
				DNDeployment::STATE_SUBMITTED => ['type' => StateInterface::TYPE_NORMAL],
15
				DNDeployment::STATE_INVALID => ['type' => StateInterface::TYPE_NORMAL],
16
				DNDeployment::STATE_APPROVED => ['type' => StateInterface::TYPE_NORMAL],
17
				DNDeployment::STATE_REJECTED => ['type' => StateInterface::TYPE_NORMAL],
18
				DNDeployment::STATE_QUEUED => ['type' => StateInterface::TYPE_NORMAL],
19
				DNDeployment::STATE_DEPLOYING => ['type' => StateInterface::TYPE_NORMAL],
20
				DNDeployment::STATE_ABORTING => ['type' => StateInterface::TYPE_NORMAL],
21
				DNDeployment::STATE_COMPLETED => ['type' => StateInterface::TYPE_FINAL],
22
				DNDeployment::STATE_FAILED => ['type' => StateInterface::TYPE_FINAL],
23
			],
24
			'transitions' => [
25
				DNDeployment::TR_NEW => ['from' => [DNDeployment::STATE_SUBMITTED, 'to' => DNDeployment::STATE_NEW],
26
				DNDeployment::TR_SUBMIT => ['from' => [DNDeployment::STATE_NEW], 'to' => DNDeployment::STATE_SUBMITTED],
27
				DNDeployment::TR_QUEUE => [
28
					'from' => [
29
						DNDeployment::STATE_SUBMITTED,
30
						DNDeployment::STATE_APPROVED
31
					],
32
					'to' => DNDeployment::STATE_QUEUED
33
				],
34
				DNDeployment::TR_INVALIDATE  => [
35
					'from' => [
36
						DNDeployment::STATE_NEW,
37
						DNDeployment::STATE_SUBMITTED,
38
						DNDeployment::STATE_APPROVED,
39
						DNDeployment::STATE_REJECTED
40
					],
41
					'to' => DNDeployment::STATE_INVALID
42
				],
43
				DNDeployment::TR_APPROVE => ['from' => [DNDeployment::STATE_SUBMITTED], 'to' => DNDeployment::STATE_APPROVED],
44
				DNDeployment::TR_REJECT => ['from' => [DNDeployment::STATE_SUBMITTED], 'to' => DNDeployment::STATE_REJECTED],
45
				DNDeployment::TR_DEPLOY => ['from' => [DNDeployment::STATE_QUEUED], 'to' => DNDeployment::STATE_DEPLOYING],
46
				DNDeployment::TR_ABORT => [
47
					'from' => [
48
						DNDeployment::STATE_QUEUED,
49
						DNDeployment::STATE_DEPLOYING,
50
						DNDeployment::STATE_ABORTING
51
					],
52
					'to' => DNDeployment::STATE_ABORTING
53
				],
54
				DNDeployment::TR_COMPLETE => ['from' => [DNDeployment::STATE_DEPLOYING], 'to' => DNDeployment::STATE_COMPLETED],
55
				DNDeployment::TR_FAIL  => [
56
					'from' => [
57
						DNDeployment::STATE_NEW,
58
						DNDeployment::STATE_SUBMITTED,
59
						DNDeployment::STATE_INVALID,
60
						DNDeployment::STATE_REJECTED,
61
						DNDeployment::STATE_APPROVED,
62
						DNDeployment::STATE_QUEUED,
63
						DNDeployment::STATE_DEPLOYING,
64
						DNDeployment::STATE_ABORTING
65
					],
66
					'to' => DNDeployment::STATE_FAILED
67
				],
68
			]
69
		]);
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')', expecting ']'
Loading history...
70
		$stateMachine = Injector::inst()->get('Finite\StateMachine\StateMachine', true, [$obj]);
71
		$loader->load($stateMachine);
72
		$stateMachine->initialize();
73
		$this->addHandlers($stateMachine);
74
		return $stateMachine;
75
	}
76
77
	protected function addHandlers($machine) {
78
		$class = get_class($machine->getObject());
79
		if (empty($this->config()->handlers[$class])) return;
80
81
		$transitions = $this->config()->handlers[$class];
82
		foreach ($transitions as $transName => $handlers) {
83
			if (!is_array($handlers)) {
84
				throw new Exception(sprintf('Transition %s must be configured as an assoc array.', $transName));
85
			}
86
87
			foreach ($handlers as $handlerClass => $handlerMethod) {
88
				$handlerObj = Injector::inst()->get($handlerClass);
89
				if (!is_callable([$handlerObj, $handlerMethod])) {
90
					throw new Exception(sprintf('Handler %s is not callable on %s.', $handlerMethod, $handlerClass));
91
				}
92
				$machine->getDispatcher()->addListener(
93
					sprintf('finite.post_transition.%s', $transName),
94
					[$handlerObj, $handlerMethod]
95
				);
96
			}
97
		}
98
	}
99
100
}
101