Main::hookRouteList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
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
     * Add CodeMirror library and extra files
83
     * @param \gplcart\core\Controller $controller
84
     * @throws InvalidArgumentException
85
     * @throws OutOfRangeException
86
     */
87
    public function addLibrary($controller)
88
    {
89
        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...
90
            throw new InvalidArgumentException('Argument must be instance of \gplcart\core\Controller');
91
        }
92
93
        $settings = $this->module->getSettings('codemirror');
94
        $controller->setJsSettings('codemirror', $settings);
95
        $controller->addAssetLibrary('codemirror');
96
97
        $library = $this->library->get('codemirror');
98
99
        if (!isset($library['basepath'])) {
100
            throw new OutOfRangeException('"basepath" key is not set in Codemirror library data');
101
        }
102
103
        $controller->setCss("{$library['basepath']}/theme/{$settings['theme']}.css");
104
105
        foreach ($settings['mode'] as $mode) {
106
            $controller->setJs("{$library['basepath']}/mode/$mode/$mode.js");
107
        }
108
    }
109
110
}
111