Completed
Push — master ( 0661db...c488b0 )
by Peter
12:28
created

EmbeddedDecorator::getClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 3
cp 0
rs 9.4286
cc 1
eloc 3
nc 1
nop 2
crap 2
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 http://maslosoft.com/mangan/
12
 */
13
14
namespace Maslosoft\Mangan\Decorators;
15
16
use Maslosoft\Addendum\Utilities\ClassChecker;
17
use Maslosoft\Mangan\Events\ClassNotFound;
18
use Maslosoft\Mangan\Events\Event;
19
use Maslosoft\Mangan\Exceptions\ManganException;
20
use Maslosoft\Mangan\Helpers\NotFoundResolver;
21
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface;
22
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
23
use Maslosoft\Mangan\Meta\DocumentPropertyMeta;
24
use Maslosoft\Mangan\Meta\ManganMeta;
25
26
/**
27
 * EmbeddedDecorator
28
 *
29
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
30
 */
31
class EmbeddedDecorator implements DecoratorInterface
32
{
33
34 15
	public function read($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
35
	{
36 15
		static::ensureClass($model, $name, $dbValue);
37 15
		$embedded = $transformatorClass::toModel($dbValue, $model->$name, $model->$name);
38 15
		$model->$name = $embedded;
39 15
	}
40
41 21
	public function write($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
42
	{
43 21
		if (null === $model->$name)
44 21
		{
45 1
			$fieldMeta = ManganMeta::create($model)->$name;
46
			/* @var $fieldMeta DocumentPropertyMeta */
47 1
			$className = $fieldMeta->embedded->class;
48 1
			if (!is_string($className))
49 1
			{
50 1
				return null;
51
			}
52
			$dbValue[$name] = $transformatorClass::fromModel(new $className);
53
			return;
54
		}
55 20
		$dbValue[$name] = $transformatorClass::fromModel($model->$name);
56 20
	}
57
58 18
	public static function ensureClass($model, $name, &$dbValue)
59
	{
60 18
		if (!is_array($dbValue) || !array_key_exists('_class', $dbValue) || empty($dbValue['_class']))
61 18
		{
62
			$class = static::getClassName($model, $name);
63
		}
64
		else
65
		{
66 18
			$class = $dbValue['_class'];
67
		}
68 18
		if (!ClassChecker::exists($class))
69 18
		{
70 1
			$event = new ClassNotFound($model);
71 1
			$event->notFound = $class;
72 1
			if (Event::hasHandler($model, NotFoundResolver::EventClassNotFound) && Event::handled($model, NotFoundResolver::EventClassNotFound, $event))
73 1
			{
74 1
				$class = $event->replacement;
75 1
			}
76
			else
77
			{
78 1
				throw new ManganException(sprintf("Embedded model class `%s` not found in model `%s` field `%s`", $class, get_class($model), $name));
79
			}
80 1
		}
81 18
		$dbValue['_class'] = $class;
82 18
	}
83
84
	protected static function getClassName($model, $name)
85
	{
86
		$fieldMeta = ManganMeta::create($model)->$name;
87
88
		/* @var $fieldMeta DocumentPropertyMeta */
89
		return $fieldMeta->embedded->class;
90
	}
91
92
}
93