Completed
Push — v2 ( d685ea )
by Peter
11:43
created

SlotFactory::create()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 47
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 8.5125
c 0
b 0
f 0
ccs 0
cts 33
cp 0
cc 6
eloc 19
nc 7
nop 4
crap 42
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL, Commercial` license[s].
5
 *
6
 * @package maslosoft/signals
7
 * @license AGPL, Commercial
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 *
11
 */
12
13
namespace Maslosoft\Signals\Factories;
14
15
use Maslosoft\Addendum\Utilities\ClassChecker;
16
use Maslosoft\Signals\Interfaces\SlotAwareInterface;
17
use Maslosoft\Signals\Signal;
18
19
/**
20
 * SlotFactory
21
 *
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
class SlotFactory
25
{
26
27
	public static function create(Signal $signals, $signal, $fqn, $injection)
28
	{
29
		// Clone signal, as it might be modified by slot
30
		$cloned = clone $signal;
31
32
		// Constructor injection
33
		if (true === $injection)
34
		{
35
			$slot = new $fqn($cloned);
36
37
			// Slot aware call
38
			if ($cloned instanceof SlotAwareInterface)
39
			{
40
				$cloned->setSlot($slot);
41
			}
42
			return $cloned;
43
		}
44
45
		// Check if class exists and log if doesn't
46
		if (!ClassChecker::exists($fqn))
47
		{
48
			$signals->getLogger()->debug(sprintf("Class `%s` not found while emiting signal `%s`", $fqn, get_class($signal)));
49
			return false;
50
		}
51
52
		// Other type injection
53
		$slot = new $fqn;
54
55
		// Slot aware call
56
		if ($cloned instanceof SlotAwareInterface)
57
		{
58
			$cloned->setSlot($slot);
59
		}
60
61
		if (strstr($injection, '()'))
62
		{
63
			// Method injection
64
			$methodName = str_replace('()', '', $injection);
65
			$slot->$methodName($cloned);
66
		}
67
		else
68
		{
69
			// field injection
70
			$slot->$injection = $cloned;
71
		}
72
		return $cloned;
73
	}
74
75
}
76