Completed
Push — master ( cf5944...0d6816 )
by Alexander
05:51 queued 02:56
created

UsrAuthFactory::create()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 0
dl 0
loc 25
ccs 15
cts 15
cp 1
crap 3
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Фабрика. Содержит алогритм выбора стратегии авторизации
4
 *
5
 * @file      UserAuthFactory.php
6
 *
7
 * PHP version 7.0+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2018 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 2
				$auth = new LoginFormStrategy(
71 2
					$post['ln'], $post['pw'], new User
72
				);
73 2
				$auth->errorHandle($post);
74
75 2
				break;
76 2
			case (isset($cookies['id'], $cookies['pw'])):
77 2
				$auth = new CookieStrategy(
78 2
					$cookies['id'], $cookies['pw'], new User
79
				);
80 2
				$auth->errorHandle($cookies);
81
82 2
				break;
83
			default:
84 2
				$auth = new GuestStrategy(new User);
85
		}
86
87 2
		return $auth;
88
	}
89
90
	/**
91
	 * @codeCoverageIgnore
92
	 * @return mixed
93
	 */
94
	protected function getCookies()
95
	{
96
		$cookies = filter_input_array(INPUT_COOKIE, $this->cookie_definitions);
97
98
		return $cookies;
99
	}
100
101
	/**
102
	 * @codeCoverageIgnore
103
	 * @return mixed
104
	 */
105
	protected function getPost()
106
	{
107
		$post = filter_input_array(INPUT_POST, $this->post_definitions);
108
109
		return $post;
110
	}
111
}
112