DBClientSidePassword::Nice()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sunnysideup\PasswordSaver\Model\Fields;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\ORM\FieldType\DBVarchar;
7
use Sunnysideup\PasswordSaver\Form\ClientSidePasswordField;
8
use Sunnysideup\UUDI\Api\HashCreator;
0 ignored issues
show
Bug introduced by
The type Sunnysideup\UUDI\Api\HashCreator 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...
9
10
class DBClientSidePassword extends DBVarchar
11
{
12
    protected $fieldLength = 7;
13
14
    private static $casting = [
15
        'FormGet' => 'HTMLText',
16
        'FormSet' => 'HTMLText',
17
    ];
18
19
    public function __construct($name = null, $size = 7, $options = [])
20
    {
21
        if ((int) $size) {
22
            $this->fieldLength = (int) $size;
23
        }
24
        parent::__construct($name, $size, $options);
25
    }
26
27
    public static function get_unique_value(?int $size = 7): string
28
    {
29
        if (! $size) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $size of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
30
            $size = 7;
31
        }
32
33
        return HashCreator::generate_hash($size);
34
    }
35
36
    /**
37
     * Remove ugly parts of a url to make it nice.
38
     */
39
    public function Nice()
40
    {
41
        return $this->value;
42
    }
43
44
    /**
45
     * Scaffold the ExternalURLField for this ExternalURL.
46
     *
47
     * @param null|mixed $title
48
     * @param null|mixed $params
49
     */
50
    public function scaffoldFormField($title = null, $params = null)
51
    {
52
        return new ClientSidePasswordField($this->name, $title);
53
    }
54
55
    public function forTemplate()
56
    {
57
        if ($this->value) {
58
            return $this->Nice();
59
        }
60
61
        return '(no code available)';
62
    }
63
64
    /**
65
     * Saves this field to the given data object.
66
     *
67
     * @param DataObject $dataObject
68
     */
69
    public function saveInto($dataObject)
70
    {
71
        $fieldName = $this->name;
72
        if (empty($fieldName)) {
73
            throw new \BadMethodCallException(
74
                "DBField::saveInto() Called on a nameless '" . static::class . "' object"
75
            );
76
        }
77
        if (! $dataObject->{$fieldName}) {
78
            $dataObject->{$fieldName} = self::get_unique_value($this->fieldLength);
79
        }
80
        $length = strlen($dataObject->{$fieldName});
81
        if ($length < $this->fieldLength) {
82
            $dataObject->{$fieldName} .= self::get_unique_value($this->fieldLength - $length);
83
        }
84
        if ($length > $this->fieldLength) {
85
            $dataObject->{$fieldName} = substr($dataObject->{$fieldName}, 0, $this->fieldLength);
86
        }
87
    }
88
89
    public function FormGet(string $link, ?string $name = 'get'): string
90
    {
91
        return $this->formInner($link, $name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Sunnysideup\PasswordSave...dePassword::formInner() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        return $this->formInner($link, /** @scrutinizer ignore-type */ $name);
Loading history...
92
    }
93
94
    public function FormSet(string $link, ?string $name = 'set'): string
95
    {
96
        return $this->formInner($link, $name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Sunnysideup\PasswordSave...dePassword::formInner() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
        return $this->formInner($link, /** @scrutinizer ignore-type */ $name);
Loading history...
97
    }
98
99
    protected function formInner(string $link, string $name): string
100
    {
101
        return '
102
            <form method="post" action="' . $link . '" class="password-getter-setter-form" formtarget="_blank" target="_blank" rel="noreferrer noopener">
103
                <input type="hidden" value="' . $this->value . '" name="Code" />
104
                <button type="submit" />' . $name . '</button>
105
            </form>
106
        ';
107
    }
108
}
109