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

SlotFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 52
ccs 15
cts 18
cp 0.8333
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B create() 0 47 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 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