AdapterChainServiceFactory::createService()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 10
nc 5
nop 1
1
<?php
2
namespace ZfcUser\Authentication\Adapter;
3
4
use Zend\ServiceManager\FactoryInterface;
5
use Zend\ServiceManager\ServiceLocatorInterface;
6
use ZfcUser\Options\ModuleOptions;
7
use ZfcUser\Authentication\Adapter\Exception\OptionsNotFoundException;
8
9
class AdapterChainServiceFactory implements FactoryInterface
10
{
11
12
    /**
13
     * @var ModuleOptions
14
     */
15
    protected $options;
16
17
    public function createService(ServiceLocatorInterface $serviceLocator)
18
    {
19
        $chain = new AdapterChain();
20
21
        $options = $this->getOptions($serviceLocator);
22
23
        //iterate and attach multiple adapters and events if offered
24
        foreach ($options->getAuthAdapters() as $priority => $adapterName) {
25
            $adapter = $serviceLocator->get($adapterName);
26
27
            if (is_callable(array($adapter, 'authenticate'))) {
28
                $chain->getEventManager()->attach('authenticate', array($adapter, 'authenticate'), $priority);
29
            }
30
31
            if (is_callable(array($adapter, 'logout'))) {
32
                $chain->getEventManager()->attach('logout', array($adapter, 'logout'), $priority);
33
            }
34
        }
35
36
        return $chain;
37
    }
38
39
40
    /**
41
     * set options
42
     *
43
     * @param ModuleOptions $options
44
     * @return AdapterChainServiceFactory
45
     */
46
    public function setOptions(ModuleOptions $options)
47
    {
48
        $this->options = $options;
49
        return $this;
50
    }
51
52
    /**
53
     * get options
54
     *
55
     * @param ServiceLocatorInterface $serviceLocator (optional) Service Locator
56
     * @return ModuleOptions $options
57
     * @throws OptionsNotFoundException If options tried to retrieve without being set but no SL was provided
58
     */
59
    public function getOptions(ServiceLocatorInterface $serviceLocator = null)
60
    {
61
        if (!$this->options) {
62
            if (!$serviceLocator) {
63
                throw new OptionsNotFoundException(
64
                    'Options were tried to retrieve but not set ' .
65
                    'and no service locator was provided'
66
                );
67
            }
68
69
            $this->setOptions($serviceLocator->get('zfcuser_module_options'));
70
        }
71
72
        return $this->options;
73
    }
74
}
75