Basic::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 *
5
 * This file is part of the Apix Project.
6
 *
7
 * (c) Franck Cassedanne <franck at ouarz.net>
8
 *
9
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
10
 *
11
 */
12
13
namespace Apix\Plugin\Auth;
14
15
/**
16
 * HTTP Basic authentication.
17
 *
18
 * @author Franck Cassedanne
19
 * @codeCoverageIgnore
20
 */
21
class Basic extends AbstractAuth
22
{
23
24
    /**
25
     * Constructor
26
     *
27
     * @param string|null $realm
28
     */
29
    public function __construct($realm = null)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER 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...
30
    {
31
        $this->realm = null !== $realm
32
                     ? $realm
33
                     : $_SERVER['SERVER_NAME'];
34
    }
35
36
    /**
37
     * @{@inheritdoc}
38
     */
39
    public function send()
40
    {
41
        header('WWW-Authenticate: Basic realm="' . $this->realm . '"');
42
        header('HTTP/1.0 401 Unauthorized');
43
    }
44
45
    /**
46
     * @{@inheritdoc}
47
     *
48
     * @link    http://uk3.php.net/manual/en/features.http-auth.php
49
     */
50
    public function authenticate()
0 ignored issues
show
Coding Style introduced by
authenticate uses the super-global variable $_SERVER 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...
51
    {
52
        if (
53
            isset($_SERVER['PHP_AUTH_USER'])
54
        ) {
55
            $this->username = $_SERVER['PHP_AUTH_USER'];
56
57
            $user = array(
58
                'username' => $_SERVER['PHP_AUTH_USER'],
59
                'password' => $_SERVER['PHP_AUTH_PW']
60
            );
61
            if (true === $this->getToken($user)) {
62
                return true;
63
            }
64
        }
65
66
        return $this->send();
67
    }
68
69
}
70