Completed
Push — master ( d63021...7a62eb )
by Peter
04:31
created

IndexManagerHandler   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 47.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 40
ccs 10
cts 21
cp 0.4762
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setupHandlers() 0 16 2
A handle() 0 20 4
1
<?php
2
3
/**
4
 * This software package is licensed under AGPL or Commercial license.
5
 *
6
 * @package maslosoft/mangan
7
 * @licence AGPL or Commercial
8
 * @copyright Copyright (c) Piotr Masełkowski <[email protected]>
9
 * @copyright Copyright (c) Maslosoft
10
 * @copyright Copyright (c) Others as mentioned in code
11
 * @link https://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Events\Handlers;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\EntityManager;
18
use Maslosoft\Mangan\Events\Event;
19
use Maslosoft\Mangan\Events\ModelEvent;
20
use Maslosoft\Mangan\Helpers\IndexManager;
21
use Maslosoft\Mangan\Interfaces\EventHandlersInterface;
22
use Maslosoft\Mangan\Interfaces\FinderInterface;
23
24
class IndexManagerHandler implements EventHandlersInterface
25
{
26
	public function setupHandlers()
27
	{
28
		$on = [
29
			EntityManager::EventBeforeInsert,
30
			EntityManager::EventBeforeSave,
31
			EntityManager::EventBeforeUpdate,
32
			FinderInterface::EventBeforeFind,
33
			FinderInterface::EventBeforeCount,
34
			FinderInterface::EventBeforeExists,
35
		];
36
		$handler = [$this, 'handle'];
37
		foreach ($on as $name)
38
		{
39
			Event::on(AnnotatedInterface::class, $name, $handler);
40
		}
41
	}
42
43 105
	public function handle(ModelEvent $e)
44
	{
45 105
		$e->isValid = true;
46
		// Fast and ugly
47 105
		if($e->name === FinderInterface::EventBeforeFind)
48
		{
49 73
			$e->handled = true;
50
		}
51 105
		if($e->name === FinderInterface::EventBeforeExists)
52
		{
53 9
			$e->handled = true;
54
		}
55 105
		if($e->name === FinderInterface::EventBeforeCount)
56
		{
57 23
			$e->handled = true;
58
		}
59
		
60 105
		IndexManager::fly()->create($e->sender);
61 105
		return true;
62
	}
63
}