Encryption   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 92.86%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 51
ccs 26
cts 28
cp 0.9286
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getEncryptionObject() 0 10 3
A createEncryptionObject() 0 9 2
A createClassName() 0 8 1
A getCipherMethods() 0 4 1
A listAvailableCiphers() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Encryption;
6
7
8
use Encryption\Cipher\ACipher;
9
use Encryption\Exceptions\CipherNotImplementedException;
10
use Encryption\Exceptions\InvalidCipherException;
11
12
class Encryption
0 ignored issues
show
Coding Style introduced by
Encryption does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
13
{
14
    public const DEFAULT_CIPHER = 'AES-256-CBC';
15
16 4
    public static function getEncryptionObject(?string $cipher = null): ACipher
17
    {
18 4
        $cipher = strtolower($cipher ?: static::DEFAULT_CIPHER);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $cipher. This often makes code more readable.
Loading history...
19 4
        $availableCiphers = static::getCipherMethods();
20 4
        if (!in_array($cipher, $availableCiphers, true)) {
21 2
            $message = sprintf('Invalid cipher selected [%s]', $cipher);
22 2
            throw new InvalidCipherException($message);
23
        }
24 2
        return static::createEncryptionObject($cipher);
25
    }
26
27 2
    protected static function createEncryptionObject(string $cipher): ACipher
28
    {
29 2
        $className = static::createClassName($cipher);
30 2
        if (!class_exists($className)) {
31
            $message = sprintf('Cipher [%s] has not been implemented yet', $cipher);
32
            throw new CipherNotImplementedException($message);
33
        }
34 2
        return new $className();
35
    }
36
37 4
    protected static function createClassName(string $cipher): string {
38 4
        $crypto = strtoupper(explode('-', $cipher)[0]);
39 4
        return sprintf('%s\%s\%s',
40 4
            'Encryption\Cipher',
41 4
            $crypto,
42 4
            str_replace('-', '', ucwords($cipher))
43
        );
44
    }
45
46 5
    public static function getCipherMethods(): array
47
    {
48 5
        return array_unique(array_map('strtolower', openssl_get_cipher_methods()));
49
    }
50
51 1
    public static function listAvailableCiphers(): array
52
    {
53 1
        $availableCiphers = [];
54 1
        foreach (static::getCipherMethods() as $cipher) {
55 1
            $className = static::createClassName($cipher);
56 1
            if (class_exists($className)) {
57 1
                $availableCiphers[] = $cipher;
58
            }
59
        }
60 1
        return $availableCiphers;
61
    }
62
}
63