Completed
Push — master ( 6a0ca3...184221 )
by Peter
05:51
created

DbRefManager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 83.33%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 0
cbo 4
dl 0
loc 49
ccs 15
cts 18
cp 0.8333
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A extractRef() 0 11 2
A createInstanceFrom() 0 4 1
A maybeCreateInstanceFrom() 0 8 2
A save() 0 7 1
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\Helpers;
15
16
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
17
use Maslosoft\Mangan\EntityManager;
18
use Maslosoft\Mangan\Finder;
19
use Maslosoft\Mangan\Helpers\PkManager;
20
use Maslosoft\Mangan\Model\DbRef;
21
22
/**
23
 * Helper class for db refs
24
 *
25
 * @internal
26
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
27
 */
28
class DbRefManager
29
{
30
31
	/**
32
	 * Extract minimum set of data to create db reference
33
	 * @param AnnotatedInterface $model
34
	 * @param string $field
35
	 * @param AnnotatedInterface $referenced
36
	 */
37 10
	public static function extractRef(AnnotatedInterface $model, $field, AnnotatedInterface $referenced = null)
38
	{
39 10
		if (null === $referenced)
40
		{
41 5
			$referenced = $model->$field;
42
		}
43 10
		$dbRef = new DbRef();
44 10
		$dbRef->pk = PkManager::getFromModel($referenced);
45 10
		$dbRef->class = get_class($referenced);
46 10
		return $dbRef;
47
	}
48
49
	public static function createInstanceFrom(DbRef $dbRef)
50
	{
51
		return (new Finder($dbRef->class))->findByPk($dbRef->pk);
0 ignored issues
show
Documentation introduced by
$dbRef->class is of type string, but the function expects a object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
52
	}
53
54 24
	public static function maybeCreateInstanceFrom($dbRef)
55
	{
56 24
		if($dbRef instanceof DbRef)
57
		{
58
			return (new Finder(new $dbRef->class))->findByPk($dbRef->pk);
59
		}
60 24
		return $dbRef;
61
	}
62
63
	/**
64
	 * Save referenced model
65
	 * @param AnnotatedInterface $referenced
66
	 * @param DbRef $dbRef
67
	 */
68 8
	public static function save(AnnotatedInterface $referenced, DbRef $dbRef)
69
	{
70
		// Ensure ref is same as referenced model
71 8
		PkManager::applyToModel($referenced, $dbRef->pk);
72 8
		$em = new EntityManager($referenced);
73 8
		$em->save();
74 8
	}
75
76
}
77