Completed
Push — master ( 91a3af...095fa7 )
by Peter
06:05
created

Receiver   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

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

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
 * 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\Manganel;
10
11
use Maslosoft\Mangan\Events\Event;
12
use Maslosoft\Mangan\Events\ModelEvent;
13
use Maslosoft\Mangan\Interfaces\TrashInterface;
14
use Maslosoft\Mangan\Signals\AfterDelete;
15
use Maslosoft\Mangan\Signals\AfterSave;
16
use Maslosoft\Mangan\Signals\ConfigInit;
17
use Maslosoft\Mangan\Traits\Model\TrashableTrait;
18
19
/**
20
 * Receiver of Mangan signals
21
 * TODO: Should remove from index on trash
22
 * TODO: Should add to index on trash restore
23
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
24
 */
25
class Receiver
26
{
27
28
	/**
29
	 * @SlotFor(AfterSave)
30
	 * @param AfterSave $signal
31
	 */
32 7
	public function onSave(AfterSave $signal)
33
	{
34 7
		(new IndexManager($signal->model))->index();
35 7
	}
36
37
	/**
38
	 * @SlotFor(AfterDelete)
39
	 * @param AfterDelete $signal
40
	 */
41 3
	public function onDelete(AfterDelete $signal)
42
	{
43 3
		(new IndexManager($signal->model))->delete();
44 3
	}
45
46
	/**
47
	 * @SlotFor(ConfigInit)
48
	 * @codeCoverageIgnore Configuration is tested on Index/SearchArray test
49
	 * @param ConfigInit $signal
50
	 */
51
	public function onInit(ConfigInit $signal)
52
	{
53
		$signal->apply(require __DIR__ . '/config/mangan.cfg.php');
54
55
		// Trash does not emit signals
56
		$this->attachTrashHandlers();
57
	}
58
59
	/**
60
	 * @staticvar boolean $once
61
	 */
62
	private function attachTrashHandlers()
63
	{
64
		// @codeCoverageIgnoreStart
65
		static $once = true;
66
		if ($once)
67
		{
68 1
			$handler = function(ModelEvent $event)
69
			{
70
				// @codeCoverageIgnoreEnd
71
				/* @var $event ModelEvent */
72 1
				$model = $event->sender;
73 1
				$this->onDelete(new AfterDelete($model));
74 1
				$event->handled = true;
75 1
				$event->isValid = true;
76
				// @codeCoverageIgnoreStart
77
			};
78
			$handler->bindTo($this);
79
			Event::on(TrashableTrait::class, TrashInterface::EventAfterTrash, $handler);
80
			$once = false;
81
		}
82
	}
83
84
}
85