UsrAuthFactory::getPost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Фабрика. Содержит алогритм выбора стратегии авторизации
4
 *
5
 * @file      UserAuthFactory.php
6
 *
7
 * PHP version 8.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2021 Alexander Yancharuk
11
 * @date      Вск Янв 27 17:34:29 2013
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>.
14
 */
15
16
namespace Veles\Auth;
17
18
use Veles\Auth\Strategies\AbstractAuthStrategy;
19
use Veles\Auth\Strategies\CookieStrategy;
20
use Veles\Auth\Strategies\GuestStrategy;
21
use Veles\Auth\Strategies\LoginFormStrategy;
22
use Veles\Model\User;
23
24
/**
25
 * Class UserAuthFactory
26
 *
27
 * @author  Alexander Yancharuk <alex at itvault dot info>
28
 */
29
class UsrAuthFactory
30
{
31
	protected $cookie_definitions = [
32
		'id' => [
33
			'filter'  => FILTER_VALIDATE_INT,
34
			'options' => [
35
				'min_range' => 1,
36
				'max_range' => PHP_INT_MAX
37
			]
38
		],
39
		'pw' => [
40
			'filter'  => FILTER_VALIDATE_REGEXP,
41
			'options' => [
42
				'regexp' => '/^.{31}$/'
43
			]
44
		]
45
	];
46
	protected $post_definitions = [
47
		'ln' => [
48
			'filter' => FILTER_VALIDATE_EMAIL,
49
		],
50
		'pw' => [
51
			'filter'  => FILTER_VALIDATE_REGEXP,
52
			'options' => [
53
				'regexp' => '/^[a-z0-9_-]{1,20}$/i'
54
			]
55
		]
56
	];
57
58
	/**
59
	 * Algorithm for choosing auth strategy
60
	 *
61
	 * @return AbstractAuthStrategy
62
	 */
63 2
	public function create()
64
	{
65 2
		$post    = $this->getPost();
66 2
		$cookies = $this->getCookies();
67
68
		switch (true) {
69 2
			case (isset($post['ln'], $post['pw'])):
70 1
				$auth = new LoginFormStrategy($post['ln'], $post['pw'], new User);
71 1
				$auth->errorHandle($post);
72
73 1
				break;
74 2
			case (isset($cookies['id'], $cookies['pw'])):
75 1
				$auth = new CookieStrategy($cookies['id'], $cookies['pw'], new User);
76 1
				$auth->errorHandle($cookies);
77
78 1
				break;
79
			default:
80 2
				$auth = new GuestStrategy(new User);
81
		}
82
83 2
		return $auth;
84
	}
85
86
	/**
87
	 * @codeCoverageIgnore
88
	 * @return mixed
89
	 */
90
	protected function getCookies()
91
	{
92
		$cookies = filter_input_array(INPUT_COOKIE, $this->cookie_definitions);
93
94
		return $cookies;
95
	}
96
97
	/**
98
	 * @codeCoverageIgnore
99
	 * @return mixed
100
	 */
101
	protected function getPost()
102
	{
103
		$post = filter_input_array(INPUT_POST, $this->post_definitions);
104
105
		return $post;
106
	}
107
}
108