Passed
Pull Request — master (#1)
by Alex
01:36
created

AbstractFactory::buildService()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 4
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\LaminasFactory;
6
7
use Arp\LaminasFactory\Exception\ServiceNotCreatedException;
8
use Arp\LaminasFactory\Exception\ServiceNotFoundException;
9
use Interop\Container\ContainerInterface;
10
use Laminas\ServiceManager\Factory\FactoryInterface;
11
use Laminas\ServiceManager\ServiceLocatorInterface;
12
use Psr\Container\ContainerExceptionInterface;
13
14
/**
15
 * @author  Alex Patterson <[email protected]>
16
 * @package Arp\LaminasFactory
17
 */
18
abstract class AbstractFactory implements FactoryInterface
19
{
20
    /**
21
     * @trait ServiceOptionsProviderTrait
22
     */
23
    use ServiceOptionsProviderTrait;
24
25
    /**
26
     * @var array
27
     */
28
    protected $factoryOptions = [];
29
30
    /**
31
     * @param array $factoryOptions
32
     */
33
    public function __construct(array $factoryOptions = [])
34
    {
35
        $this->factoryOptions = $factoryOptions;
36
    }
37
38
    /**
39
     * @param ContainerInterface $container     The dependency injection container.
40
     * @param string             $name          The name of the service to retrieved.
41
     * @param string             $requestedName The service that is being created.
42
     *
43
     * @return mixed
44
     *
45
     * @throws ServiceNotFoundException If the requested service cannot be loaded.
46
     */
47
    protected function getService(ContainerInterface $container, string $name, string $requestedName)
48
    {
49
        if ($name === $requestedName) {
50
            throw new ServiceNotFoundException(
51
                sprintf(
52
                    'Encountered a circular dependency reference for service \'%s\'.',
53
                    $requestedName
54
                )
55
            );
56
        }
57
58
        if (!$container->has($name)) {
59
            throw new ServiceNotFoundException(
60
                sprintf(
61
                    'The required \'%s\' dependency could not be found while creating service \'%s\'.',
62
                    $name,
63
                    $requestedName
64
                )
65
            );
66
        }
67
68
        return $container->get($name);
69
    }
70
71
    /**
72
     * Create a new service instance using the provided $options.
73
     *
74
     * @param ServiceLocatorInterface $serviceLocator
75
     * @param string                  $name
76
     * @param array|null              $options
77
     * @param string                  $requestedName
78
     *
79
     * @return mixed
80
     *
81
     * @throws ServiceNotCreatedException  If the service cannot be built.
82
     */
83
    protected function buildService(
84
        ServiceLocatorInterface $serviceLocator,
85
        string $name,
86
        ?array $options,
87
        string $requestedName
88
    ) {
89
        try {
90
            return $serviceLocator->build($name, $options);
91
        } catch (ContainerExceptionInterface $e) {
92
            throw new ServiceNotCreatedException(
93
                sprintf(
94
                    'Failed to build service \'%s\' required as dependency of service \'%s\' : %s',
95
                    $name,
96
                    $requestedName,
97
                    $e->getMessage()
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface or Interop\Container\Exception\ContainerException or Interop\Container\Exception\NotFoundException or Interop\Container\Exception\NotFoundException or Laminas\ServiceManager\E...tion\ExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
                    $e->/** @scrutinizer ignore-call */ getMessage()
Loading history...
98
                ),
99
                $e->getCode(),
0 ignored issues
show
Bug introduced by
The method getCode() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface or Interop\Container\Exception\ContainerException or Interop\Container\Exception\NotFoundException or Interop\Container\Exception\NotFoundException or Laminas\ServiceManager\E...tion\ExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
                $e->/** @scrutinizer ignore-call */ getCode(),
Loading history...
100
                $e
101
            );
102
        }
103
    }
104
}
105