UniqueModelsAwareTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 67.86%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 94
ccs 19
cts 28
cp 0.6786
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getModels() 0 4 1
A addModel() 0 9 2
A removeModel() 0 18 4
A hasModel() 0 11 3
A setModels() 0 16 2
1
<?php
2
3
/**
4
 * This software package is licensed under `AGPL-3.0-only, proprietary` license[s].
5
 *
6
 * @package maslosoft/manganel
7
 * @license AGPL-3.0-only, proprietary
8
 *
9
 * @copyright Copyright (c) Peter Maselkowski <[email protected]>
10
 * @link https://maslosoft.com/manganel/
11
 */
12
13
namespace Maslosoft\Manganel\Traits;
14
15
use Maslosoft\Addendum\Interfaces\AnnotatedInterface;
16
17
/**
18
 * Use this trait to provide mutli-model feature to classes.
19
 *
20
 * NOTE: It will keep only one instance type in models
21
 *
22
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
23
 */
24
trait UniqueModelsAwareTrait
25
{
26
27
	/**
28
	 * Annotated model
29
	 * @var AnnotatedInterface[]
30
	 */
31
	private $models = [];
32
33
	/**
34
	 * Annotated models
35
	 * @var AnnotatedInterface[]
36
	 */
37 32
	public function getModels()
38
	{
39 32
		return $this->models;
40
	}
41
42
	/**
43
	 *
44
	 * @param AnnotatedInterface[] $models
45
	 * @return $this
46
	 */
47 33
	public function setModels($models)
48
	{
49
		// Unique based on class name
50
		// See: http://stackoverflow.com/a/2561283/5444623
51
		$map = function($value)
52
		{
53 33
			if (is_object($value))
54
			{
55 33
				return get_class($value);
56
			}
57
			return $value;
58 33
		};
59 33
		$unique = array_intersect_key($models, array_unique(array_map($map, $models)));
60 33
		$this->models = array_values($unique);
61 33
		return $this;
62
	}
63
64
	/**
65
	 * Add model to set but only if it's instance is not present in set.
66
	 *
67
	 * @param AnnotatedInterface $model
68
	 * @return boolean Whether model was added
69
	 */
70 32
	public function addModel(AnnotatedInterface $model)
71
	{
72 32
		if (!$this->hasModel($model))
73
		{
74 29
			$this->models[] = $model;
75 29
			return true;
76
		}
77 28
		return false;
78
	}
79
80
	/**
81
	 * Remove model from set.
82
	 *
83
	 * @param AnnotatedInterface $model
84
	 * @return boolean Whether model was removed
85
	 */
86
	public function removeModel(AnnotatedInterface $model)
87
	{
88
		if (!$this->hasModel($model))
89
		{
90
			return false;
91
		}
92
		foreach ($this->models as $index => $existing)
93
		{
94
			if (get_class($existing) === get_class($model))
95
			{
96
				unset($this->models[$index]);
97
				return true;
98
			}
99
		}
100
101
		// Should not happen, model not found even if should exists
102
		return false;
103
	}
104
105 32
	public function hasModel(AnnotatedInterface $model)
106
	{
107 32
		foreach ($this->models as $existing)
108
		{
109 28
			if (get_class($existing) === get_class($model))
110
			{
111 28
				return true;
112
			}
113
		}
114 29
		return false;
115
	}
116
117
}
118