Completed
Push — 3.x ( 935b97...03c8d8 )
by Daniel
9s
created

AdapterChainServiceFactory::__invoke()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 8.9197
cc 4
eloc 11
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
        $chain->setEventManager($serviceLocator->get('EventManager'));
20
21
        $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...
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
     * @var ModuleOptions
41
     */
42
    protected $options;
43
44
    public function createService(ServiceLocatorInterface $serviceLocator)
45
    {
46
        $this->__invoke($serviceLocator, null);
47
    }
48
49
50
    /**
51
     * set options
52
     *
53
     * @param ModuleOptions $options
54
     * @return AdapterChainServiceFactory
55
     */
56
    public function setOptions(ModuleOptions $options)
57
    {
58
        $this->options = $options;
59
        return $this;
60
    }
61
62
    /**
63
     * get options
64
     *
65
     * @param ServiceLocatorInterface $serviceLocator (optional) Service Locator
66
     * @return ModuleOptions $options
67
     * @throws OptionsNotFoundException If options tried to retrieve without being set but no SL was provided
68
     */
69
    public function getOptions(ServiceLocatorInterface $serviceLocator = null)
70
    {
71
        if (!$this->options) {
72
            if (!$serviceLocator) {
73
                throw new OptionsNotFoundException(
74
                    'Options were tried to retrieve but not set ' .
75
                    'and no service locator was provided'
76
                );
77
            }
78
79
            $this->setOptions($serviceLocator->get('zfcuser_module_options'));
80
        }
81
82
        return $this->options;
83
    }
84
}
85