Completed
Push — master ( 72403c...256e3e )
by Tony Karavasilev (Тони
17:00
created

SymmetricCipherFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 20
c 1
b 0
f 0
dl 0
loc 81
ccs 14
cts 14
cp 1
rs 10

3 Methods

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