Test Failed
Pull Request — main (#59)
by Dimitri
06:14
created

BaseHandler::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Security\Encryption\Handlers;
13
14
use BlitzPHP\Contracts\Security\EncrypterInterface;
15
use BlitzPHP\Utilities\String\Text;
16
17
/**
18
 * Classe de base pour les gestionnaires de chiffrement
19
 */
20
abstract class BaseHandler implements EncrypterInterface
21
{
22
    /**
23
     * Clé de démarrage
24
     */
25
    protected ?string $key = '';
26
27
    /**
28
     * Constructeur
29
     */
30
    public function __construct(?object $config = null)
31
    {
32
        $config ??= (object) config('encryption');
33
34
        // rendre les paramètres facilement accessibles
35
        foreach (get_object_vars($config) as $key => $value) {
36
            if (property_exists($this, $key)) {
37
                $this->{$key} = $value;
38
            } elseif (property_exists($this, $key = Text::camel($key))) {
39
                $this->{$key} = $value;
40
            }
41
        }
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47
    public function getKey(): string
48
    {
49
        return $this->key;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->key could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
50
    }
51
52
    /**
53
     * Byte-safe substr()
54
     */
55
    protected static function substr(string $str, int $start, ?int $length = null): string
56
    {
57
        return mb_substr($str, $start, $length, '8bit');
58
    }
59
60
    /**
61
     * Fourni un accès en lecture seule à certaines de nos propriétés
62
     *
63
     * @param string $key Nom de la propriete
64
     *
65
     * @return array|bool|int|string|null
66
     */
67
    public function __get($key)
68
    {
69
        if ($this->__isset($key)) {
70
            return $this->{$key};
71
        }
72
73
        return null;
74
    }
75
76
    /**
77
     * Assure la vérification de certaines de nos propriétés
78
     *
79
     * @param string $key Nom de la propriete
80
     */
81
    public function __isset($key): bool
82
    {
83
        return property_exists($this, $key);
84
    }
85
}
86