Completed
Push — master ( 2132a7...0ecdcd )
by Peter
10:35
created

DbRefDecorator::ensureClass()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 7.9062

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
ccs 3
cts 8
cp 0.375
rs 9.2
c 1
b 0
f 0
cc 4
eloc 8
nc 3
nop 3
crap 7.9062
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\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\Finder;
21
use Maslosoft\Mangan\Helpers\DbRefManager;
22
use Maslosoft\Mangan\Helpers\NotFoundResolver;
23
use Maslosoft\Mangan\Interfaces\Decorators\Property\DecoratorInterface;
24
use Maslosoft\Mangan\Interfaces\Transformators\TransformatorInterface;
25
use Maslosoft\Mangan\Meta\ManganMeta;
26
use Maslosoft\Mangan\Model\DbRef;
27
28
/**
29
 * DbRefDecorator
30
 *
31
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
32
 */
33
class DbRefDecorator implements DecoratorInterface
34
{
35
36 2
	public function read($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
37
	{
38 2
		if (!$dbValue)
39
		{
40
			$fieldMeta = ManganMeta::create($model)->field($name);
41
			$model->$name = $fieldMeta->default;
42
			return;
43
		}
44
45
		// Assume that ref is already provided
46 2
		if (!empty($dbValue['_class']) && $dbValue['_class'] !== DbRef::class)
47
		{
48
			$model->$name = $transformatorClass::toModel($dbValue);
49
			return;
50
		}
51 2
		$dbValue['_class'] = DbRef::class;
52 2
		$dbRef = $transformatorClass::toModel($dbValue);
53 2
		self::ensureClass($model, $name, $dbRef);
54
		/* @var $dbRef DbRef */
55 2
		$referenced = new $dbRef->class;
56 2
		$model->$name = (new Finder($referenced))->findByPk($dbRef->pk);
57 2
	}
58
59 4
	public function write($model, $name, &$dbValue, $transformatorClass = TransformatorInterface::class)
60
	{
61 4
		if (!$model->$name)
62
		{
63
			return;
64
		}
65 4
		$dbRef = DbRefManager::extractRef($model, $name);
66 4
		$referenced = $model->$name;
67 4
		$fieldMeta = ManganMeta::create($model)->field($name);
68 4
		if ($fieldMeta->dbRef->updatable)
69
		{
70 3
			DbRefManager::save($referenced, $dbRef);
71
		}
72 4
		$dbValue[$name] = $transformatorClass::fromModel($dbRef, false);
73 4
	}
74
75 7
	public static function ensureClass($model, $name, DbRef $dbRef)
76
	{
77 7
		if (!ClassChecker::exists($dbRef->class))
78
		{
79
			$event = new ClassNotFound($model);
80
			$event->notFound = $dbRef->class;
81
			if (Event::hasHandler($model, NotFoundResolver::EventClassNotFound) && Event::handled($model, NotFoundResolver::EventClassNotFound, $event))
82
			{
83
				$dbRef->class = $event->replacement;
84
			}
85
			else
86
			{
87
				throw new ManganException(sprintf("Referenced model class `%s` not found in model `%s` field `%s`", $dbRef->class, get_class($model), $name));
88
			}
89
		}
90 7
	}
91
}
92