Completed
Push — master ( 230a66...5a1fa2 )
by Peter
34:35 queued 33:08
created

Receiver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 54.55%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 6
dl 0
loc 60
ccs 6
cts 11
cp 0.5455
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onSave() 0 4 1
A onDelete() 0 4 1
A onInit() 0 7 1
A attachTrashHandlers() 0 21 2
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel;
14
15
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
16
use Maslosoft\Mangan\Events\Event;
17
use Maslosoft\Mangan\Events\ModelEvent;
18
use Maslosoft\Mangan\Interfaces\TrashInterface;
19
use Maslosoft\Mangan\Signals\AfterDelete;
20
use Maslosoft\Mangan\Signals\AfterSave;
21
use Maslosoft\Mangan\Signals\ConfigInit;
22
use Maslosoft\Mangan\Traits\Model\TrashableTrait;
23
24
/**
25
 * Receiver of Mangan signals
26
 * TODO: Should remove from index on trash
27
 * TODO: Should add to index on trash restore
28
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
29
 */
30
class Receiver implements AnnotatedInterface
31
{
32
33
	/**
34
	 * @SlotFor(AfterSave)
35
	 * @param AfterSave $signal
36
	 */
37 6
	public function onSave(AfterSave $signal)
38
	{
39 6
		(new IndexManager($signal->model))->index();
40 6
	}
41
42
	/**
43
	 * @SlotFor(AfterDelete)
44
	 * @param AfterDelete $signal
45
	 */
46 1
	public function onDelete(AfterDelete $signal)
47
	{
48 1
		(new IndexManager($signal->model))->delete();
49 1
	}
50
51
	/**
52
	 * @SlotFor(ConfigInit)
53
	 * @codeCoverageIgnore Configuration is tested on Index/SearchArray test
54
	 * @param ConfigInit $signal
55
	 */
56
	public function onInit(ConfigInit $signal)
57
	{
58
		$signal->apply(require __DIR__ . '/config/mangan.cfg.php');
59
60
		// Trash does not emit signals
61
		$this->attachTrashHandlers();
62
	}
63
64
	/**
65
	 * @staticvar boolean $once
66
	 */
67
	private function attachTrashHandlers()
68
	{
69
		// @codeCoverageIgnoreStart
70
		static $once = true;
71
		if ($once)
72
		{
73
			$handler = function(ModelEvent $event)
74
			{
75
				// @codeCoverageIgnoreEnd
76
				/* @var $event ModelEvent */
77
				$model = $event->sender;
78
				$this->onDelete(new AfterDelete($model));
79
				$event->handled = true;
80
				$event->isValid = true;
81
				// @codeCoverageIgnoreStart
82
			};
83
			$handler->bindTo($this);
84
			Event::on(TrashableTrait::class, TrashInterface::EventAfterTrash, $handler);
85
			$once = false;
86
		}
87
	}
88
89
}
90