Test Setup Failed
Push — master ( ec638a...cb9435 )
by Julito
51:10
created

Editor   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 252
Duplicated Lines 7.94 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 20
loc 252
rs 8.8
c 0
b 0
f 0
wmc 36
lcom 1
cbo 2

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A getName() 0 4 1
A setName() 0 4 1
A createHtml() 0 6 1
A editorReplace() 20 21 1
C toJavascript() 0 44 13
A setConfigAttribute() 0 4 1
A getConfigAttribute() 0 4 2
C processConfig() 0 30 11
A getEditorTemplate() 0 4 1
A getEditorStandAloneTemplate() 0 4 1
A formatTemplates() 0 4 1
A getLocale() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Component\Editor;
5
6
use Chamilo\CoreBundle\Framework\Template;
7
use Symfony\Component\Translation\Translator;
8
use Symfony\Component\Routing\RouterInterface;
9
use Symfony\Component\Translation\TranslatorInterface;
10
11
/**
12
 * Class Editor
13
 * @package Chamilo\CoreBundle\Component\Editor
14
 */
15
class Editor
16
{
17
    /**
18
     * Name of the instance.
19
     *
20
     * @access protected
21
     * @var string
22
     */
23
    public $name;
24
25
    /**
26
     * Name of the toolbar to load.
27
     *
28
     * @var string
29
     */
30
    public $toolbarSet;
31
32
    /**
33
     * Initial value.
34
     *
35
     * @var string
36
     */
37
    public $value;
38
39
    /**
40
     * @var array
41
     */
42
    public $config;
43
44
    /** @var TranslatorInterface */
45
    public $translator;
46
47
    /** @var RouterInterface */
48
    public $urlGenerator;
49
50
    /** @var \Template */
51
    public $template;
52
53
    /**
54
     * Editor constructor.
55
     * @param TranslatorInterface $translator
56
     * @param RouterInterface $urlGenerator
57
     */
58
    public function __construct(
59
        TranslatorInterface $translator,
60
        RouterInterface $urlGenerator
61
    ) {
62
        $this->toolbarSet = 'Basic';
63
        $this->value = '';
64
        $this->config = array();
65
        $this->setConfigAttribute('width', '100%');
66
        $this->setConfigAttribute('height', '200');
67
        $this->setConfigAttribute('fullPage', false);
68
69
        $this->translator = $translator;
70
        $this->urlGenerator = $urlGenerator;
71
        //$this->course = $course;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getName()
78
    {
79
        return $this->name;
80
    }
81
82
    /**
83
     * @param string $name
84
     */
85
    public function setName($name)
86
    {
87
        $this->name = $name;
88
    }
89
90
    /**
91
     * Return the HTML code required to run editor.
92
     *
93
     * @return string
94
     */
95
    public function createHtml()
96
    {
97
        $html = '<textarea id="'.$this->getName().'" name="'.$this->getName().'">'.$this->value.'</textarea>';
98
        
99
        return $html;
100
    }
101
102
    /**
103
     * @return string
104
     */
105 View Code Duplication
    public function editorReplace()
106
    {
107
        $toolbar = new Toolbar(
108
            $this->urlGenerator,
109
            $this->toolbarSet,
110
            $this->config
111
        );
112
113
        $toolbar->setLanguage($this->getLocale());
114
        $config = $toolbar->getConfig();
115
        $javascript = $this->toJavascript($config);
116
117
        $html = "<script>
118
           CKEDITOR.replace('".$this->name."',
119
               $javascript
120
           );
121
           
122
           </script>";
123
124
        return $html;
125
    }
126
127
    /**
128
     * Converts a PHP variable into its Javascript equivalent.
129
     * The code of this method has been "borrowed" from the function drupal_to_js() within the Drupal CMS.
130
     * @param mixed $var  The variable to be converted into Javascript syntax
131
     *
132
     * @return string    Returns a string
133
     * Note: This function is similar to json_encode(),
134
     * in addition it produces HTML-safe strings, i.e. with <, > and & escaped.
135
     * @link http://drupal.org/
136
     */
137
    protected function toJavascript($var)
138
    {
139
        switch (gettype($var)) {
140
            case 'boolean':
141
                return $var ? 'true' : 'false'; // Lowercase necessary!
142
            case 'integer':
143
                //no break
144
            case 'double':
145
                return (string)$var;
146
                //no break
147
            case 'resource':
148
                //no break
149
            case 'string':
150
                return '"'.str_replace(
151
                    array("\r", "\n", "<", ">", "&"),
152
                    array('\r', '\n', '\x3c', '\x3e', '\x26'),
153
                    addslashes($var)
154
                ).'"';
155
                break;
156
            case 'array':
157
                // Arrays in JSON can't be associative. If the array is empty or if it
158
                // has sequential whole number keys starting with 0, it's not associative
159
                // so we can go ahead and convert it as an array.
160
                if (empty($var) || array_keys($var) === range(0, sizeof($var) - 1)) {
161
                    $output = array();
162
                    foreach ($var as $v) {
163
                        $output[] = $this->toJavascript($v);
164
                    }
165
166
                    return '[ '.implode(', ', $output).' ]';
167
                }
168
                //no break
169
            case 'object':
170
                // Otherwise, fall through to convert the array as an object.
171
                $output = array();
172
                foreach ($var as $k => $v) {
173
                    $output[] = $this->toJavascript(strval($k)).': '.$this->toJavascript($v);
174
                }
175
                return '{ '.implode(', ', $output).' }';
176
                break;
177
            default:
178
                return 'null';
179
        }
180
    }
181
182
    /**
183
     * @param string $key
184
     * @param mixed  $value
185
     */
186
    public function setConfigAttribute($key, $value)
187
    {
188
        $this->config[$key] = $value;
189
    }
190
191
    /**
192
     * @param string $key
193
     *
194
     * @return mixed
195
     */
196
    public function getConfigAttribute($key)
197
    {
198
        return isset($this->config[$key]) ? $this->config[$key] : null;
199
    }
200
201
    /**
202
     * @param array $config
203
     */
204
    public function processConfig($config)
205
    {
206
        if (is_array($config)) {
207
            foreach ($config as $key => $value) {
208
                switch ($key) {
209
                    case 'ToolbarSet':
210
                        $this->toolbarSet = $value;
211
                        break;
212
                    case 'Config':
213
                        $this->processConfig($value);
214
                        break;
215
                    case 'width':
216
                    case 'Width':
217
                        $this->setConfigAttribute('width', $value);
218
                        break;
219
                    case 'height':
220
                    case 'Height':
221
                        $this->setConfigAttribute('height', $value);
222
                        break;
223
                    case 'FullPage':
224
                    case 'fullPage':
225
                        $this->setConfigAttribute('fullPage', $value);
226
                        break;
227
                    default:
228
                        $this->setConfigAttribute($key, $value);
229
                        break;
230
                }
231
            }
232
        }
233
    }
234
235
    /**
236
     * @return null
237
     */
238
    public function getEditorTemplate()
239
    {
240
        return null;
241
    }
242
243
    /**
244
     * @return string
245
     */
246
    public function getEditorStandAloneTemplate()
247
    {
248
        return 'javascript/editor/elfinder_standalone.tpl';
249
    }
250
251
    /**
252
     * @return null
253
     */
254
    public function formatTemplates($templates)
255
    {
256
        return null;
257
    }
258
259
    /**
260
     * @return string
261
     */
262
    public function getLocale()
263
    {
264
        return $this->translator->getLocale();
265
    }
266
}
267