Completed
Push — master ( c632d0...4a7120 )
by Peter
23:08
created

SlotFactory::create()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 47
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.1666

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 47
ccs 15
cts 18
cp 0.8333
rs 8.5125
cc 6
eloc 19
nc 7
nop 4
crap 6.1666
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 13
	public static function create(Signal $signals, $signal, $fqn, $injection)
28
	{
29
		// Clone signal, as it might be modified by slot
30 13
		$cloned = clone $signal;
31
32
		// Constructor injection
33 13
		if (true === $injection)
34
		{
35 1
			$slot = new $fqn($cloned);
36
37
			// Slot aware call
38 1
			if ($cloned instanceof SlotAwareInterface)
39
			{
40
				$cloned->setSlot($slot);
41
			}
42 1
			return $cloned;
43
		}
44
45
		// Check if class exists and log if doesn't
46 12
		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 12
		$slot = new $fqn;
54
55
		// Slot aware call
56 12
		if ($cloned instanceof SlotAwareInterface)
57
		{
58 1
			$cloned->setSlot($slot);
59
		}
60
61 12
		if (strstr($injection, '()'))
62
		{
63
			// Method injection
64 9
			$methodName = str_replace('()', '', $injection);
65 9
			$slot->$methodName($cloned);
66
		}
67
		else
68
		{
69
			// field injection
70 3
			$slot->$injection = $cloned;
71
		}
72 12
		return $cloned;
73
	}
74
75
}
76