Completed
Push — master ( 515575...4e0394 )
by Vytautas
9s
created

canCreateServiceWithName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 3
crap 2
1
<?php
2
3
namespace Svycka\Settings\Collection\Factory;
4
5
use Interop\Container\ContainerInterface;
6
use Interop\Container\Exception\ContainerException;
7
use Svycka\Settings\Collection\CollectionsManager;
8
use Svycka\Settings\Collection\SettingsCollection;
9
use Svycka\Settings\Options\CollectionOptions;
10
use Svycka\Settings\Options\ModuleOptions;
11
use Svycka\Settings\Provider\OwnerProviderInterface;
12
use Svycka\Settings\Storage\StorageAdapterInterface;
13
use Svycka\Settings\Type\TypesManager;
14
use Zend\ServiceManager\Factory\AbstractFactoryInterface;
15
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
16
use Zend\ServiceManager\Exception\ServiceNotFoundException;
17
18
/**
19
 * @author Vytautas Stankus <[email protected]>
20
 * @license MIT
21
 */
22
final class SettingsCollectionAbstractFactory implements AbstractFactoryInterface
23
{
24
    /**
25
     * Can the factory create an instance for the service?
26
     *
27
     * @param  ContainerInterface $container
28
     * @param  string $requestedName
29
     * @return bool
30
     */
31 3
    public function canCreate(ContainerInterface $container, $requestedName)
32
    {
33
        /** @var CollectionsManager $config */
34 3
        $config = $container->get(ModuleOptions::class)->getCollections();
35 3
        if (empty($config)) {
36 2
            return false;
37
        }
38
39 1
        return isset($config[$requestedName]);
40
    }
41
42
    /**
43
     * Create an object
44
     *
45
     * @param  ContainerInterface $container
46
     * @param  string             $requestedName
47
     * @param  null|array         $options
48
     * @return object
49
     * @throws ServiceNotFoundException if unable to resolve the service.
50
     * @throws ServiceNotCreatedException if an exception is raised when
51
     *     creating a service.
52
     * @throws ContainerException if any other error occurs
53
     */
54 1
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
55
    {
56 1
        $collectionsConfig = $container->get(ModuleOptions::class)->getCollections();
57 1
        $config = new CollectionOptions($collectionsConfig[$requestedName]);
58 1
        $config->setName($requestedName);
59
60
        /** @var StorageAdapterInterface $storage */
61 1
        $storage = $container->get($config->getStorageAdapter());
62
        /** @var OwnerProviderInterface $owner_provider */
63 1
        $owner_provider = $container->get($config->getOwnerProvider());
64
        /** @var TypesManager $typesManager */
65 1
        $typesManager = $container->get(TypesManager::class);
66
67 1
        return new SettingsCollection($config, $storage, $owner_provider, $typesManager);
68
    }
69
}
70