SymmetricCipherFactory   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 24
c 1
b 0
f 0
dl 0
loc 94
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createInstance() 0 14 3
A create() 0 3 1
A __debugInfo() 0 11 1
1
<?php
2
3
/**
4
 * Factory for object symmetric encryption algorithm object instancing.
5
 */
6
7
namespace CryptoManana\Factories;
8
9
use CryptoManana\Core\Abstractions\DesignPatterns\AbstractFactory as FactoryPattern;
10
use CryptoManana\Core\Abstractions\MessageEncryption\AbstractSymmetricEncryptionAlgorithm as EncryptionAlgorithm;
11
use CryptoManana\SymmetricEncryption\Aes128 as Aes128;
12
use CryptoManana\SymmetricEncryption\Aes192 as Aes192;
13
use CryptoManana\SymmetricEncryption\Aes256 as Aes256;
14
use CryptoManana\SymmetricEncryption\Camellia128 as Camellia128;
15
use CryptoManana\SymmetricEncryption\Camellia192 as Camellia192;
16
use CryptoManana\SymmetricEncryption\Camellia256 as Camellia256;
17
use CryptoManana\SymmetricEncryption\TripleDes as TripleDes;
18
use CryptoManana\SymmetricEncryption\Rc4 as Rc4;
19
20
/**
21
 * Class SymmetricCipherFactory - Factory for object symmetric encryption algorithm object instancing.
22
 *
23
 * @package CryptoManana\Factories
24
 */
25
class SymmetricCipherFactory extends FactoryPattern
26
{
27
    /**
28
     * The AES-128 type.
29
     */
30
    const AES_128 = Aes128::class;
31
32
    /**
33
     * The AES-192 type.
34
     */
35
    const AES_192 = Aes192::class;
36
37
    /**
38
     * The AES-256 type.
39
     */
40
    const AES_256 = Aes256::class;
41
42
    /**
43
     * The CAMELLIA-128 type.
44
     */
45
    const CAMELLIA_128 = Camellia128::class;
46
47
    /**
48
     * The CAMELLIA-192 type.
49
     */
50
    const CAMELLIA_192 = Camellia192::class;
51
52
    /**
53
     * The CAMELLIA-256 type.
54
     */
55
    const CAMELLIA_256 = Camellia256::class;
56
57
    /**
58
     * The 3DES-168 type.
59
     */
60
    const TRIPLE_DES_168 = TripleDes::class;
61
62
    /**
63
     * The RC4-128 type.
64
     */
65
    const RC4_128 = Rc4::class;
66
67
    /**
68
     * Get debug information for the class instance.
69
     *
70
     * @return array Debug information.
71
     */
72 1
    public function __debugInfo()
73
    {
74 1
        return [
75 1
            self::class . '::AES_128' => self::AES_128,
76 1
            self::class . '::AES_192' => self::AES_192,
77 1
            self::class . '::AES_256' => self::AES_256,
78 1
            self::class . '::CAMELLIA_128' => self::CAMELLIA_128,
79 1
            self::class . '::CAMELLIA_192' => self::CAMELLIA_192,
80 1
            self::class . '::CAMELLIA_256' => self::CAMELLIA_256,
81 1
            self::class . '::TRIPLE_DES_168' => self::TRIPLE_DES_168,
82 1
            self::class . '::RC4_128' => self::RC4_128,
83 1
        ];
84
    }
85
86
    /**
87
     * Create an encryption algorithm object
88
     *
89
     * @param string|null $type The algorithm class name as type for creation.
90
     *
91
     * @return EncryptionAlgorithm|object|null An encryption algorithm object or null.
92
     */
93 3
    public function create($type)
94
    {
95 3
        return self::createInstance($type);
96
    }
97
98
    /**
99
     * Create an encryption algorithm object
100
     *
101
     * @param string|null $type The algorithm class name as type for creation.
102
     *
103
     * @return EncryptionAlgorithm|object|null An encryption algorithm object or null.
104
     */
105 4
    public static function createInstance($type)
106
    {
107
        /**
108
         * Check if class exists and has a correct base class
109
         *
110
         * @var EncryptionAlgorithm|null $exception Object instance.
111
         */
112 4
        if (class_exists($type) && is_subclass_of($type, EncryptionAlgorithm::class)) {
113 4
            $exception = new $type();
114
        } else {
115 2
            $exception = null; // Invalid type given
116
        }
117
118 4
        return $exception;
119
    }
120
}
121