ServiceProvider   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A define() 0 8 1
A defined() 0 4 1
A undefine() 0 4 1
A provide() 0 11 2
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\Service;
13
14
class ServiceProvider
15
{
16
	/**
17
	 * @var callable
18
	 */
19
	static private $provider;
20
21
	/**
22
	 * Define the service provider.
23
	 *
24
	 * @param callable $provider
25
	 *
26
	 * @return callable The previous provider, or `null` if none was defined.
27
	 */
28
	static public function define(callable $provider)
29
	{
30
		$previous = self::$provider;
31
32
		self::$provider = $provider;
33
34
		return $previous;
35
	}
36
37
	/**
38
	 * Return the current provider.
39
	 *
40
	 * @return callable|null
41
	 */
42
	static public function defined()
43
	{
44
		return self::$provider;
45
	}
46
47
	/**
48
	 * Undefine the provider.
49
	 */
50
	static public function undefine()
51
	{
52
		self::$provider = null;
53
	}
54
55
	/**
56
	 * Return a service.
57
	 *
58
	 * @param string $id Service identifier.
59
	 *
60
	 * @return mixed
61
	 */
62
	static public function provide($id)
63
	{
64
		$provider = self::$provider;
65
66
		if (!$provider)
67
		{
68
			throw new \LogicException("No provider is defined yet. Please define one with `ServiceProvider::define(\$provider)`.");
69
		}
70
71
		return $provider($id);
72
	}
73
}
74