1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CrossKnowledge\DeviceDetectBundle\Services; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
6
|
|
|
use \Doctrine\Common\Cache\CacheProvider; |
7
|
|
|
|
8
|
|
|
class DeviceDetect |
9
|
|
|
{ |
10
|
|
|
/** @var RequestStack */ |
11
|
|
|
protected $requestStack; |
12
|
|
|
|
13
|
|
|
/** @var \DeviceDetector\DeviceDetector */ |
14
|
|
|
protected $deviceDetector; |
15
|
|
|
|
16
|
|
|
/** @var Cache */ |
17
|
|
|
protected $cacheManager; |
18
|
|
|
|
19
|
|
|
/** @var [] */ |
20
|
|
|
protected $deviceDetectorOptions; |
21
|
|
|
|
22
|
|
|
public function __construct(RequestStack $stack, CacheProvider $cache, $deviceDetectorOptions) |
23
|
|
|
{ |
24
|
|
|
$this->cacheManager = $cache; |
|
|
|
|
25
|
|
|
$this->requestStack = $stack; |
26
|
|
|
$this->setOptions($deviceDetectorOptions); |
27
|
|
|
|
28
|
|
|
if (null !== $this->requestStack && $this->requestStack->getCurrentRequest()) { |
29
|
|
|
$userAgent = $this->requestStack->getCurrentRequest()->headers->get('User-Agent'); |
30
|
|
|
} else { |
31
|
|
|
$userAgent = ''; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->deviceDetector = new \DeviceDetector\DeviceDetector($userAgent); |
35
|
|
|
$this->deviceDetector->setCache($this->getCacheManager()); |
36
|
|
|
|
37
|
|
|
if (!empty($this->deviceDetectorOptions['discard_bot_information'])) { |
38
|
|
|
$this->deviceDetector->discardBotInformation(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!empty($this->deviceDetectorOptions['skip_bot_detection'])) { |
42
|
|
|
$this->deviceDetector->skipBotDetection(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->deviceDetector->parse(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function isTablet() |
49
|
|
|
{ |
50
|
|
|
return $this->deviceDetector->isTablet(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function isMobile() |
54
|
|
|
{ |
55
|
|
|
if ($this->deviceDetector->isTablet()) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->deviceDetector->isMobile(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function isDesktop() |
63
|
|
|
{ |
64
|
|
|
return $this->deviceDetector->isDesktop(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getCacheManager() |
68
|
|
|
{ |
69
|
|
|
return $this->cacheManager; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function setOptions($deviceDetectorOptions) |
73
|
|
|
{ |
74
|
|
|
$this->deviceDetectorOptions = $deviceDetectorOptions; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getDeviceDetector() |
78
|
|
|
{ |
79
|
|
|
return $this->deviceDetector; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..