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

DeviceType::getValue()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 4
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