DiFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 43
wmc 5
lcom 1
cbo 4
ccs 10
cts 10
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A __invoke() 0 4 1
A get() 0 10 2
1
<?php
2
namespace mxdiModule\Service;
3
4
use mxdiModule\ServiceManager\DiAbstractFactory;
5
use mxdiModule\Service\Exception\CannotCreateService;
6
use mxdiModule\Traits\ServiceTrait;
7
use Zend\ServiceManager\ServiceLocatorAwareInterface;
8
use Zend\ServiceManager\ServiceLocatorAwareTrait;
9
10
final class DiFactory implements ServiceLocatorAwareInterface
11
{
12
    use ServiceLocatorAwareTrait;
13
    use ServiceTrait;
14
15
    /** @var DiAbstractFactory */
16
    private $diAbstractFactory;
17
18 3
    public function __construct(DiAbstractFactory $factory = null)
19
    {
20 3
        $this->diAbstractFactory = $factory ?: new DiAbstractFactory();
21 3
    }
22
23
    /**
24
     * Create service
25
     *
26
     * @param string $fqcn FQCN of the service
27
     * @return object
28
     */
29 1
    public function __invoke($fqcn)
30
    {
31 1
        return $this->get($fqcn);
32
    }
33
34
    /**
35
     * Create service
36
     *
37
     * @param string $fqcn FQCN of the service
38
     * @return object
39
     *
40
     * @throws CannotCreateService
41
     */
42 3
    public function get($fqcn)
43
    {
44 3
        $serviceName = $this->getCanonicalName($fqcn);
45
46 3
        if ($this->diAbstractFactory->canCreateServiceWithName($this->serviceLocator, $serviceName, $fqcn)) {
47 1
            return $this->diAbstractFactory->createServiceWithName($this->serviceLocator, $serviceName, $fqcn);
48
        }
49
50 2
        throw CannotCreateService::forClass($fqcn);
51
    }
52
}
53