Passed
Push — main ( d71b68...a743f7 )
by Dimitri
08:12 queued 04:09
created

BaseHandler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 12
c 2
b 1
f 0
dl 0
loc 62
ccs 7
cts 7
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 3
A substr() 0 3 1
A __get() 0 7 2
A __isset() 0 3 1
A getKey() 0 3 1
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
16
/**
17
 * Classe de base pour les gestionnaires de chiffrement
18
 */
19
abstract class BaseHandler implements EncrypterInterface
20
{
21
    /**
22
     * Clé de démarrage
23
     */
24
    protected string $key = '';
25
26
    /**
27
     * Constructeur
28
     */
29
    public function __construct(?object $config = null)
30
    {
31 6
        $config ??= (object) config('encryption');
32
33
        // rendre les paramètres facilement accessibles
34
        foreach (get_object_vars($config) as $key => $value) {
35
            if (property_exists($this, $key)) {
36 6
                $this->{$key} = $value;
37
            }
38
        }
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function getKey(): string
45
    {
46 2
        return $this->key;
47
    }
48
49
    /**
50
     * Byte-safe substr()
51
     */
52
    protected static function substr(string $str, int $start, ?int $length = null): string
53
    {
54 6
        return mb_substr($str, $start, $length, '8bit');
55
    }
56
57
    /**
58
     * Fourni un accès en lecture seule à certaines de nos propriétés
59
     *
60
     * @param string $key Nom de la propriete
61
     *
62
     * @return array|bool|int|string|null
63
     */
64
    public function __get($key)
65
    {
66
        if ($this->__isset($key)) {
67 4
            return $this->{$key};
68
        }
69
70 2
        return null;
71
    }
72
73
    /**
74
     * Assure la vérification de certaines de nos propriétés
75
     *
76
     * @param string $key Nom de la propriete
77
     */
78
    public function __isset($key): bool
79
    {
80 4
        return property_exists($this, $key);
81
    }
82
}
83