Completed
Push — master ( 6565fb...b207da )
by Thomas
10:01
created

UserValidator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 9
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C getValidations() 0 34 8
A getCustomConstraints() 0 6 1
1
<?php
2
namespace keeko\core\validator;
3
4
use keeko\framework\validator\ModelValidator;
5
use keeko\framework\preferences\SystemPreferences;
6
7
class UserValidator extends ModelValidator {
8
	
9
	protected function getValidations() {
10
		$prefs = $this->service->getPreferenceLoader()->getSystemPreferences();
11
		
12
		$validations = [];
13
		
14
		if ($prefs->getUserLogin() != SystemPreferences::LOGIN_EMAIL) {
15
			$validations['user_name'] = [
16
				['constraint' => 'notnull'],
17
				['constraint' => 'unique-username']
18
			];
19
		}
20
		
21
		if ($prefs->getUserEmail() || $prefs->getUserLogin() != SystemPreferences::LOGIN_USERNAME) {
22
			$validations['email'] = [['constraint' => 'email']];
23
			if ($prefs->getUserLogin() != SystemPreferences::LOGIN_USERNAME) {
24
				$validations['email'][] = ['constraint' => 'unique-email'];
25
			}
26
		}
27
28
		if ($prefs->getUserNames() == SystemPreferences::VALUE_REQUIRED) {
29
			$validations['given_name'] = [['constraint' => 'notnull']];
30
			$validations['family_name'] = [['constraint' => 'notnull']];
31
		}
32
		
33
		if ($prefs->getUserBirth() == SystemPreferences::VALUE_REQUIRED) {
34
			$validations['birth'] = [['constraint' => 'required']];
35
		}
36
		
37
		if ($prefs->getUserSex() == SystemPreferences::VALUE_REQUIRED) {
38
			$validations['sex'] = [['constraint' => 'choice', 'choices' => [0, 1]]];
39
		}
40
		
41
		return $validations;
42
	}
43
	
44
	protected function getCustomConstraints() {
45
		return [
46
			'unique-username' => 'keeko\core\validator\constraints\UniqueUsername',
47
			'unique-email' => 'keeko\core\validator\constraints\UniqueEmail'
48
		];
49
	}
50
}