Completed
Push — development ( dff276...1af152 )
by Alexander
03:35
created

UsrAuthFactory::create()   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 27
Code Lines 20

Duplication

Lines 14
Ratio 51.85 %

Code Coverage

Tests 23
CRAP Score 7

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 14
loc 27
ccs 23
cts 23
cp 1
rs 6.7272
cc 7
eloc 20
nc 9
nop 0
crap 7
1
<?php
2
/**
3
 * Фабрика. Содержит алогритм выбора стратегии авторизации
4
 *
5
 * @file      UserAuthFactory.php
6
 *
7
 * PHP version 5.6+
8
 *
9
 * @author    Alexander Yancharuk <alex at itvault dot info>
10
 * @copyright © 2012-2016 Alexander Yancharuk <alex at itvault at info>
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
	 * @todo Move validation and error handling into separate class
62
	 * @return AbstractAuthStrategy
63
	 */
64 1
	public function create()
65
	{
66 1
		$post    = $this->getPost();
67 1
		$cookies = $this->getCookies();
68 1
		$auth    = null;
0 ignored issues
show
Unused Code introduced by
$auth is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
70 1
		switch (true) {
71 1 View Code Duplication
			case (isset($post['ln'], $post['pw'])):
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72 1
				$auth = new LoginFormStrategy(
73 1
					$post['ln'], $post['pw'], new User
74 1
				);
75 1
				$post['ln'] || $auth->setError($auth::ERR_NOT_VALID_LOGIN);
76 1
				$post['pw'] || $auth->setError($auth::ERR_NOT_VALID_PASSWORD);
77 1
				break;
78 1 View Code Duplication
			case (isset($cookies['id'], $cookies['pw'])):
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79 1
				$auth = new CookieStrategy(
80 1
					$cookies['id'], $cookies['pw'], new User
81 1
				);
82 1
				$cookies['id'] || $auth->setError($auth::ERR_NOT_VALID_UID);
83 1
				$cookies['pw'] || $auth->setError($auth::ERR_NOT_VALID_HASH);
84 1
				break;
85 1
			default:
86 1
				$auth = new GuestStrategy(new User);
87 1
		}
88
89 1
		return $auth;
90
	}
91
92
	/**
93
	 * @codeCoverageIgnore
94
	 * @return mixed
95
	 */
96
	protected function getCookies()
97
	{
98
		$cookies = filter_input_array(INPUT_COOKIE, $this->cookie_definitions);
99
100
		return $cookies;
101
	}
102
103
	/**
104
	 * @codeCoverageIgnore
105
	 * @return mixed
106
	 */
107
	protected function getPost()
108
	{
109
		$post = filter_input_array(INPUT_POST, $this->post_definitions);
110
111
		return $post;
112
	}
113
}
114