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

ParentIdHandler::setupHandlers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
crap 6
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