ErrorCodeManager::getTitle()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace App\Ship\Exceptions\Codes;
4
use App\Ship\Exceptions\InternalErrorException;
5
use Exception;
6
use ReflectionClass;
7
8
/**
9
 * Class ErrorCodeManager
10
 *
11
 * @author  Johannes Schobel <[email protected]>
12
 */
13
class ErrorCodeManager
14
{
15
    /**
16
     * @param array $error
17
     *
18
     * @return mixed
19
     */
20
    public static function getCode(array $error)
21
    {
22
        return self::getKeyFromArray($error, 'code', 0);
23
    }
24
25
    /**
26
     * @param array $error
27
     *
28
     * @return mixed
29
     */
30
    public static function getTitle(array $error)
31
    {
32
        return self::getKeyFromArray($error, 'title', '');
33
    }
34
35
    /**
36
     * @param array $error
37
     *
38
     * @return mixed
39
     */
40
    public static function getDescription(array $error)
41
    {
42
        return self::getKeyFromArray($error, 'description', '');
43
    }
44
45
    /**
46
     * Returns the value for a given key in the array or a default value
47
     *
48
     * @param array $error
49
     * @param       $key
50
     * @param       $default
51
     *
52
     * @return mixed
53
     */
54
    private static function getKeyFromArray(array $error, $key, $default)
55
    {
56
        return isset($error[$key]) ? $error[$key] : $default;
57
    }
58
59
    /**
60
     * Returns all "defined" CodeTables
61
     *
62
     * @return array
63
     */
64
    public static function getCodeTables()
65
    {
66
        $codeTables = [
67
            ApplicationErrorCodesTable::class,
68
            CustomErrorCodesTable::class,
69
        ];
70
71
        return $codeTables;
72
    }
73
74
    /**
75
     * Get all arrays for this one error code table
76
     *
77
     * @param $codeTable
78
     *
79
     * @return array
80
     * @throws InternalErrorException
81
     */
82
    public static function getErrorsForCodeTable($codeTable)
83
    {
84
        try {
85
            $class = new $codeTable;
86
        }
87
        catch (Exception $exception) {
88
            throw new InternalErrorException();
89
        }
90
91
        // now we need to get all errors (i.e., constants) from this class!
92
        $reflectionClass = new ReflectionClass($class);
93
        $constants = $reflectionClass->getConstants();
94
95
        return $constants;
96
    }
97
98
    /**
99
     * Get all errors across all defined error code tables
100
     *
101
     * @return array
102
     */
103
    public static function getErrorsForCodeTables()
104
    {
105
        $tables = self::getCodeTables();
106
107
        $result = [];
108
109
        foreach ($tables as $table) {
110
            $errors = self::getErrorsForCodeTable($table);
111
            $result = array_merge($result, $errors);
112
        }
113
114
        return $result;
115
    }
116
}