Completed
Push — master ( b5ddf1...6d6217 )
by Iurii
01:10
created

Device::getDeviceType()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 16
nc 5
nop 0
dl 0
loc 28
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @package Device
5
 * @author Iurii Makukh
6
 * @copyright Copyright (c) 2017, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+
8
 */
9
10
namespace gplcart\modules\device;
11
12
use gplcart\core\Module;
13
14
/**
15
 * Main class for Device module
16
 */
17
class Device extends Module
18
{
19
20
    /**
21
     * Constructor
22
     */
23
    public function __construct()
24
    {
25
        parent::__construct();
26
    }
27
28
    /**
29
     * Implements hook "route.list"
30
     * @param array $routes
31
     */
32
    public function hookRouteList(array &$routes)
33
    {
34
        $routes['admin/module/settings/device'] = array(
35
            'access' => 'module_edit',
36
            'handlers' => array(
37
                'controller' => array('gplcart\\modules\\device\\controllers\\Settings', 'editSettings')
38
            )
39
        );
40
    }
41
42
    /**
43
     * Implements hook "theme"
44
     * @param \gplcart\core\Controller $controller
45
     */
46
    public function hookTheme($controller)
47
    {
48
        $device = $this->getDeviceType();
49
        $store_id = $controller->getStore('store_id');
50
        $settings = $this->config->module('device');
51
52
        if ($controller->isBackend()//
53
                || $device === 'desktop'//
54
                || empty($settings['theme'][$store_id][$device])) {
55
            return null;
56
        }
57
58
        $theme = $settings['theme'][$store_id][$device];
59
60
        if ($this->config->isEnabledModule($theme)) {
61
            $controller->setCurrentTheme($theme);
62
        }
63
    }
64
65
    /**
66
     * Implements hook "library.list"
67
     * @param array $libraries
68
     */
69
    public function hookLibraryList(array &$libraries)
70
    {
71
        $libraries['mobile_detect'] = array(
72
            'name' => 'Mobile Detect',
73
            'description' => 'A lightweight PHP class for detecting mobile devices',
74
            'url' => 'https://github.com/serbanghita/Mobile-Detect',
75
            'download' => 'https://github.com/serbanghita/Mobile-Detect/archive/2.8.25.zip',
76
            'type' => 'php',
77
            'version_source' => array(
78
                'file' => 'vendor/mobiledetect/mobiledetectlib/Mobile_Detect.php',
79
                'pattern' => '/.*VERSION.*(\\d+\\.+\\d+\\.+\\d+)/',
80
                'lines' => 100
81
            ),
82
            'module' => 'device',
83
            'files' => array(
84
                'vendor/mobiledetect/mobiledetectlib/Mobile_Detect.php'
85
            )
86
        );
87
    }
88
89
    /**
90
     * Implements hook "module.enable.after"
91
     */
92
    public function hookModuleEnableAfter()
93
    {
94
        $this->getLibrary()->clearCache();
95
    }
96
97
    /**
98
     * Implements hook "module.disable.after"
99
     */
100
    public function hookModuleDisableAfter()
101
    {
102
        $this->getLibrary()->clearCache();
103
    }
104
105
    /**
106
     * Implements hook "module.install.after"
107
     */
108
    public function hookModuleInstallAfter()
109
    {
110
        $this->getLibrary()->clearCache();
111
    }
112
113
    /**
114
     * Implements hook "module.uninstall.after"
115
     */
116
    public function hookModuleUninstallAfter()
117
    {
118
        $this->getLibrary()->clearCache();
119
    }
120
121
    /**
122
     * Returns a device type
123
     * @return string
124
     */
125
    protected function getDeviceType()
126
    {
127
        /* @var $session \gplcart\core\helpers\Session */
128
        $session = $this->getHelper('Session');
129
130
        $device = $session->get('device');
131
132
        if (empty($device)) {
133
134
            try {
135
                $detector = $this->getMobileDetectInstance();
136
            } catch (\Exception $ex) {
137
                return 'desktop';
138
            }
139
140
            if ($detector->isMobile()) {
141
                $device = 'mobile';
142
            } else if ($detector->isTablet()) {
143
                $device = 'tablet';
144
            } else {
145
                $device = 'desktop';
146
            }
147
148
            $session->set('device', $device);
149
        }
150
151
        return $device;
152
    }
153
154
    /**
155
     * Returns instance on detector class
156
     * @return \Mobile_Detect
157
     * @throws \InvalidArgumentException
158
     */
159
    protected function getMobileDetectInstance()
160
    {
161
        $this->getLibrary()->load('mobile_detect');
162
163
        if (!class_exists('Mobile_Detect')) {
164
            throw new \InvalidArgumentException('Class Mobile_Detect not forund');
165
        }
166
167
        return new \Mobile_Detect;
168
    }
169
170
}
171