Completed
Push — master ( 0b3edf...8cb687 )
by Alexis
01:31
created

SilexUserServiceProvider::subscribe()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.2
cc 4
eloc 7
nc 8
nop 2
1
<?php
2
3
namespace AWurth\SilexUser\Provider;
4
5
use AWurth\SilexUser\Controller\AuthController;
6
use AWurth\SilexUser\Controller\RegistrationController;
7
use AWurth\SilexUser\Entity\UserManager;
8
use AWurth\SilexUser\EventListener\AuthenticationListener;
9
use AWurth\SilexUser\EventListener\EmailConfirmationListener;
10
use AWurth\SilexUser\EventListener\FlashListener;
11
use AWurth\SilexUser\Mailer\TwigSwiftMailer;
12
use AWurth\SilexUser\Security\LoginManager;
13
use LogicException;
14
use Pimple\Container;
15
use Pimple\ServiceProviderInterface;
16
use Silex\Api\BootableProviderInterface;
17
use Silex\Api\ControllerProviderInterface;
18
use Silex\Api\EventListenerProviderInterface;
19
use Silex\Application;
20
use Silex\ControllerCollection;
21
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22
use Symfony\Component\Translation\Loader\PhpFileLoader;
23
use Symfony\Component\Translation\Translator;
24
25
/**
26
 * Silex User Service Provider.
27
 *
28
 * @author Alexis Wurth <[email protected]>
29
 */
30
class SilexUserServiceProvider implements ServiceProviderInterface, BootableProviderInterface, ControllerProviderInterface, EventListenerProviderInterface
31
{
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function register(Container $app)
36
    {
37
        // Configuration
38
        $app['silex_user.use_templates'] = true;
39
        $app['silex_user.use_translations'] = true;
40
        $app['silex_user.use_flash_notifications'] = true;
41
        $app['silex_user.use_authentication_listener'] = false;
42
        $app['silex_user.registration.confirmation.enable'] = false;
43
        $app['silex_user.registration.confirmation.from_email'] = null;
44
45
        // Services
46
        $app['silex_user.user_manager'] = function ($app) {
47
            return new UserManager($app);
48
        };
49
50
        $app['silex_user.login_manager'] = function ($app) {
51
            return new LoginManager(
52
                $app['security.token_storage'],
53
                $app['security.user_checker'],
54
                $app['security.session_strategy'],
55
                $app['request_stack']
56
            );
57
        };
58
59
        $app['silex_user.user_provider.username'] = function ($app) {
60
            return new UserProvider($app['silex_user.user_manager']);
61
        };
62
63
        $app['silex_user.user_provider.username_email'] = function ($app) {
64
            return new EmailUserProvider($app['silex_user.user_manager']);
65
        };
66
67
        $app['silex_user.mailer'] = function ($app) {
68
            if (isset($app['mailer']) && get_class($app['mailer']) === 'Swift_Mailer') {
69
                $parameters = [
70
                    'from_email' => [
71
                        'confirmation' => $app['silex_user.registration.confirmation.from_email']
72
                    ]
73
                ];
74
75
                return new TwigSwiftMailer($app['mailer'], $app['twig'], $app['url_generator'], $parameters);
76
            }
77
78
            return null;
79
        };
80
81
        // Controllers
82
        $app['auth.controller'] = function ($app) {
83
            return new AuthController($app);
84
        };
85
86
        $app['registration.controller'] = function ($app) {
87
            return new RegistrationController($app);
88
        };
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function boot(Application $app)
95
    {
96
        if (!isset($app['silex_user.user_class'])) {
97
            throw new LogicException('The "silex_user.user_class" option must be set');
98
        }
99
100
        if (!isset($app['silex_user.firewall_name'])) {
101
            throw new LogicException('The "silex_user.firewall_name" option must be set');
102
        }
103
104
        if (true === $app['silex_user.registration.confirmation.enable']) {
105
            if (null === $app['silex_user.mailer']) {
106
                throw new LogicException('You must configure a mailer to enable email notifications');
107
            }
108
109
            if (empty($app['silex_user.registration.confirmation.from_email'])) {
110
                throw new LogicException('The "silex_user.registration.confirmation.from_email" must be set');
111
            }
112
        }
113
114
        if (true === $app['silex_user.use_templates']) {
115
            $app['twig.loader.filesystem']->addPath(__DIR__ . '/../Resources/views/');
116
        }
117
118
        if (true === $app['silex_user.use_translations']) {
119
            /** @var Translator $translator */
120
            $translator = $app['translator'];
121
122
            $translator->addLoader('php', new PhpFileLoader());
123
124
            $translator->addResource('php', __DIR__ . '/../Resources/translations/en.php', 'en');
125
            $translator->addResource('php', __DIR__ . '/../Resources/translations/fr.php', 'fr');
126
        }
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function connect(Application $app)
133
    {
134
        /** @var ControllerCollection $controllers */
135
        $controllers = $app['controllers_factory'];
136
137
        $controllers->get('/login', 'auth.controller:loginAction')
138
            ->bind('silex_user.login');
139
140
        $controllers->method('GET|POST')
141
            ->match('/register', 'registration.controller:registerAction')
142
            ->bind('silex_user.register');
143
144
        $controllers->get('/register/confirmed', 'registration.controller:confirmedAction')
145
            ->bind('silex_user.registration_confirmed');
146
147
        if (true === $app['silex_user.registration.confirmation.enable']) {
148
            $controllers->get('/register/check-email', 'registration.controller:checkEmailAction')
149
                ->bind('silex_user.registration_check_email');
150
151
            $controllers->get('/register/confirm/{token}', 'registration.controller:confirmAction')
152
                ->bind('silex_user.registration_confirm');
153
        }
154
155
        return $controllers;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
162
    {
163
        if (true === $app['silex_user.use_authentication_listener']) {
164
            $dispatcher->addSubscriber(new AuthenticationListener($app['silex_user.login_manager'], $app['silex_user.firewall_name']));
165
        }
166
167
        if (true === $app['silex_user.use_flash_notifications']) {
168
            $dispatcher->addSubscriber(new FlashListener($app['session'], $app['translator']));
169
        }
170
171
        if (true === $app['silex_user.registration.confirmation.enable']) {
172
            $dispatcher->addSubscriber(new EmailConfirmationListener($app['silex_user.mailer'], $app['url_generator'], $app['session']));
173
        }
174
    }
175
}
176