DefaultProvisioner::provision()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 6
nop 3
dl 0
loc 21
ccs 0
cts 11
cp 0
crap 42
rs 9.2222
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/boot project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Boot\Service\Provisioner;
10
11
use Auryn\Injector;
12
use Daikon\Boot\Service\ServiceDefinitionInterface;
13
use Daikon\Config\ConfigProviderInterface;
14
use Daikon\Interop\RuntimeException;
15
16
final class DefaultProvisioner implements ProvisionerInterface
17
{
18
    public function provision(
19
        Injector $injector,
20
        ConfigProviderInterface $configProvider,
21
        ServiceDefinitionInterface $serviceDefinition
22
    ): void {
23
        $serviceClass = $serviceDefinition->getServiceClass();
24
        $settings = $serviceDefinition->getSettings();
25
26
        $injector->define($serviceClass, [':settings' => $settings]);
27
28
        // there will only be one instance of the service when the "share" setting is true (default)
29
        if (!isset($settings['_share']) || true === $settings['_share']) {
30
            $injector->share($serviceClass);
31
        }
32
33
        if (isset($settings['_alias'])) {
34
            $alias = $settings['_alias'];
35
            if (!is_string($alias) && !class_exists($alias)) {
36
                throw new RuntimeException('Alias must be an existing fully qualified class or interface name.');
37
            }
38
            $injector->alias($alias, $serviceClass);
39
        }
40
    }
41
}
42