Passed
Push — master ( f0334d...12f38b )
by Moecasts
07:32
created

DeviceType::getValue()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 13
ccs 9
cts 10
cp 0.9
crap 5.025
rs 9.6111
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
        return null;
27
    }
28
29 7
    public static function getValue(Agent $agent): ?int
30
    {
31 7
        if ($agent->isDesktop()) {
32 7
            return DeviceType::DESKTOP;
33 1
        } else if ($agent->isTablet()) {
34 1
            return DeviceType::TABLET;
35 1
        } else if ($agent->isPhone()) {
36 1
            return DeviceType::PHONE;
37 1
        } elseif ($agent->isRobot()) {
38 1
            return DeviceType::ROBOT;
39
        }
40
41
        return null;
42
    }
43
}
44