Completed
Push — 2.x ( a081e5 )
by Daniel
15:13
created

AdapterChainServiceFactory::__invoke()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 10
nc 5
nop 3
1
<?php
2
namespace ZfcUser\Authentication\Adapter;
3
4
use Interop\Container\ContainerInterface;
5
use Interop\Container\Exception\ContainerException;
6
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
7
use Zend\ServiceManager\Exception\ServiceNotFoundException;
8
use Zend\ServiceManager\Factory\FactoryInterface;
9
use Zend\ServiceManager\ServiceLocatorInterface;
10
use ZfcUser\Authentication\Adapter\AdapterChain;
11
use ZfcUser\Options\ModuleOptions;
12
use ZfcUser\Authentication\Adapter\Exception\OptionsNotFoundException;
13
14
class AdapterChainServiceFactory implements FactoryInterface
15
{
16
    public function __invoke(ContainerInterface $serviceLocator, $requestedName, array $options = null)
17
    {
18
        $chain = new AdapterChain();
19
20
        $options = $this->getOptions($serviceLocator);
0 ignored issues
show
Documentation introduced by
$serviceLocator is of type object<Interop\Container\ContainerInterface>, but the function expects a null|object<Zend\Service...erviceLocatorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
21
22
        //iterate and attach multiple adapters and events if offered
23
        foreach ($options->getAuthAdapters() as $priority => $adapterName) {
24
            $adapter = $serviceLocator->get($adapterName);
25
26
            if (is_callable(array($adapter, 'authenticate'))) {
27
                $chain->getEventManager()->attach('authenticate', array($adapter, 'authenticate'), $priority);
28
            }
29
30
            if (is_callable(array($adapter, 'logout'))) {
31
                $chain->getEventManager()->attach('logout', array($adapter, 'logout'), $priority);
32
            }
33
        }
34
35
        return $chain;
36
    }
37
38
    /**
39
     * @var ModuleOptions
40
     */
41
    protected $options;
42
43
    public function createService(ServiceLocatorInterface $serviceLocator)
44
    {
45
        $this->__invoke($serviceLocator, null);
46
    }
47
48
49
    /**
50
     * set options
51
     *
52
     * @param ModuleOptions $options
53
     * @return AdapterChainServiceFactory
54
     */
55
    public function setOptions(ModuleOptions $options)
56
    {
57
        $this->options = $options;
58
        return $this;
59
    }
60
61
    /**
62
     * get options
63
     *
64
     * @param ServiceLocatorInterface $serviceLocator (optional) Service Locator
65
     * @return ModuleOptions $options
66
     * @throws OptionsNotFoundException If options tried to retrieve without being set but no SL was provided
67
     */
68
    public function getOptions(ServiceLocatorInterface $serviceLocator = null)
69
    {
70
        if (!$this->options) {
71
            if (!$serviceLocator) {
72
                throw new OptionsNotFoundException(
73
                    'Options were tried to retrieve but not set ' .
74
                    'and no service locator was provided'
75
                );
76
            }
77
78
            $this->setOptions($serviceLocator->get('zfcuser_module_options'));
79
        }
80
81
        return $this->options;
82
    }
83
}
84