IsUniqueOperation::validate()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 33
Code Lines 16

Duplication

Lines 22
Ratio 66.67 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 22
loc 33
rs 8.439
cc 6
eloc 16
nc 18
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Icybee package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Icybee\Modules\Users\Operation;
13
14
use ICanBoogie\ErrorCollection;
15
use ICanBoogie\Operation;
16
use Icybee\Modules\Users\User;
17
18
/**
19
 * Checks whether the specified email or username is unique, as in _not already used_.
20
 */
21
class IsUniqueOperation extends Operation
22
{
23
	/**
24
	 * @inheritdoc
25
	 */
26
	protected function get_controls()
27
	{
28
		return [
29
30
			self::CONTROL_AUTHENTICATION => true
31
32
		] + parent::get_controls();
33
	}
34
35
	/**
36
	 * @inheritdoc
37
	 */
38
	protected function validate(ErrorCollection $errors)
39
	{
40
		$request = $this->request;
41
		$username = $request[User::USERNAME];
42
		$email = $request[User::EMAIL];
43
		$uid = $request[User::UID] ?: 0;
44
45 View Code Duplication
		if ($username)
46
		{
47
			if ($this->module->model->select('uid')->where('username = ? AND uid != ?', $username, $uid)->rc)
48
			{
49
				$errors->add(User::USERNAME, "This username is already used");
50
			}
51
		}
52
		else
53
		{
54
			unset($errors[User::USERNAME]);
55
		}
56
57 View Code Duplication
		if ($email)
58
		{
59
			if ($this->module->model->select('uid')->where('email = ? AND uid != ?', $email, $uid)->rc)
60
			{
61
				$errors->add(User::EMAIL, "This email is already used");
62
			}
63
		}
64
		else
65
		{
66
			unset($errors[User::EMAIL]);
67
		}
68
69
		return count($errors) == 0;
70
	}
71
72
	/**
73
	 * @inheritdoc
74
	 */
75
	protected function process()
76
	{
77
		return true;
78
	}
79
}
80