SmsLoginController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.3%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 6
c 5
b 0
f 1
lcom 1
cbo 3
dl 0
loc 103
ccs 26
cts 27
cp 0.963
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A loginAction() 0 8 1
A verifyAction() 0 21 2
1
<?php
2
/**
3
 * This code is part of the Apiary SMS Login Provider project.
4
 *
5
 * (c) Darren Mothersele <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Apiary\SmsLoginProvider;
12
13
use Apiary\SmsLoginProvider\SmsHandler\SmsHandlerInterface;
14
use Silex\Application;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\Security\Core\Exception\AuthenticationException;
0 ignored issues
show
Coding Style introduced by
As per PSR2, there should be exactly one blank line after the last USE statement, 2 were found though.
Loading history...
17
18
19
class SmsLoginController
20
{
21
22
    /**
23
     * An array of countries for the select list options.
24
     *
25
     * @var array
26
     */
27
    protected $countries;
28
29
    /**
30
     * SMS Handler used to send validate numbers and send SMS verification
31
     *
32
     * @var \Apiary\SmsLoginProvider\SmsHandler\SmsHandlerInterface
33
     */
34
    protected $handler;
35
36
    /**
37
     * String used to create verification SMS message, must contain `%s` which
38
     * is replaced by the verification code.
39
     *
40
     * @var string
41
     */
42
    protected $messageTemplate;
43
44
    /**
45
     * The name of the view template to use to render the login form.
46
     * Defaults to 'login.twig'
47
     *
48
     * @var string
49
     */
50
    protected $viewName;
51
52
    protected $debug;
53
54
    /**
55
     * SmsLoginController constructor.
56
     * @param \Apiary\SmsLoginProvider\SmsHandler\SmsHandlerInterface $handler
57
     * @param string|NULL $view
58
     * @param string|NULL $message
59
     * @param null $debug
0 ignored issues
show
Bug introduced by
There is no parameter named $debug. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
60
     */
61 9
    public function __construct(
62
        SmsHandlerInterface $handler,
63
        $view = null,
64
        $message = null
65
    )
66
    {
67 9
        $this->messageTemplate = $message ?: 'Security code: %s';
68 9
        $countryData = file_get_contents(__DIR__ . '/../data/countries.json');
69 9
        $this->countries = json_decode($countryData);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($countryData) of type * is incompatible with the declared type array of property $countries.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
70 9
        $this->handler = $handler;
71 9
        $this->viewName = $view ?: 'login.twig';
72 9
    }
73
74
    /**
75
     * Login controller action
76
     * Step 1: Login shows form to enter mobile phone number.
77
     *
78
     * @param \Silex\Application $app
79
     * @param \Symfony\Component\HttpFoundation\Request $request
80
     * @return string
81
     */
82 3
    public function loginAction(Application $app, Request $request)
83
    {
84 3
        return $app['twig']->render($this->viewName, [
85 3
            'country_list' => $this->countries,
86 3
            'error' => $app['security.last_error']($request),
87 3
            'form_action' => $app['url_generator']->generate('sms.login'),
88 3
        ]);
89
    }
90
91
    /**
92
     * Verify controller action
93
     * Step 2: Send code to number by SMS and show form to verify code
94
     *
95
     * @param \Silex\Application $app
96
     * @param \Symfony\Component\HttpFoundation\Request $request
97
     * @return string
98
     */
99 3
    public function verifyAction(Application $app, Request $request)
100
    {
101 3
        $mobile = $request->get('mobile');
102 3
        $country = $request->get('country');
103
104 3
        if (empty($mobile)) {
105
            throw new AuthenticationException('Mobile number is required');
106
        }
107
108 3
        $code = sprintf("%'.05d", rand(0, 99999));
109 3
        $app['session']->set('code', $code);
110
111 3
        $number = $this->handler->lookupNumber($mobile, $country);
112 3
        $message = sprintf($this->messageTemplate, $code);
113 3
        $this->handler->sendSMS($number, $message);
114
115 3
        return $app['twig']->render($this->viewName, [
116 3
            'mobile' => $number,
117 3
            'form_action' => $app['url_generator']->generate('sms.login') . '/check',
118 3
        ]);
119
    }
120
121
}
0 ignored issues
show
Coding Style introduced by
According to PSR2, the closing brace of classes should be placed on the next line directly after the body.

Below you find some examples:

// Incorrect placement according to PSR2
class MyClass
{
    public function foo()
    {

    }
    // This blank line is not allowed.

}

// Correct
class MyClass
{
    public function foo()
    {

    } // No blank lines after this line.
}
Loading history...
122