MeterTypeEnum::getCode()   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
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 MeterTypeEnum
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 $telcoms = [
21
        'prepaid' => '01',
22
        'postpaid' => '02',
23
    ];
24
25
    private $code, $name;
26
27
    private function __construct(string $code, string $name)
28
    {
29
        $this->code = $code;
30
        $this->name = $name;
31
    }
32
33
    public function getCode(): string
34
    {
35
        return $this->code;
36
    }
37
38
    public function getName(): string
39
    {
40
        return ucfirst($this->name);
41
    }
42
43
    public function toArray(): array
44
    {
45
        return ['code' => $this->getCode(), 'name' => $this->getName()];
46
    }
47
48
    /**
49
     * @param $name
50
     * @return MeterTypeEnum|null
51
     * @throws ClubKonnectErrorException
52
     */
53
    public static function getMeterType($name): ?MeterTypeEnum
54
    {
55
        $cleanedName = strtolower(trim($name));
56
        if (!key_exists($cleanedName, self::$telcoms))
57
            throw new ClubKonnectErrorException("No Meter Type available with the name '$name'", 999);
58
        if (!key_exists($cleanedName, self::$cache)) {
59
            self::$cache[$cleanedName] = new MeterTypeEnum(self::$telcoms[$cleanedName], $cleanedName);
60
        }
61
        return self::$cache[$cleanedName];
62
    }
63
64
    public function __toString(): string
65
    {
66
        return $this->getName();
67
    }
68
}
69