Completed
Push — master ( c40504...c55971 )
by Peter
12:03
created

ParentIdHandler   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 18.75%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 0
cbo 3
dl 0
loc 59
ccs 3
cts 16
cp 0.1875
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setupHandlers() 0 17 2
B handle() 0 24 5
A maybeSetValue() 0 11 2
1
<?php
2
3
namespace Maslosoft\Mangan\Events\Handlers;
4
5
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
6
use Maslosoft\Mangan\EntityManager;
7
use Maslosoft\Mangan\Events\Event;
8
use Maslosoft\Mangan\Events\ModelEvent;
9
use Maslosoft\Mangan\Interfaces\EventHandlersInterface;
10
use Maslosoft\Mangan\Meta\ManganMeta;
11
use Maslosoft\Mangan\Traits\Model\WithParentTrait;
12
13
/**
14
 * ParentIdHandler
15
 *
16
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
17
 */
18
class ParentIdHandler implements EventHandlersInterface
19
{
20
21
	public function setupHandlers()
22
	{
23
		$on = [
24
			EntityManager::EventBeforeInsert,
25
			EntityManager::EventBeforeSave,
26
			EntityManager::EventBeforeUpdate,
27
		];
28
		$handler = [$this, 'handle'];
29
//		$handler = function(ModelEvent $e)
30
//		{
31
//			echo $e;
32
//		};
33
		foreach ($on as $name)
34
		{
35
			Event::on(WithParentTrait::class, $name, $handler);
36
		}
37
	}
38
39 6
	public function handle(ModelEvent $e)
40
	{
41 6
		$e->isValid = true;
42
		/**
43
		 * TODO This breaks Event/ParentChildTrashable
44
		 */
45 6
		return;
46
		$model = $e->sender;
0 ignored issues
show
Unused Code introduced by
$model = $e->sender; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
47
		$meta = ManganMeta::create($model);
48
		foreach ($meta->fields() as $name => $fieldMeta)
49
		{
50
			if ($model->$name instanceof AnnotatedInterface)
51
			{
52
				$this->maybeSetValue($model, $model->$name);
53
			}
54
			elseif (is_array($model->$name))
55
			{
56
				foreach ($model->$name as $subModel)
57
				{
58
					$this->maybeSetValue($model, $subModel);
59
				}
60
			}
61
		}
62
	}
63
64
	private function maybeSetValue($model, $subModel)
65
	{
66
		/**
67
		 * TODO Possibly does not handle recurent traits (deep)
68
		 */
69
		$traits = class_uses($subModel);
70
		if (in_array(WithParentTrait::class, $traits))
71
		{
72
			$subModel->parentId = $model->_id;
73
		}
74
	}
75
76
}
77