ExceptionFactory::__debugInfo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 18
ccs 17
cts 17
cp 1
rs 9.7333
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * Factory object for framework exception instancing.
5
 */
6
7
namespace CryptoManana\Factories;
8
9
use CryptoManana\Core\Abstractions\DesignPatterns\AbstractFactory as FactoryPattern;
10
use CryptoManana\Core\Abstractions\ErrorHandling\AbstractException as FrameworkException;
11
use CryptoManana\Exceptions\BadPracticeException as BadPractice;
12
use CryptoManana\Exceptions\CryptographyException as CryptographyProblem;
13
use CryptoManana\Exceptions\IncompatibleException as BackwardIncompatible;
14
use CryptoManana\Exceptions\UnsupportedException as UnsupportedAlgorithm;
15
use CryptoManana\Exceptions\AccessDeniedException as AccessDenied;
16
use CryptoManana\Exceptions\BreachAttemptException as BreachAttempt;
17
use CryptoManana\Exceptions\MaliciousPayloadException as MaliciousPayload;
18
use CryptoManana\Exceptions\BotDetectedException as BotDetected;
19
use CryptoManana\Exceptions\IdentificationFailureException as IdentificationFailure;
20
use CryptoManana\Exceptions\AuthenticationFailureException as AuthenticationFailure;
21
use CryptoManana\Exceptions\AuthorizationFailureException as AuthorizationFailure;
22
use CryptoManana\Exceptions\SessionExpiredException as SessionExpired;
23
use CryptoManana\Exceptions\TokenExpiredException as TokenExpired;
24
use CryptoManana\Exceptions\WrongConfigurationException as WrongConfiguration;
25
use CryptoManana\Exceptions\InsecureUsageException as InsecureUsage;
26
27
/**
28
 * Class ExceptionFactory - Factory object for framework exception instancing.
29
 *
30
 * @package CryptoManana\Factories
31
 */
32
class ExceptionFactory extends FactoryPattern
33
{
34
    /**
35
     * The `cryptography` exception type.
36
     */
37
    const CRYPTOGRAPHY_PROBLEM = CryptographyProblem::class;
38
39
    /**
40
     * The `bad practice` exception type.
41
     */
42
    const BAD_PRACTICE = BadPractice::class;
43
44
    /**
45
     * The `unsupported algorithm` exception type.
46
     */
47
    const UNSUPPORTED_ALGORITHM = UnsupportedAlgorithm::class;
48
49
    /**
50
     * The `backward incompatible` exception type.
51
     */
52
    const BACKWARD_INCOMPATIBLE = BackwardIncompatible::class;
53
54
    /**
55
     * The `access denied` exception type.
56
     */
57
    const ACCESS_DENIED = AccessDenied::class;
58
59
    /**
60
     * The `breach attempt` exception type.
61
     */
62
    const BREACH_ATTEMPT = BreachAttempt::class;
63
64
    /**
65
     * The `malicious payload` exception type.
66
     */
67
    const MALICIOUS_PAYLOAD = MaliciousPayload::class;
68
69
    /**
70
     * The `bot detected` exception type.
71
     */
72
    const BOT_DETECTED = BotDetected::class;
73
74
    /**
75
     * The `identification failure` exception type.
76
     */
77
    const IDENTIFICATION_FAILURE = IdentificationFailure::class;
78
79
    /**
80
     * The `authentication failure` exception type.
81
     */
82
    const AUTHENTICATION_FAILURE = AuthenticationFailure::class;
83
84
    /**
85
     * The `authorization failure` exception type.
86
     */
87
    const AUTHORIZATION_FAILURE = AuthorizationFailure::class;
88
89
    /**
90
     * The `session expired` exception type.
91
     */
92
    const SESSION_EXPIRED = SessionExpired::class;
93
94
    /**
95
     * The `token expired` exception type.
96
     */
97
    const TOKEN_EXPIRED = TokenExpired::class;
98
99
    /**
100
     * The `wrong configuration` exception type.
101
     */
102
    const WRONG_CONFIGURATION = WrongConfiguration::class;
103
104
    /**
105
     * The `insecure usage` exception type.
106
     */
107
    const INSECURE_USAGE = InsecureUsage::class;
108
109
    /**
110
     * Get debug information for the class instance.
111
     *
112
     * @return array Debug information.
113
     */
114 1
    public function __debugInfo()
115
    {
116 1
        return [
117 1
            self::class . '::BAD_PRACTICE' => self::BAD_PRACTICE,
118 1
            self::class . '::CRYPTOGRAPHY_PROBLEM' => self::CRYPTOGRAPHY_PROBLEM,
119 1
            self::class . '::BACKWARD_INCOMPATIBLE' => self::BACKWARD_INCOMPATIBLE,
120 1
            self::class . '::UNSUPPORTED_ALGORITHM' => self::UNSUPPORTED_ALGORITHM,
121 1
            self::class . '::ACCESS_DENIED' => self::ACCESS_DENIED,
122 1
            self::class . '::BREACH_ATTEMPT' => self::BREACH_ATTEMPT,
123 1
            self::class . '::MALICIOUS_PAYLOAD' => self::MALICIOUS_PAYLOAD,
124 1
            self::class . '::BOT_DETECTED' => self::BOT_DETECTED,
125 1
            self::class . '::IDENTIFICATION_FAILURE' => self::IDENTIFICATION_FAILURE,
126 1
            self::class . '::AUTHENTICATION_FAILURE' => self::AUTHENTICATION_FAILURE,
127 1
            self::class . '::AUTHORIZATION_FAILURE' => self::AUTHORIZATION_FAILURE,
128 1
            self::class . '::SESSION_EXPIRED' => self::SESSION_EXPIRED,
129 1
            self::class . '::TOKEN_EXPIRED' => self::TOKEN_EXPIRED,
130 1
            self::class . '::WRONG_CONFIGURATION' => self::WRONG_CONFIGURATION,
131 1
            self::class . '::INSECURE_USAGE' => self::INSECURE_USAGE,
132 1
        ];
133
    }
134
135
    /**
136
     * Create a framework exception.
137
     *
138
     * @param string|null $type The exception class name as type for creation.
139
     *
140
     * @return FrameworkException|object|null An exception object or null.
141
     */
142 3
    public function create($type)
143
    {
144 3
        return self::createInstance($type);
145
    }
146
147
    /**
148
     * Create a framework exception.
149
     *
150
     * @param string|null $type The exception class name as type for creation.
151
     *
152
     * @return FrameworkException|object|null An exception object or null.
153
     */
154 5
    public static function createInstance($type)
155
    {
156
        /**
157
         * Check if class exists and has a correct base class
158
         *
159
         * @var FrameworkException|null $exception Object instance.
160
         */
161 5
        if (class_exists($type) && is_subclass_of($type, FrameworkException::class)) {
162 5
            $exception = new $type();
163
        } else {
164 2
            $exception = null; // Invalid type given
165
        }
166
167 5
        return $exception;
168
    }
169
}
170