ServerInitializer::createDelegatorWithName()   B
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.3073

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 25
ccs 10
cts 13
cp 0.7692
rs 8.439
cc 5
eloc 11
nc 8
nop 4
crap 5.3073
1
<?php
2
/**
3
 * @author Stefano Torresi (http://stefanotorresi.it)
4
 * @license See the file LICENSE.txt for copying permission.
5
 * ************************************************
6
 */
7
8
namespace Thorr\OAuth2\Server;
9
10
use OAuth2\Server as OAuth2Server;
11
use Thorr\OAuth2\GrantType\ThirdPartyGrantType;
12
use Thorr\OAuth2\Options\ModuleOptions;
13
use Zend\ServiceManager\DelegatorFactoryInterface;
14
use Zend\ServiceManager\ServiceLocatorInterface;
15
16
class ServerInitializer implements DelegatorFactoryInterface
17
{
18
    /**
19
     * A factory that creates delegates of a given service
20
     *
21
     * @param ServiceLocatorInterface $serviceLocator the service locator which requested the service
22
     * @param string                  $name           the normalized service name
23
     * @param string                  $requestedName  the requested service name
24
     * @param callable                $callback       the callback that is responsible for creating the service
25
     *
26
     * @throws \RuntimeException
27
     *
28
     * @return OAuth2Server
29
     */
30 2
    public function createDelegatorWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName, $callback)
31
    {
32
        /** @var OAuth2Server|callable $oauth2Server */
33 2
        $oauth2Server = $callback();
34
35
        /* ensure compatibility with https://github.com/zfcampus/zf-oauth2/pull/93 */
36 2
        if (is_callable($oauth2Server)) {
37 1
            $oauth2ServerFactory = $oauth2Server;
38 1
            $oauth2Server        = $oauth2ServerFactory();
39 1
        }
40
41
        /** @var ModuleOptions $moduleOptions */
42 2
        $moduleOptions = $serviceLocator->get(ModuleOptions::class);
43
44 2
        $thirdPartyProviders = $moduleOptions->getThirdPartyProviders();
45
46 2
        if ($moduleOptions->isThirdPartyGrantTypeEnabled() && ! empty($thirdPartyProviders)) {
47
            /** @var ThirdPartyGrantType $thirdPartyGrant */
48
            $thirdPartyGrant = $serviceLocator->get(ThirdPartyGrantType::class);
49
50
            $oauth2Server->addGrantType($thirdPartyGrant);
51
        }
52
53 2
        return isset($oauth2ServerFactory) ? $oauth2ServerFactory : $oauth2Server;
54
    }
55
}
56