DiFactory::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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