Completed
Push — master ( 2fef72...ca35b8 )
by Łukasz
23:27
created

UserConverter::toFieldDefinition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
10
11
use eZ\Publish\Core\Persistence\Legacy\Content\FieldValue\Converter;
12
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldDefinition;
13
use eZ\Publish\Core\Persistence\Legacy\Content\StorageFieldValue;
14
use eZ\Publish\SPI\Persistence\Content\FieldValue;
15
use eZ\Publish\SPI\Persistence\Content\Type\FieldDefinition;
16
17
class UserConverter implements Converter
18
{
19
    private const PASSWORD_VALIDATOR_IDENTIFIER = 'PasswordValueValidator';
20
21
    private const REQUIRE_AT_LEAST_ONE_UPPER_CASE_CHAR = 1;
22
    private const REQUIRE_AT_LEAST_ONE_LOWER_CASE_CHAR = 2;
23
    private const REQUIRE_AT_LEAST_ONE_NUMERIC_CHAR = 4;
24
    private const REQUIRE_AT_LEAST_ONE_NON_ALPHANUMERIC_CHAR = 8;
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function toStorageValue(FieldValue $value, StorageFieldValue $storageFieldValue): void
30
    {
31
        // There is no contained data. All data is external. So we just do nothing here.
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function toFieldValue(StorageFieldValue $value, FieldValue $fieldValue): void
38
    {
39
        // There is no contained data. All data is external. So we just do nothing here.
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function toStorageFieldDefinition(FieldDefinition $fieldDef, StorageFieldDefinition $storageDef): void
46
    {
47
        $validatorParameters = [];
48
        if (isset($fieldDef->fieldTypeConstraints->validators[self::PASSWORD_VALIDATOR_IDENTIFIER])) {
49
            $validatorParameters = $fieldDef->fieldTypeConstraints->validators[self::PASSWORD_VALIDATOR_IDENTIFIER];
50
        }
51
52
        $rules = [
53
            'requireAtLeastOneUpperCaseCharacter' => self::REQUIRE_AT_LEAST_ONE_UPPER_CASE_CHAR,
54
            'requireAtLeastOneLowerCaseCharacter' => self::REQUIRE_AT_LEAST_ONE_LOWER_CASE_CHAR,
55
            'requireAtLeastOneNumericCharacter' => self::REQUIRE_AT_LEAST_ONE_NUMERIC_CHAR,
56
            'requireAtLeastOneNonAlphanumericCharacter' => self::REQUIRE_AT_LEAST_ONE_NON_ALPHANUMERIC_CHAR,
57
        ];
58
59
        $storageDef->dataInt1 = 0;
60
        foreach ($rules as $rule => $flag) {
61
            if (isset($validatorParameters[$rule]) && $validatorParameters[$rule]) {
62
                $storageDef->dataInt1 |= $flag;
63
            }
64
        }
65
66
        $storageDef->dataInt2 = null;
67
        if (isset($validatorParameters['minLength'])) {
68
            $storageDef->dataInt2 = $validatorParameters['minLength'];
69
        }
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function toFieldDefinition(StorageFieldDefinition $storageDef, FieldDefinition $fieldDef): void
76
    {
77
        $validatorParameters = [];
78
79
        $rules = [
80
            self::REQUIRE_AT_LEAST_ONE_UPPER_CASE_CHAR => 'requireAtLeastOneUpperCaseCharacter',
81
            self::REQUIRE_AT_LEAST_ONE_LOWER_CASE_CHAR => 'requireAtLeastOneLowerCaseCharacter',
82
            self::REQUIRE_AT_LEAST_ONE_NUMERIC_CHAR => 'requireAtLeastOneNumericCharacter',
83
            self::REQUIRE_AT_LEAST_ONE_NON_ALPHANUMERIC_CHAR => 'requireAtLeastOneNonAlphanumericCharacter',
84
        ];
85
86
        foreach ($rules as $flag => $rule) {
87
            $validatorParameters[$rule] = (bool) ($storageDef->dataInt1 & $flag);
88
        }
89
90
        $validatorParameters['minLength'] = $storageDef->dataInt2;
91
92
        $fieldDef->fieldTypeConstraints->validators[self::PASSWORD_VALIDATOR_IDENTIFIER] = $validatorParameters;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getIndexColumn(): bool
99
    {
100
        return false;
101
    }
102
}
103