Completed
Push — master ( 4a7120...86a668 )
by Peter
05:17
created

SlotFactory::create()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 48
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 48
rs 8.551
ccs 22
cts 22
cp 1
cc 6
eloc 19
nc 7
nop 4
crap 6
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 14
	public static function create(Signal $signals, $signal, $fqn, $injection)
28
	{
29
		// Clone signal, as it might be modified by slot
30 14
		$cloned = clone $signal;
31
32
		// Constructor injection
33 14
		if (true === $injection)
34 14
		{
35 2
			$slot = new $fqn($cloned);
36
37
			// Slot aware call
38 2
			if ($cloned instanceof SlotAwareInterface)
39 2
			{
40 1
				$cloned->setSlot($slot);
41 1
			}
42 2
			return $cloned;
43
		}
44
45
		// Check if class exists and log if doesn't
46
		// @codeCoverageIgnoreStart
47
		if (!ClassChecker::exists($fqn))
48
		{
49
			$signals->getLogger()->debug(sprintf("Class `%s` not found while emiting signal `%s`", $fqn, get_class($signal)));
50
			return false;
51
		}
52
		// @codeCoverageIgnoreEnd
53
		// Other type injection
54 12
		$slot = new $fqn;
55
56
		// Slot aware call
57 12
		if ($cloned instanceof SlotAwareInterface)
58 12
		{
59 1
			$cloned->setSlot($slot);
60 1
		}
61
62 12
		if (strstr($injection, '()'))
63 12
		{
64
			// Method injection
65 9
			$methodName = str_replace('()', '', $injection);
66 9
			$slot->$methodName($cloned);
67 9
		}
68
		else
69
		{
70
			// field injection
71 3
			$slot->$injection = $cloned;
72
		}
73 12
		return $cloned;
74
	}
75
76
}
77