Completed
Pull Request — master (#105)
by Tom
02:40
created

UserAgentUtil   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 57
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserAgent() 0 10 2
A shouldAttachmentBeCal() 0 10 2
A isIOS() 0 7 3
A isIOS7() 0 4 2
1
<?php
2
3
namespace JeroenDesloovere\VCard\Util;
4
5
/**
6
 * Class UserAgentUtil
7
 *
8
 * @package JeroenDesloovere\VCard\Util
9
 */
10
class UserAgentUtil
11
{
12
    /**
13
     * Returns the browser user agent string.
14
     *
15
     * @return string
16
     */
17
    public static function getUserAgent(): string
18
    {
19
        $browser = 'unknown';
20
21
        if (array_key_exists('HTTP_USER_AGENT', $_SERVER)) {
22
            $browser = strtolower($_SERVER['HTTP_USER_AGENT']);
23
        }
24
25
        return $browser;
26
    }
27
28
    /**
29
     * Checks if we should return vcard in cal wrapper
30
     *
31
     * @return bool
32
     */
33
    public static function shouldAttachmentBeCal(): bool
34
    {
35
        $browser = self::getUserAgent();
36
37
        $matches = [];
38
        preg_match('/os (\d+)_(\d+)/', $browser, $matches);
39
        $version = isset($matches[1]) ? ((int) $matches[1]) : 999;
40
41
        return ($version < 8);
42
    }
43
44
    /**
45
     * Is iOS - Check if the user is using an iOS-device
46
     *
47
     * @return bool
48
     */
49
    public static function isIOS(): bool
50
    {
51
        // get user agent
52
        $browser = self::getUserAgent();
53
54
        return (strpos($browser, 'iphone') || strpos($browser, 'ipod') || strpos($browser, 'ipad'));
55
    }
56
57
    /**
58
     * Is iOS less than 7 (should cal wrapper be returned)
59
     *
60
     * @return bool
61
     */
62
    public static function isIOS7(): bool
63
    {
64
        return (self::isIOS() && self::shouldAttachmentBeCal());
65
    }
66
}
67