RecoveryTokenStatusType   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 64
rs 10
c 1
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getSQLDeclaration() 0 3 1
A convertToPHPValue() 0 19 6
A getName() 0 3 1
A convertToDatabaseValue() 0 21 6
1
<?php
2
3
/**
4
 * Copyright 2022 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in file comment
Loading history...
18
19
namespace Surfnet\StepupMiddleware\ApiBundle\Doctrine\Type;
20
21
use Doctrine\DBAL\Platforms\AbstractPlatform;
22
use Doctrine\DBAL\Types\ConversionException;
23
use Doctrine\DBAL\Types\Type;
24
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\RecoveryTokenStatus;
0 ignored issues
show
Bug introduced by
The type Surfnet\StepupMiddleware...lue\RecoveryTokenStatus was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
/**
27
 * Custom Doctrine Type for the four possible statuses a recovery token
28
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
29
class RecoveryTokenStatusType extends Type
30
{
31
    public const NAME = 'stepup_recovery_token_status';
32
33
    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
34
    {
35
        return $platform->getIntegerTypeDeclarationSQL($column);
36
    }
37
38
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $platform should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
39
     * @throws ConversionException
40
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
41
    public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): int
42
    {
43
        if (!$value instanceof RecoveryTokenStatus) {
44
            throw new ConversionException(
45
                sprintf(
46
                    "Encountered illegal recovery token status of type %s '%s', expected a RecoveryTokenStatus instance",
47
                    get_debug_type($value),
48
                    is_scalar($value) ? (string)$value : '',
49
                ),
50
            );
51
        }
52
53
        if (RecoveryTokenStatus::active()->equals($value)) {
54
            return 0;
55
        } elseif (RecoveryTokenStatus::revoked()->equals($value)) {
56
            return 10;
57
        } elseif (RecoveryTokenStatus::forgotten()->equals($value)) {
58
            return 20;
59
        }
60
61
        throw new ConversionException(sprintf("Encountered inconvertible second factor status '%s'", (string)$value));
62
    }
63
64
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $platform should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $value should have a doc-comment as per coding-style.
Loading history...
65
     * @throws ConversionException
66
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
67
    public function convertToPHPValue(mixed $value, AbstractPlatform $platform): RecoveryTokenStatus
68
    {
69
        if (is_scalar($value)) {
70
            $value = (string)$value;
71
        }
72
73
        if ($value === '0') {
74
            return RecoveryTokenStatus::active();
75
        } elseif ($value === '10') {
76
            return RecoveryTokenStatus::revoked();
77
        } elseif ($value === '20') {
78
            return RecoveryTokenStatus::forgotten();
79
        }
80
81
        throw new ConversionException(
82
            sprintf(
83
                "Encountered illegal recovery token status of type %s '%s', expected it to be one of [0,10,20]",
84
                get_debug_type($value),
85
                is_scalar($value) ? (string)$value : '',
86
            ),
87
        );
88
    }
89
90
    public function getName(): string
91
    {
92
        return self::NAME;
93
    }
94
}
95