ServiceReference::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
/**
15
 * A representation of a service reference.
16
 *
17
 * Can be used to resolve the service or invoke it with some arguments.
18
 */
19
final class ServiceReference
20
{
21
	/**
22
	 * @var string
23
	 */
24
	private $id;
25
26
	/**
27
	 * @param string $id Service identifier
28
	 */
29
	public function __construct($id)
30
	{
31
		$this->id = $id;
32
	}
33
34
	/**
35
	 * @return string
36
	 */
37
	public function __toString()
38
	{
39
		return $this->id;
40
	}
41
42
	/**
43
	 * @param array ...$args
44
	 *
45
	 * @return mixed
46
	 */
47
	public function __invoke(...$args)
48
	{
49
		/* @var $service callable */
50
		$service = $this->resolve();
51
52
		return $service(...$args);
53
	}
54
55
	/**
56
	 * @param string $name
57
	 * @param array $arguments
58
	 */
59
	public function __call($name, array $arguments)
60
	{
61
		return $this->resolve()->$name(...$arguments);
62
	}
63
64
	/**
65
	 * @return object
66
	 */
67
	public function resolve()
68
	{
69
		return ServiceProvider::provide($this->id);
70
	}
71
}
72