Passed
Push — master ( 4d9c37...7cec18 )
by Dominik
13:53
created

Service::parse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 2
1
<?php
2
3
/**
4
 * Service.php
5
 *
6
 * @author Dominik Kocuj
7
 * @license https://opensource.org/licenses/MIT The MIT License
8
 * @copyright Copyright (c) 2017-2018 kocuj.pl
9
 * @package kocuj_di
10
 */
11
namespace Kocuj\Di\ArgumentParser\Service;
12
13
use Kocuj\Di\ArgumentParser\ArgumentParserInterface;
14
use Kocuj\Di\ArgumentParser\Exception;
15
use Kocuj\Di\Container\ContainerInterface;
16
17
/**
18
 * Service argument parser for service
19
 */
20
class Service implements ArgumentParserInterface
21
{
22
23
    /**
24
     * Dependency injection container for services
25
     *
26
     * @var ContainerInterface
27
     */
28
    private $container;
29
30
    /**
31
     * Service identifier
32
     *
33
     * @var string
34
     */
35
    private $id;
36
37
    /**
38
     * Service argument to parse
39
     *
40
     * @var array
41
     */
42
    private $argument;
43
44
    /**
45
     * Constructor
46
     *
47
     * @param ContainerInterface $container
48
     *            Dependency injection container for services
49
     * @param string $id
50
     *            Service identifier
51
     * @param array $argument
52
     *            Service argument to parse
53
     */
54 12
    public function __construct(ContainerInterface $container, string $id, array $argument)
55
    {
56
        // remember arguments
57 12
        $this->container = $container;
58 12
        $this->id = $id;
59 12
        $this->argument = $argument;
60
        // check argument in "service" element - for compatibility with 1.2.0
61 12
        if (isset($this->argument['service'])) {
62 3
            trigger_error('Argument "service" is deprecated and will be removed in version 2.0.0; please use "value" instead', E_USER_NOTICE);
63
            $this->argument['value'] = $this->argument['service'];
64
        } else {
65
            // check argument
66 9
            if (! isset($this->argument['value'])) {
67 3
                throw new Exception('No "value" argument');
68
            }
69
        }
70 6
    }
71
72
    /**
73
     * Parse service argument and return argument to service constructor
74
     *
75
     * @throws Exception
76
     * @return mixed Parsed argument
77
     * @see \Kocuj\Di\ArgumentParser\ArgumentParserInterface::parse()
78
     */
79 6
    public function parse()
80
    {
81
        // get service from argument
82 6
        $service = $this->argument['value'];
83
        // check if service in argument is different than creating service
84 6
        if ($service === $this->id) {
85 3
            throw new Exception(sprintf('Service in argument can\'t be the same as creating service ("%s")', $service));
86
        }
87
        // exit
88 3
        return $this->container->get($service);
89
    }
90
}
91