Completed
Pull Request — master (#1839)
by Basil
08:34
created

Request::getIsAdmin()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.4444
c 0
b 0
f 0
cc 8
nc 5
nop 0
1
<?php
2
3
namespace luya\web;
4
5
use Yii;
6
7
/**
8
 * Request Component.
9
 *
10
 * Extending the {{yii\web\Request}} class by predefine values and add ability to verify whether the request is in admin context or not.
11
 *
12
 * @property boolean $isAdmin Whether the request is admin or not.
13
 *
14
 * @author Basil Suter <[email protected]>
15
 * @since 1.0.0
16
 */
17
class Request extends \yii\web\Request
18
{
19
    /**
20
     * @var boolean Force web request to enable unit tests with simulated web requests
21
     */
22
    public $forceWebRequest = false;
23
    
24
    /**
25
     * @var string The validation cookie for cookies, should be overwritten in your configuration.
26
     *
27
     * The cookie validation key is generated randomly or by any new release but should be overriten in your config.
28
     *
29
     * http://randomkeygen.com using a 504-bit WPA Key
30
     */
31
    public $cookieValidationKey = '(`1gq(|TI2Zxx7zZH<Zk052a9a$@l2EtD9wT`lkTO@7uy{cPaJt4y70mxh4q(3';
32
    
33
    /**
34
     * @var array A list of default available parsers.
35
     */
36
    public $parsers = [
37
        'application/json' => 'yii\web\JsonParser',
38
    ];
39
    
40
    private $_isAdmin;
41
    
42
    /**
43
     * Setter method to force isAdmin request.
44
     *
45
     * @param boolean $state Whether its an admin request or not
46
     */
47
    public function setIsAdmin($state)
48
    {
49
        $this->_isAdmin = $state;
50
    }
51
    
52
    /**
53
     * Getter method resolves the current url request and check if admin context.
54
     *
55
     * This is mostly used in order to bootstrap more modules and application logic in admin context.
56
     *
57
     * @return boolean If the current request is in admin context return value is true, otherwise false.
58
     */
59
    public function getIsAdmin()
60
    {
61
        if ($this->_isAdmin === null) {
62
            if ($this->getIsConsoleRequest() && !$this->forceWebRequest && !Yii::$app->hasModule('admin')) {
63
                $this->_isAdmin = false;
64
            } else {
65
                // if there is only an application with admin module and set as default route
66
                // this might by the admin module even when pathInfo is empty
67
                if (Yii::$app->defaultRoute == 'admin' && empty($this->pathInfo)) {
68
                    $this->_isAdmin = true;
69
                } else {
70
                    $resolver = Yii::$app->composition->getResolvedPathInfo($this);
71
                    $parts = explode('/', $resolver->resolvedPath);
72
                    $first = reset($parts);
73
                    if (preg_match('/admin/i', $first, $results)) {
74
                        $this->_isAdmin = true;
75
                    } else {
76
                        $this->_isAdmin = false;
77
                    }
78
                }
79
            }
80
        }
81
        
82
        return $this->_isAdmin;
83
    }
84
    
85
    /**
86
     * Get the user client language.
87
     *
88
     * @param string $defaultValue Return if not set.
89
     * @return string
90
     */
91
    public function getClientLanguage($defaultValue)
0 ignored issues
show
Coding Style introduced by
getClientLanguage 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...
92
    {
93
        return isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : $defaultValue;
94
    }
95
}
96