Completed
Push — master ( 4572bc...38e61c )
by Julito
10:57
created

Editor   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 78
c 1
b 0
f 0
dl 0
loc 220
rs 9.68
wmc 34

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A setName() 0 3 1
A getTextareaId() 0 3 1
A __construct() 0 12 1
A setTextareaId() 0 5 1
A setConfigAttribute() 0 3 1
A getLocale() 0 3 1
A getConfigAttribute() 0 3 2
A getEditorStandAloneTemplate() 0 3 1
B processConfig() 0 26 11
C toJavascript() 0 40 13
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Component\Editor;
5
6
use Symfony\Component\Routing\RouterInterface;
7
use Symfony\Contracts\Translation\TranslatorInterface;
8
9
/**
10
 * Class Editor.
11
 *
12
 * @package Chamilo\CoreBundle\Component\Editor
13
 */
14
class Editor
15
{
16
    /**
17
     * @var string
18
     */
19
    public $textareaId;
20
21
    /**
22
     * Name of the instance.
23
     *
24
     * @var string
25
     */
26
    public $name;
27
28
    /**
29
     * Name of the toolbar to load.
30
     *
31
     * @var string
32
     */
33
    public $toolbarSet;
34
35
    /**
36
     * Initial value.
37
     *
38
     * @var string
39
     */
40
    public $value;
41
42
    /**
43
     * @var array
44
     */
45
    public $config;
46
47
    /** @var TranslatorInterface */
48
    public $translator;
49
50
    /** @var RouterInterface */
51
    public $urlGenerator;
52
53
    /** @var \Template */
54
    public $template;
55
56
    /**
57
     * Editor constructor.
58
     *
59
     * @param TranslatorInterface $translator
60
     * @param RouterInterface     $urlGenerator
61
     */
62
    public function __construct(
63
        TranslatorInterface $translator,
64
        RouterInterface $urlGenerator
65
    ) {
66
        $this->toolbarSet = 'Basic';
67
        $this->value = '';
68
        $this->config = [];
69
        $this->setConfigAttribute('width', '100%');
70
        $this->setConfigAttribute('height', '200');
71
        $this->setConfigAttribute('fullPage', false);
72
        $this->translator = $translator;
73
        $this->urlGenerator = $urlGenerator;
74
        //$this->course = $course;
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getName()
81
    {
82
        return $this->name;
83
    }
84
85
    /**
86
     * @param string $name
87
     */
88
    public function setName($name)
89
    {
90
        $this->name = $name;
91
    }
92
93
    /**
94
     * @return string
95
     */
96
    public function getTextareaId()
97
    {
98
        return $this->textareaId;
99
    }
100
101
    /**
102
     * @param string $textareaId
103
     *
104
     * @return Editor
105
     */
106
    public function setTextareaId($textareaId)
107
    {
108
        $this->textareaId = $textareaId;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @param string $key
115
     * @param mixed  $value
116
     */
117
    public function setConfigAttribute($key, $value)
118
    {
119
        $this->config[$key] = $value;
120
    }
121
122
    /**
123
     * @param string $key
124
     *
125
     * @return mixed
126
     */
127
    public function getConfigAttribute($key)
128
    {
129
        return isset($this->config[$key]) ? $this->config[$key] : null;
130
    }
131
132
    /**
133
     * @param array $config
134
     */
135
    public function processConfig($config)
136
    {
137
        if (is_array($config)) {
138
            foreach ($config as $key => $value) {
139
                switch ($key) {
140
                    case 'ToolbarSet':
141
                        $this->toolbarSet = $value;
142
                        break;
143
                    case 'Config':
144
                        $this->processConfig($value);
145
                        break;
146
                    case 'width':
147
                    case 'Width':
148
                        $this->setConfigAttribute('width', $value);
149
                        break;
150
                    case 'height':
151
                    case 'Height':
152
                        $this->setConfigAttribute('height', $value);
153
                        break;
154
                    case 'FullPage':
155
                    case 'fullPage':
156
                        $this->setConfigAttribute('fullPage', $value);
157
                        break;
158
                    default:
159
                        $this->setConfigAttribute($key, $value);
160
                        break;
161
                }
162
            }
163
        }
164
    }
165
166
    /**
167
     * @return string
168
     */
169
    public function getEditorStandAloneTemplate()
170
    {
171
        return 'javascript/editor/elfinder_standalone.html.twig';
172
    }
173
174
    /**
175
     * @return string
176
     */
177
    public function getLocale()
178
    {
179
        return api_get_language_isocode();
180
    }
181
182
    /**
183
     * Converts a PHP variable into its Javascript equivalent.
184
     * The code of this method has been "borrowed" from the function drupal_to_js() within the Drupal CMS.
185
     *
186
     * @param mixed $var The variable to be converted into Javascript syntax
187
     *
188
     * @return string Returns a string
189
     *                Note: This function is similar to json_encode(),
190
     *                in addition it produces HTML-safe strings, i.e. with <, > and & escaped.
191
     *
192
     * @see http://drupal.org/
193
     */
194
    protected function toJavascript($var)
195
    {
196
        switch (gettype($var)) {
197
            case 'boolean':
198
                return $var ? 'true' : 'false'; // Lowercase necessary!
199
            case 'integer':
200
            case 'double':
201
                return (string) $var;
202
            case 'resource':
203
            case 'string':
204
                return '"'.str_replace(
205
                    ["\r", "\n", "<", ">", "&"],
206
                    ['\r', '\n', '\x3c', '\x3e', '\x26'],
207
                    addslashes($var)
0 ignored issues
show
Bug introduced by
It seems like $var can also be of type resource; however, parameter $str of addslashes() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

207
                    addslashes(/** @scrutinizer ignore-type */ $var)
Loading history...
208
                ).'"';
209
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
210
            case 'array':
211
                // Arrays in JSON can't be associative. If the array is empty or if it
212
                // has sequential whole number keys starting with 0, it's not associative
213
                // so we can go ahead and convert it as an array.
214
                if (empty($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
215
                    $output = [];
216
                    foreach ($var as $v) {
217
                        $output[] = $this->toJavascript($v);
218
                    }
219
220
                    return '[ '.implode(', ', $output).' ]';
221
                }
222
                //no break
223
            case 'object':
224
                // Otherwise, fall through to convert the array as an object.
225
                $output = [];
226
                foreach ($var as $k => $v) {
227
                    $output[] = $this->toJavascript(strval($k)).': '.$this->toJavascript($v);
228
                }
229
230
                return '{ '.implode(', ', $output).' }';
231
                break;
232
            default:
233
                return 'null';
234
        }
235
    }
236
}
237