Completed
Pull Request — develop (#542)
by Mathias
09:01
created

Module   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 91.3%

Importance

Changes 0
Metric Value
wmc 10
eloc 46
dl 0
loc 101
ccs 42
cts 46
cp 0.913
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 3 1
A getPublicDir() 0 3 1
A getAutoloaderConfig() 0 28 4
A init() 0 9 2
A onBootstrap() 0 36 2
1
<?php
2
/**
3
 * YAWIK
4
 * Auth Module Bootstrap
5
 *
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Auth;
11
12
use Acl\Listener\CheckPermissionsListener;
13
use Auth\Listener\SocialProfilesUnconfiguredErrorListener;
14
use Yawik\Composer\AssetProviderInterface;
15
use Core\ModuleManager\ModuleConfigLoader;
16
use Zend\Loader\Exception\InvalidArgumentException;
17
use Zend\Mvc\MvcEvent;
18
use Auth\Listener\TokenListener;
19
20
/**
21
 * Bootstrap class of the Auth module
22
 */
23
class Module implements AssetProviderInterface
24
{
25 5
    public function init(\Zend\ModuleManager\ModuleManagerInterface $moduleManager)
26
    {
27 5
        if (\Zend\Console\Console::isConsole()) {
28 1
            return;
29
        }
30
31 4
        $eventManager  = $moduleManager->getEventManager()->getSharedManager();
32 4
        $tokenListener = new TokenListener();
33 4
        $tokenListener->attachShared($eventManager);
34 4
    }
35
    /**
36
     * Loads module specific configuration.
37
     *
38
     * @return array
39
     */
40 5
    public function getConfig()
41
    {
42 5
        return ModuleConfigLoader::load(__DIR__ . '/../../config');
43
    }
44
45
    /**
46
     * Loads module specific autoloader configuration.
47
     *
48
     * @return array
49
     */
50 5
    public function getAutoloaderConfig()
51
    {
52 5
        $addProvidersDir = null;
53
        $directories = [
54 5
            __DIR__.'/../../../../hybridauth/hybridauth/additional-providers',
55
            __DIR__.'/../../../../vendor/hybridauth/hybridauth/additional-providers',
56
        ];
57
58 5
        foreach ($directories as $directory) {
59 5
            if (is_dir($directory)) {
60 5
                $addProvidersDir = $directory;
61 5
                break;
62
            }
63
        }
64
65 5
        if (is_null($addProvidersDir)) {
66
            throw new InvalidArgumentException('HybridAuth additional providers directories is not found.');
67
        }
68
69
        return array(
70
            'Zend\Loader\ClassMapAutoloader' => array(
71
                // This is an hack due to bad design of Hybridauth
72
                // This ensures the class from "addtional-providers" is loaded.
73
                array(
74 5
                    'Hybrid_Providers_XING' => $addProvidersDir . '/hybridauth-xing/Providers/XING.php',
75
                ),
76
                array(
77 5
                    'Hybrid_Providers_Github' => $addProvidersDir. '/hybridauth-github/Providers/GitHub.php',
78
                ),
79
            ),
80
        );
81
    }
82
83 4
    public function onBootstrap(MvcEvent $e)
84
    {
85 4
        if (\Zend\Console\Console::isConsole()) {
86
            return;
87
        }
88 4
        $eventManager = $e->getApplication()->getEventManager();
89 4
        $services     = $e->getApplication()->getServiceManager();
90
            
91 4
        $eventManager->attach(
92 4
            MvcEvent::EVENT_ROUTE,
93
            function (MvcEvent $e) use ($services) {
94
                /* @var $checkPermissionsListener \Acl\Listener\CheckPermissionsListener */
95 4
                $checkPermissionsListener = $services->get('Auth/CheckPermissionsListener');
96 4
                $checkPermissionsListener->onRoute($e);
97 4
            },
98 4
            -10
99
        );
100
101 4
        $eventManager->attach(
102 4
            MvcEvent::EVENT_DISPATCH,
103
            function (MvcEvent $e) use ($services) {
104
                /** @var CheckPermissionsListener $checkPermissionsListener */
105 4
                $checkPermissionsListener = $services->get('Auth/CheckPermissionsListener');
106 4
                $checkPermissionsListener->onDispatch($e);
107 4
            },
108 4
            10
109
        );
110
111 4
        $unauthorizedAccessListener = $services->get('UnauthorizedAccessListener');
112 4
        $unauthorizedAccessListener->attach($eventManager);
113
114 4
        $deactivatedUserListener = $services->get('DeactivatedUserListener');
115 4
        $deactivatedUserListener->attach($eventManager);
116
117 4
        $socialProfilesUnconfiguredErrorListener = new SocialProfilesUnconfiguredErrorListener();
118 4
        $socialProfilesUnconfiguredErrorListener->attach($eventManager);
119 4
    }
120
121
    public function getPublicDir()
122
    {
123
        return realpath(__DIR__.'/../../public');
124
    }
125
}
126