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

ServiceFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 10 3
A __construct() 0 4 1
1
<?php
2
3
/**
4
 * ServiceFactory.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\Service;
12
13
use Kocuj\Di\ArgumentParser\ArgumentParserFactoryInterface;
14
use Kocuj\Di\Container\ContainerInterface;
15
use Kocuj\Di\Service\Shared\Shared;
16
use Kocuj\Di\Service\Standard\Standard;
17
18
/**
19
 * Service factory
20
 */
21
class ServiceFactory implements ServiceFactoryInterface
22
{
23
24
    /**
25
     * Service argument parser factory
26
     *
27
     * @var ArgumentParserFactoryInterface
28
     */
29
    private $argumentParserFactory;
30
31
    /**
32
     * Constructor
33
     *
34
     * @param
35
     *            ArgumentParserFactoryInterface Service argument parser factory
36
     *            @codeCoverageIgnore
37
     */
38
    public function __construct(ArgumentParserFactoryInterface $argumentParserFactory)
39
    {
40
        // remember arguments
41
        $this->argumentParserFactory = $argumentParserFactory;
42
    }
43
44
    /**
45
     * Create standard or shared service
46
     *
47
     * @param ContainerInterface $container
48
     *            Dependency injection container for services
49
     * @param ServiceType $serviceType
50
     *            Service type
51
     * @param string $id
52
     *            Service identifier
53
     * @param string $source
54
     *            Source for service to create
55
     * @param array $arguments
56
     *            Service arguments to parse
57
     * @return ServiceInterface Service creator object
58
     * @see \Kocuj\Di\Service\ServiceFactoryInterface::create() @codeCoverageIgnore
59
     */
60
    public function create(ContainerInterface $container, ServiceType $serviceType, string $id, string $source, array $arguments = []): ServiceInterface
61
    {
62
        // exit
63
        switch ($serviceType->getValue()) {
64
            case ServiceType::STANDARD:
65
                return new Standard($this->argumentParserFactory, $container, $id, $source, $arguments);
66
            case ServiceType::SHARED:
67
                return new Shared($this->argumentParserFactory, $container, $id, $source, $arguments);
68
            default:
69
                throw new Exception(sprintf('Unknown service type "%s"', $serviceType->getValue()));
70
        }
71
    }
72
}
73