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

SettingsCollectionAbstractFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 48
c 0
b 0
f 0
wmc 3
lcom 0
cbo 3
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A canCreate() 0 10 2
A __invoke() 0 15 1
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