Completed
Push — master ( d306f8...b42bd6 )
by Derek Stephen
45:29
created

AuthorisationController::getForm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace App\Controller;
4
5
use Del\Form\Field\Radio;
6
use Del\Form\Field\Submit;
7
use Del\Form\Form;
8
use OAuth2\Request;
9
use OAuth2\Response;
10
11
class AuthorisationController extends OAuthController
12
{
13
14
    public function authAction()
0 ignored issues
show
Coding Style introduced by
authAction uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
15
    {
16
        $server = $this->oauth2Server;
17
18
        $request = Request::createFromGlobals();
19
        $response = new Response();
20
21
        if (!$server->validateAuthorizeRequest($request, $response)) {
22
            $response->send();
23
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method authAction() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
24
        }
25
26
        $form = $this->getForm();
27
28
        if (empty($_POST)) {
29
            $html = $form->render();
30
            echo $html;
31
            exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method authAction() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
32
        } else {
33
            $post = $this->getRequest()->getParsedBody();
34
            $form->populate($post);
0 ignored issues
show
Bug introduced by
It seems like $post defined by $this->getRequest()->getParsedBody() on line 33 can also be of type null or object; however, Del\Form\AbstractForm::populate() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
35
            if ($form->isValid()) {
36
                $data = $form->getValues();
37
                $isAuthorized = ($data['auth'] === 'yes');
38
                $server->handleAuthorizeRequest($request, $response, $isAuthorized);
39
                $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
40
                exit("SUCCESS! Authorization Code: $code");
0 ignored issues
show
Coding Style Compatibility introduced by
The method authAction() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
41
            }
42
        }
43
        $response->setStatusCode(400);
44
        $response->send();
45
        exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method authAction() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
46
    }
47
48
    /**
49
     * @return Form
50
     */
51
    private function getForm()
52
    {
53
        $form = new Form('auth');
54
        $radio = new Radio('auth');
55
        $radio->setOptions([
56
            'yes' => 'Yes',
57
            'no' => 'No',
58
        ]);
59
        $radio->setLabel('Do you authorise TestClient?');
60
        $radio->setRenderInline(true);
61
        $radio->setRequired(true);
62
        $submit = new Submit('submit');
63
64
        $form->addField($radio)
65
            ->addField($submit);
66
67
        return $form;
68
    }
69
}