GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ModelCollection   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 69
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A offsetExists() 0 13 2
A offsetGet() 0 16 3
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie\Module;
13
14
use ICanBoogie\ActiveRecord;
15
use ICanBoogie\ActiveRecord\ConnectionCollection;
16
use ICanBoogie\Module;
17
18
/**
19
 * Model collection.
20
 *
21
 * Extends the ActiveRecord model collection with the models defined by the modules.
22
 */
23
class ModelCollection extends ActiveRecord\ModelCollection
24
{
25
	/**
26
	 * @var ModuleCollection
27
	 */
28
	private $modules;
29
30
	/**
31
	 * @param ConnectionCollection $connections Connections manager.
32
	 * @param ModuleCollection $modules ModuleCollection manager.
33
	 * @param array $definitions Model definitions.
34
	 */
35
	public function __construct(ConnectionCollection $connections, ModuleCollection $modules, array $definitions = [])
36
	{
37
		parent::__construct($connections, $definitions);
38
39
		$this->modules = $modules;
40
	}
41
42
	/**
43
	 * Checks if a model exists by first checking if the module it belongs to is enabled and that
44
	 * it actually defines the model.
45
	 *
46
	 * @param string $id
47
	 *
48
	 * @return bool
49
	 */
50
	public function offsetExists($id)
51
	{
52
		list($module_id, $model_id) = explode('/', $id) + [ 1 => 'primary' ];
53
54
		if (!isset($this->modules[$module_id]))
55
		{
56
			return parent::offsetExists($id);
57
		}
58
59
		$descriptor = $this->modules->descriptors[$module_id];
60
61
		return isset($descriptor[Descriptor::MODELS][$model_id]);
62
	}
63
64
	/**
65
	 * Gets the specified model of the specified module.
66
	 *
67
	 * The pattern used to request a model is `<module_id>[/<model_id>]` where `<module_id>` is
68
	 * the identifier of the module and `<model_id>` is the identifier of the module's model. The
69
	 * `<model_id>` part is optional and defaults to `primary`.
70
	 *
71
	 * @param string $id Identifier of the model.
72
	 *
73
	 * @return ActiveRecord\Model
74
	 */
75
	public function offsetGet($id)
76
	{
77
		if (isset($this->instances[$id]))
78
		{
79
			return $this->instances[$id];
80
		}
81
82
		list($module_id, $model_id) = explode('/', $id) + [ 1 => 'primary' ];
83
84
		if (!isset($this->modules[$module_id]))
85
		{
86
			return parent::offsetGet($id);
87
		}
88
89
		return $this->instances[$id] = $this->modules[$module_id]->model($model_id);
90
	}
91
}
92