Service   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 67
rs 10
ccs 16
cts 16
cp 1
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 10 2
A __construct() 0 15 3
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
 */
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
 * @package Kocuj\Di\ArgumentParser\Service
21
 */
22
class Service implements ArgumentParserInterface
23
{
24
    /**
25
     * Dependency injection container for services
26
     *
27
     * @var ContainerInterface
28
     */
29
    private $container;
30
31
    /**
32
     * Service identifier
33
     *
34
     * @var string
35
     */
36
    private $id;
37
38
    /**
39
     * Service argument to parse
40
     *
41
     * @var array
42
     */
43
    private $argument;
44
45
    /**
46
     * Constructor
47
     *
48
     * @param ContainerInterface $container Dependency injection container for services
49
     * @param string $id Service identifier
50
     * @param array $argument Service argument to parse
51
     * @throws Exception
52
     */
53 12
    public function __construct(ContainerInterface $container, string $id, array $argument)
54
    {
55
        // remember arguments
56 12
        $this->container = $container;
57 12
        $this->id = $id;
58 12
        $this->argument = $argument;
59
        // check argument in "service" element - for compatibility with 1.2.0
60 12
        if (isset($this->argument['service'])) {
61 3
            $this->argument['value'] = $this->argument['service'];
62 3
            trigger_error('Argument "service" is deprecated and will be removed in version 2.0.0; please use "value" instead',
63 3
                E_USER_NOTICE);
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