Completed
Push — master ( a8a40f...ce4bfe )
by Iurii
05:51
created

Main::addLibrary()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 1
1
<?php
2
3
/**
4
 * @package Code Mirror
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\codemirror;
11
12
use gplcart\core\Controller;
13
use gplcart\core\Library;
14
use gplcart\core\Module;
15
use InvalidArgumentException;
16
use OutOfRangeException;
17
18
/**
19
 * Main class for Code Mirror module
20
 */
21
class Main
22
{
23
24
    /**
25
     * Module class instance
26
     * @var \gplcart\core\Module $module
27
     */
28
    protected $module;
29
30
    /**
31
     * Library class instance
32
     * @var \gplcart\core\Library $library
33
     */
34
    protected $library;
35
36
    /**
37
     * @param Module $module
38
     * @param Library $library
39
     */
40
    public function __construct(Module $module, Library $library)
41
    {
42
        $this->module = $module;
43
        $this->library = $library;
44
    }
45
46
    /**
47
     * Implements hook "library.list"
48
     * @param array $libraries
49
     */
50
    public function hookLibraryList(array &$libraries)
51
    {
52
        $libraries['codemirror'] = array(
53
            'name' => 'Codemirror', // @text
54
            'description' => 'In-browser code editor', // @text
55
            'type' => 'asset',
56
            'module' => 'codemirror',
57
            'url' => 'https://codemirror.net',
58
            'download' => 'https://github.com/codemirror/CodeMirror/archive/5.19.0.zip',
59
            'version' => '5.19.0',
60
            'files' => array(
61
                'lib/codemirror.js',
62
                'lib/codemirror.css',
63
            ),
64
        );
65
    }
66
67
    /**
68
     * Implements hook "route.list"
69
     * @param array $routes
70
     */
71
    public function hookRouteList(array &$routes)
72
    {
73
        $routes['admin/module/settings/codemirror'] = array(
74
            'access' => 'module_edit',
75
            'handlers' => array(
76
                'controller' => array('gplcart\\modules\\codemirror\\controllers\\Settings', 'editSettings')
77
            )
78
        );
79
    }
80
81
    /**
82
     * Implements hook "module.enable.after"
83
     */
84
    public function hookModuleEnableAfter()
85
    {
86
        $this->library->clearCache();
87
    }
88
89
    /**
90
     * Implements hook "module.disable.after"
91
     */
92
    public function hookModuleDisableAfter()
93
    {
94
        $this->library->clearCache();
95
    }
96
97
    /**
98
     * Implements hook "module.install.after"
99
     */
100
    public function hookModuleInstallAfter()
101
    {
102
        $this->library->clearCache();
103
    }
104
105
    /**
106
     * Implements hook "module.uninstall.after"
107
     */
108
    public function hookModuleUninstallAfter()
109
    {
110
        $this->library->clearCache();
111
    }
112
113
    /**
114
     * Add CodeMirror library and extra files
115
     * @param \gplcart\core\Controller $controller
116
     * @throws InvalidArgumentException
117
     * @throws OutOfRangeException
118
     */
119
    public function addLibrary($controller)
120
    {
121
        if (!$controller instanceof Controller) {
0 ignored issues
show
Bug introduced by
The class gplcart\core\Controller does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
122
            throw new InvalidArgumentException('Argument must be instance of \gplcart\core\Controller');
123
        }
124
125
        $settings = $this->module->getSettings('codemirror');
126
        $controller->setJsSettings('codemirror', $settings);
127
        $controller->addAssetLibrary('codemirror');
128
129
        $library = $this->library->get('codemirror');
130
131
        if (!isset($library['basepath'])) {
132
            throw new OutOfRangeException('"basepath" key is not set in Codemirror library data');
133
        }
134
135
        $controller->setCss("{$library['basepath']}/theme/{$settings['theme']}.css");
136
137
        foreach ($settings['mode'] as $mode) {
138
            $controller->setJs("{$library['basepath']}/mode/$mode/$mode.js");
139
        }
140
    }
141
142
}
143