Completed
Push — master ( 76e7c4...f53b25 )
by Henry
14s queued 10s
created

DiscoEnum::getByUID()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
/**
3
 * Created By: Henry Ejemuta
4
 * PC: Enrico Systems
5
 * Project: laravel-clubkonnect
6
 * Company: Stimolive Technologies Limited
7
 * Class Name: NetworkEnum.php
8
 * Date Created: 5/14/21
9
 * Time Created: 10:47 AM
10
 */
11
12
namespace HenryEjemuta\LaravelClubKonnect\Enums;
13
14
15
use HenryEjemuta\LaravelClubKonnect\Exceptions\ClubKonnectErrorException;
16
17
class DiscoEnum
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
18
{
19
    private static $cache = [];
20
    private static $discos = [
21
        'ekedc' => ['code' => '01', 'name' => 'Eko Electric - EKEDC'],
22
        'ikedc' => ['code' => '02', 'name' => 'Ikeja Electric - IKEDC'],
23
        'aedc' => ['code' => '03', 'name' => 'Abuja Electric - AEDC'],
24
        'kedc' => ['code' => '04', 'name' => 'Kano Electric - KEDC'],
25
        'phedc' => ['code' => '05', 'name' => 'Porthacourt Electric - PHEDC'],
26
        'jedc' => ['code' => '06', 'name' => 'Jos Electric - JEDC'],
27
        'ibedc' => ['code' => '07', 'name' => 'Ibadan Electric - IBEDC'],
28
        'kaedc' => ['code' => '08', 'name' => 'Kaduna Elecdtric - KAEDC'],
29
        'eedc' => ['code' => '09', 'name' => 'Enugu Electric - EEDC'],
30
    ];
31
32
    private $disco;
33
    private $uid;
34
35
    private function __construct(string $uid, array $disco)
36
    {
37
        $this->uid = $uid;
38
        $this->disco = (object)$disco;
39
    }
40
41
    public function getUID(): string
42
    {
43
        return $this->disco->uid;
44
    }
45
46
    public function getCode(): string
47
    {
48
        return $this->disco->code;
49
    }
50
51
    public function getName(): string
52
    {
53
        return $this->disco->name;
54
    }
55
56
    public function toArray(): array
57
    {
58
        return [
59
            'uid' => $this->getUID(),
60
            'code' => $this->getCode(),
61
            'name' => $this->getName()
62
        ];
63
    }
64
65
    /**
66
     * @param $uid
67
     * @return DiscoEnum|null
68
     * @throws ClubKonnectErrorException
69
     */
70
    public static function getByUID($uid): ?DiscoEnum
71
    {
72
        $uid = trim("$uid");
73
        if (!key_exists($uid, self::$discos))
74
            throw new ClubKonnectErrorException("Not a valid ClubKonnect Disco", 999);
75
        if (!key_exists($uid, self::$cache))
76
            self::$cache[$uid] = new DiscoEnum($uid, self::$discos[$uid]);
77
        return self::$cache[$uid];
78
    }
79
80
    /**
81
     * @param $code
82
     * @return DiscoEnum|null
83
     * @throws ClubKonnectErrorException
84
     */
85
    public static function getByCode($code): ?DiscoEnum
86
    {
87
        $code = trim($code);
88
        if (!key_exists($code, self::$cache)) {
89
            $found = false;
90
            foreach (self::$discos as $idx => $disco) {
91
                if ($disco['code'] == $code) {
92
                    self::$cache[$code] = new DiscoEnum($idx, $disco);
93
                    $found = true;
94
                }
95
            }
96
            if (!$found) {
97
                throw new ClubKonnectErrorException("Not a valid ClubKonnect Disco", 999);
98
            }
99
        }
100
        return self::$cache[$code];
101
    }
102
103
    public function __toString(): string
104
    {
105
        return print_r($this->toArray(), true);
106
    }
107
}
108