CableTvEnum::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 CableTvEnum
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 $tvs = [
21
        'dstv' => 'DStv',
22
        'gotv' => 'GOtv',
23
        'startimes' => 'Startimes',
24
    ];
25
26
    private $code, $name;
27
28
    private function __construct(string $code, string $name)
29
    {
30
        $this->code = $code;
31
        $this->name = $name;
32
    }
33
34
    public function getCode(): string
35
    {
36
        return $this->code;
37
    }
38
39
    public function getName(): string
40
    {
41
        return ucfirst($this->name);
42
    }
43
44
    public function toArray(): array
45
    {
46
        return ['code' => $this->getCode(), 'name' => $this->getName()];
47
    }
48
49
    /**
50
     * @param $code
51
     * @return CableTvEnum|null
52
     * @throws ClubKonnectErrorException
53
     */
54
    public static function getCableTv($code): ?CableTvEnum
55
    {
56
        $cleanedName = strtolower(trim($code));
57
        if (!key_exists($cleanedName, self::$tvs))
58
            throw new ClubKonnectErrorException("No Cable TV available with the code '$code'", 404);
59
        if (!key_exists($cleanedName, self::$cache))
60
            self::$cache[$cleanedName] = new CableTvEnum($cleanedName, self::$tvs[$cleanedName]);
61
        return self::$cache[$cleanedName];
62
    }
63
64
    public function __toString(): string
65
    {
66
        return $this->getName();
67
    }
68
}
69