Passed
Push — master ( df3782...6e3db9 )
by Moecasts
05:14 queued 51s
created

DeviceType::getType()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
ccs 7
cts 7
cp 1
crap 3
rs 10
1
<?php
2
3
namespace Moecasts\Laravel\UserLoginLog;
4
5
use Jenssegers\Agent\Agent;
6
7
class DeviceType
8
{
9
    const DESKTOP = 1;
10
    const TABLET = 2;
11
    const PHONE = 3;
12
    const ROBOT = 4;
13
14 2
    public static function getType(int $value): ?String
15
    {
16 2
        $class = new \ReflectionClass(get_called_class());
17
18 2
        $constants = $class->getConstants();
19
20 2
        foreach ($constants as $name => $val) {
21 2
            if ($val === $value) {
22 2
                return $name;
23
            }
24
        }
25
26 1
        return null;
27
    }
28
29 7
    public static function getValue(Agent $agent): ?int
30
    {
31 7
        if ($agent->isTablet()) {
32 1
            return DeviceType::TABLET;
33 7
        } else if ($agent->isPhone()) {
34 1
            return DeviceType::PHONE;
35 7
        } elseif ($agent->isRobot()) {
36 1
            return DeviceType::ROBOT;
37
        } else {
38 7
            return DeviceType::DESKTOP;
39
        }
40
    }
41
}
42