NetworkEnum   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 54
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCode() 0 4 1
A getName() 0 4 1
A toArray() 0 4 1
A getNetwork() 0 10 3
A __toString() 0 4 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 NetworkEnum
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
        'mtn' => '01',
22
        'glo' => '02',
23
        'etisalat' => '03',
24
        'airtel' => '04',
25
    ];
26
27
    private $code, $name;
28
29
    private function __construct(string $code, string $name)
30
    {
31
        $this->code = $code;
32
        $this->name = $name;
33
    }
34
35
    public function getCode(): string
36
    {
37
        return $this->code;
38
    }
39
40
    public function getName(): string
41
    {
42
        return ucfirst($this->name);
43
    }
44
45
    public function toArray(): array
46
    {
47
        return ['code' => $this->getCode(), 'name' => $this->getName()];
48
    }
49
50
    /**
51
     * @param $name
52
     * @return NetworkEnum|null
53
     * @throws ClubKonnectErrorException
54
     */
55
    public static function getNetwork($name): ?NetworkEnum
56
    {
57
        $cleanedName = strtolower(trim($name));
58
        if (!key_exists($cleanedName, self::$telcoms))
59
            throw new ClubKonnectErrorException("No Telcom available with the name '$name'", 999);
60
        if (!key_exists($cleanedName, self::$cache)) {
61
            self::$cache[$cleanedName] = new NetworkEnum(self::$telcoms[$cleanedName], $cleanedName);
62
        }
63
        return self::$cache[$cleanedName];
64
    }
65
66
    public function __toString(): string
67
    {
68
        return $this->getName();
69
    }
70
}
71