Completed
Push — master ( 38e383...432f1e )
by Vojtěch
03:37
created

RemoteAccessManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 96
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A process() 0 6 2
C isAllowed() 0 23 8
A isAllowedAll() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SixtyEightPublishers\Application\RemoteAccessManager;
6
7
use Nette\Http\IRequest;
8
use Nette\SmartObject;
9
use SixtyEightPublishers\Application\RemoteAccessManager\Handler\IAccessHandler;
10
11
/**
12
 * @method  RemoteAccessManager   onAccess()
13
 * @method  RemoteAccessManager   onDeny()
14
 */
15
class RemoteAccessManager implements IRemoteAccessManager
16
{
17
	use SmartObject;
18
19
	const
20
		COOKIE_SECRET = 'ram-secret-key',
21
		ALLOWED_ALL = TRUE,
22
		DENY_ALL = FALSE;
23
24
	/** @var \Nette\Http\IRequest */
25
	private $request;
26
27
	/** @var array */
28
	private $whitelist;
29
30
	/** @var array */
31
	private $blacklist;
32
33
	/** @var string|null */
34
	private $key;
35
36
	/** @var bool|true */
37
	private $mode;
38
39
	/** @var \SixtyEightPublishers\Application\RemoteAccessManager\Handler\IAccessHandler */
40
	private $handler;
41
42
	/** @var null|callable */
43
	public $onAllow;
44
45
	/** @var null|callable */
46
	public $onDeny;
47
48
	/**
49
	 * @param \Nette\Http\IRequest    $request
50
	 * @param string|array            $blacklist
51
	 * @param string|array            $whitelist
52
	 * @param string                  $key
53
	 * @param bool|TRUE               $mode
54
	 * @param IAccessHandler          $handler
55
	 */
56
	public function __construct(IRequest $request, $blacklist = [], $whitelist = [], $mode = self::ALLOWED_ALL, $key = self::COOKIE_SECRET, IAccessHandler $handler)
57
	{
58
		$this->request = $request;
59
		$this->blacklist = $blacklist;
0 ignored issues
show
Documentation Bug introduced by
It seems like $blacklist can also be of type string. However, the property $blacklist is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
60
		$this->whitelist = $whitelist;
0 ignored issues
show
Documentation Bug introduced by
It seems like $whitelist can also be of type string. However, the property $whitelist is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
61
		$this->mode = $mode;
62
		$this->key = $key;
63
		$this->handler = $handler;
64
65
		$this->onAllow[] = [$this->handler, 'allow'];
66
		$this->onDeny[] = [$this->handler, 'deny'];
67
	}
68
69
	public function process()
70
	{
71
		$this->isAllowed()
72
			? $this->onAllow()
0 ignored issues
show
Documentation Bug introduced by
The method onAllow does not exist on object<SixtyEightPublish...er\RemoteAccessManager>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
73
			: $this->onDeny();
74
	}
75
76
	/**
77
	 * @return bool
78
	 */
79
	private function isAllowed() : bool
80
	{
81
		$addr = $this->request->getRemoteAddress() ?: php_uname('n');
82
		$secret = $this->request->getCookie($this->key);
83
84
		if ($this->isAllowedAll()) {
85
			$blacklist = is_string($this->blacklist)
86
				? preg_split('#[,\s]+#', $this->blacklist)
87
				: (array) $this->blacklist;
88
			$allow = !(in_array($addr, $blacklist, TRUE) || in_array("$secret@$addr", $blacklist, TRUE));
89
		} else {
90
			$whitelist = is_string($this->whitelist)
91
				? preg_split('#[,\s]+#', $this->whitelist)
92
				: (array) $this->whitelist;
93
			if (!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
94
				$whitelist[] = '127.0.0.1';
95
				$whitelist[] = '::1';
96
			}
97
			$allow = in_array($addr, $whitelist, TRUE) || in_array("$secret@$addr", $whitelist, TRUE);
98
		}
99
100
		return $allow;
101
	}
102
103
	/**
104
	 * @return bool
105
	 */
106
	private function isAllowedAll() : bool
107
	{
108
		return $this->mode === self::ALLOWED_ALL;
109
	}
110
}
111