Completed
Push — master ( 5a3b36...5d92a3 )
by Peter
25:03
created

SlotFactory::create()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 47
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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