Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
Email
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 sendCode
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 code
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 signIn
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace Miniframe\SocialLogin\Controller;
4
5use Miniframe\Core\AbstractController;
6use Miniframe\Core\Config;
7use Miniframe\Core\Request;
8use Miniframe\Core\Response;
9use Miniframe\Middleware\Session;
10use Miniframe\Response\PhpResponse;
11use Miniframe\Response\RedirectResponse;
12use Miniframe\SocialLogin\Middleware\SocialLogin;
13use Miniframe\SocialLogin\Provider\Email as EmailProvider;
14
15class Email extends AbstractSocialLoginController
16{
17    /**
18     * Reference to the user session.
19     *
20     * @var Session
21     */
22    protected $session;
23    /**
24     * Reference to the Email Service
25     *
26     * @var EmailProvider
27     */
28    protected $emailService;
29
30    /**
31     * Initializes the Email controller
32     *
33     * @param Request     $request              Reference to the Request object.
34     * @param Config      $config               Reference to the Config object.
35     * @param SocialLogin $socialLogin          Reference to the Social Login middleware.
36     * @param string      $socialLoginPrefixUrl Base URL for the Social Login pages.
37     * @param Session     $session              Reference to the user session.
38     */
39    public function __construct(
40        Request $request,
41        Config $config,
42        SocialLogin $socialLogin,
43        string $socialLoginPrefixUrl,
44        Session $session
45    ) {
46        parent::__construct($request, $config, $socialLogin, $socialLoginPrefixUrl);
47        $this->session = $session;
48        $this->emailService = new EmailProvider(
49            $this->request,
50            $this->config,
51            $this->session,
52            $this->socialLoginPrefixUrl
53        );
54
55        // Extra security check; is emailing enabled?
56        if (!preg_grep('/^email$/i', $this->config->get('sociallogin', 'providers'))) {
57            throw new \RuntimeException('Email provider is not enabled');
58        }
59    }
60
61    /**
62     * Sends the code and redirects to the next step, or go back to the login page with an error message.
63     *
64     * @return Response
65     */
66    public function sendCode(): Response
67    {
68        $state = SocialLogin::parseState($this->request->getRequest('state') ?? $this->request->getPost('state'));
69        try {
70            $this->emailService->authenticate();
71        } catch (\InvalidArgumentException $exception) {
72            $state['error'] = $exception->getMessage();
73        }
74        return new RedirectResponse(
75            $this->socialLoginPrefixUrl . 'login?state=' . rawurlencode(SocialLogin::generateState($state))
76        );
77    }
78
79    /**
80     * Shows the page where the end-user can enter the code.
81     *
82     * @return Response
83     */
84    public function code(): Response
85    {
86        // Define the public href for the "Close" button
87        $publicHref = $this->config->has('sociallogin', 'public_href')
88            ? $this->config->get('sociallogin', 'public_href') : null;
89
90        // Do we have an error?
91        $state = SocialLogin::parseState($this->request->getRequest('state') ?? $this->request->getPost('state'));
92        $error = $state['error'] ?? null;
93        $state['error'] = null;
94
95        // Return template
96        return new PhpResponse(__DIR__ . '/../../templates/email_code.html.php', [
97            'socialLoginPrefixUrl' => $this->socialLoginPrefixUrl,
98            'publicHref' => $publicHref,
99            'email' => $this->session->get('_SOCIALLOGIN')['email']['address'],
100            'state' => SocialLogin::generateState($state),
101            'error' => $error,
102            'darkmode' => $this->darkmode,
103        ]);
104    }
105
106    /**
107     * Signs in the user, or go back to the code page with an error message.
108     *
109     * @return Response
110     */
111    public function signIn(): Response
112    {
113        $state = SocialLogin::parseState($this->request->getRequest('state') ?? $this->request->getPost('state'));
114        try {
115            $this->socialLogin->login($this->emailService->authenticate());
116            // Redirect to original page
117            return new RedirectResponse($state['redirectUrl'] ?? $this->config->get('framework', 'base_href'));
118        } catch (\InvalidArgumentException $exception) {
119            $state['error'] = $exception->getMessage();
120        }
121        return new RedirectResponse(
122            $this->socialLoginPrefixUrl . 'email/code?state=' . rawurlencode(SocialLogin::generateState($state))
123        );
124    }
125}