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

Receiver::attachTrashHandlers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 21
ccs 5
cts 5
cp 1
rs 9.3142
cc 2
eloc 11
nc 2
nop 0
crap 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