Completed
Push — master ( c6cfcb...a88956 )
by Julito
11:58
created

Connector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 8
dl 0
loc 27
rs 9.9332
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Component\Editor;
6
7
use Chamilo\CoreBundle\Component\Editor\Driver\Driver;
8
use Chamilo\CoreBundle\Entity\Course;
9
use Chamilo\CoreBundle\Entity\Session;
10
use Chamilo\CoreBundle\Entity\User;
11
use Doctrine\ORM\EntityManager;
12
use Symfony\Component\Routing\Router;
13
use Symfony\Component\Routing\RouterInterface;
14
use Symfony\Component\Translation\Translator;
15
use Symfony\Contracts\Translation\TranslatorInterface;
16
17
//use Symfony\Component\Security\Core\SecurityContext;
18
19
/**
20
 * Class elFinder Connector - editor + Chamilo repository.
21
 */
22
class Connector
23
{
24
    /** @var Course */
25
    public $course;
26
27
    /** @var User */
28
    public $user;
29
30
    /** @var Session */
31
    public $session;
32
33
    /** @var Translator */
34
    public $translator;
35
36
    /** @var Router */
37
    public $urlGenerator;
38
39
    public $security;
40
41
    public $paths;
42
43
    public $entityManager;
44
45
    public $drivers = [];
46
    public $driverList = [];
47
48
    public function __construct(
49
        EntityManager $entityManager,
50
        array $paths,
51
        RouterInterface $urlGenerator,
52
        TranslatorInterface $translator,
53
        $security,
54
        $user,
55
        $course,
56
        $session
57
    ) {
58
        $this->paths = [
59
            //'root_sys' => api_get_path(SYS_PATH),
60
            //'sys_root' => api_get_path(SYS_PATH), // just an alias
61
            //'sys_course_path' => api_get_path(SYS_COURSE_PATH),
62
            //   'sys_config_path' => $app['path.config'],
63
            'path.temp' => api_get_path(SYS_ARCHIVE_PATH),
64
            //'sys_log_path' => $app['path.logs']
65
        ];
66
        $this->entityManager = $entityManager;
67
        //$this->paths = $paths;
68
        $this->urlGenerator = $urlGenerator;
69
        $this->translator = $translator;
70
        $this->security = $security;
71
        $this->user = $user;
72
        $this->course = $course;
73
        $this->session = $session;
74
        $this->driverList = $this->getDefaultDriverList();
75
    }
76
77
    public function getDriverList(): array
78
    {
79
        return $this->driverList;
80
    }
81
82
    /**
83
     * Available driver list.
84
     *
85
     * @param array $list
86
     */
87
    public function setDriverList($list)
88
    {
89
        $this->driverList = $list;
90
    }
91
92
    /**
93
     * @param Driver $driver
94
     */
95
    public function addDriver($driver)
96
    {
97
        if (!empty($driver)) {
98
            $this->drivers[$driver->getName()] = $driver;
99
        }
100
    }
101
102
    public function getDrivers(): array
103
    {
104
        return $this->drivers;
105
    }
106
107
    /**
108
     * @param string $driverName
109
     *
110
     * @return Driver
111
     */
112
    public function getDriver($driverName)
113
    {
114
        if (isset($this->drivers[$driverName])) {
115
            return $this->drivers[$driverName];
116
        }
117
118
        return null;
119
    }
120
121
    /**
122
     * @param bool $processDefaultValues
123
     *
124
     * @return array
125
     */
126
    public function getRoots($processDefaultValues = true)
127
    {
128
        $roots = [];
129
        $drivers = $this->getDrivers();
130
        /** @var Driver $driver */
131
        foreach ($drivers as $driver) {
132
            if ($processDefaultValues) {
133
                $plugin = [
134
                    'chamilo' => [
135
                        'driverName' => $driver->getName(),
136
                        'connector' => $this,
137
                    ],
138
                ];
139
                $configuration = $driver->getConfiguration();
140
                $driver->setup();
141
                $configuration['plugin'] = $plugin;
142
                $root = $this->updateWithDefaultValues($configuration);
143
            }
144
            $roots[] = $root;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $root does not seem to be defined for all execution paths leading up to this point.
Loading history...
145
        }
146
147
        return $roots;
148
    }
149
150
    /**
151
     * Merges the default driver settings.
152
     *
153
     * @param array $driver
154
     *
155
     * @return array
156
     */
157
    public function updateWithDefaultValues($driver)
158
    {
159
        if (empty($driver) || !isset($driver['driver'])) {
160
            return [];
161
        }
162
163
        $defaultDriver = $this->getDefaultDriverSettings();
164
165
        if (isset($driver['attributes'])) {
166
            $attributes = array_merge($defaultDriver['attributes'], $driver['attributes']);
167
        } else {
168
            $attributes = $defaultDriver['attributes'];
169
        }
170
171
        $driverUpdated = array_merge($defaultDriver, $driver);
172
        $driverUpdated['driver'] = 'Chamilo\CoreBundle\Component\Editor\Driver\\'.$driver['driver'];
173
        $driverUpdated['attributes'] = $attributes;
174
175
        return $driverUpdated;
176
    }
177
178
    /**
179
     * Get default driver settings.
180
     */
181
    public function getDefaultDriverSettings(): array
182
    {
183
        // for more options: https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options
184
        return [
185
            'uploadOverwrite' => false, // Replace files on upload or give them new name if the same file was uploaded
186
            //'acceptedName' =>
187
            'uploadAllow' => [
188
                'image',
189
                'audio',
190
                'video',
191
                'text/html',
192
                'text/csv',
193
                'application/pdf',
194
                'application/postscript',
195
                'application/vnd.ms-word',
196
                'application/vnd.ms-excel',
197
                'application/vnd.ms-powerpoint',
198
                'application/pdf',
199
                'application/xml',
200
                'application/vnd.oasis.opendocument.text',
201
                'application/x-shockwave-flash',
202
                'application/vnd.adobe.flash.movie',
203
            ], // allow files
204
            //'uploadDeny' => array('text/x-php'),
205
            'uploadOrder' => ['allow'], // only executes allow
206
            'disabled' => [
207
                'duplicate',
208
                'rename',
209
                'mkdir',
210
                'mkfile',
211
                'copy',
212
                'cut',
213
                'paste',
214
                'edit',
215
                'extract',
216
                'archive',
217
                'help',
218
                'resize',
219
            ],
220
            'attributes' => [
221
                // Hiding dangerous files
222
                [
223
                    'pattern' => '/\.(php|py|pl|sh|xml)$/i',
224
                    'read' => false,
225
                    'write' => false,
226
                    'hidden' => true,
227
                    'locked' => false,
228
                ],
229
                // Hiding _DELETED_ files
230
                [
231
                    'pattern' => '/_DELETED_/',
232
                    'read' => false,
233
                    'write' => false,
234
                    'hidden' => true,
235
                    'locked' => false,
236
                ],
237
                // Hiding thumbnails
238
                [
239
                    'pattern' => '/.tmb/',
240
                    'read' => false,
241
                    'write' => false,
242
                    'hidden' => true,
243
                    'locked' => false,
244
                ],
245
                [
246
                    'pattern' => '/.thumbs/',
247
                    'read' => false,
248
                    'write' => false,
249
                    'hidden' => true,
250
                    'locked' => false,
251
                ],
252
                [
253
                    'pattern' => '/.quarantine/',
254
                    'read' => false,
255
                    'write' => false,
256
                    'hidden' => true,
257
                    'locked' => false,
258
                ],
259
            ],
260
        ];
261
    }
262
263
    public function getOperations(): array
264
    {
265
        //https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options-2.1
266
        $opts = [
267
            //'debug' => true,
268
            'bind' => [
269
                'upload rm mkdir' => [$this, 'manageCommands'],
270
            ],
271
            'sessionCloseEarlier' => false,
272
        ];
273
274
        $this->setDrivers();
275
        $opts['roots'] = $this->getRoots();
276
277
        return $opts;
278
    }
279
280
    /**
281
     * Set drivers from list.
282
     */
283
    public function setDrivers()
284
    {
285
        foreach ($this->getDriverList() as $driverName) {
286
            $this->setDriver($driverName);
287
        }
288
    }
289
290
    /**
291
     * Sets a driver.
292
     *
293
     * @param string $driverName
294
     */
295
    public function setDriver($driverName)
296
    {
297
        $driverClass = $this->getDriverClass($driverName);
298
299
        /** @var Driver $driver */
300
        $driver = new $driverClass();
301
        $driver->setName($driverName);
302
        $driver->setConnector($this);
303
        $this->addDriver($driver);
304
    }
305
306
    /**
307
     * Simple function to demonstrate how to control file access using "accessControl" callback.
308
     * This method will disable accessing files/folders starting from  '.' (dot).
309
     *
310
     * @param string $attr   attribute name (read|write|locked|hidden)
311
     * @param string $path   file path relative to volume root directory started with directory separator
312
     * @param string $data
313
     * @param string $volume
314
     *
315
     * @return bool|null
316
     */
317
    public function access($attr, $path, $data, $volume)
318
    {
319
        return 0 === strpos(basename($path), '.')       // if file/folder begins with '.' (dot)
320
            ? !('read' == $attr || 'write' == $attr)    // set read+write to false, other (locked+hidden) set to true
321
            : null; // else elFinder decide it itself
322
    }
323
324
    /**
325
     * @param string $cmd
326
     * @param array  $result
327
     * @param array  $args
328
     * @param Finder $elFinder
329
     */
330
    public function manageCommands($cmd, $result, $args, $elFinder)
331
    {
332
    }
333
334
    /**
335
     * Available driver list.
336
     *
337
     * @return array
338
     */
339
    private function getDefaultDriverList()
340
    {
341
        return [
342
            'CourseDriver',
343
            'CourseUserDriver',
344
            'DropBoxDriver',
345
            'HomeDriver',
346
            'PersonalDriver',
347
        ];
348
    }
349
350
    /**
351
     * @param string $driver
352
     *
353
     * @return string
354
     */
355
    private function getDriverClass($driver)
356
    {
357
        return 'Chamilo\CoreBundle\Component\Editor\Driver\\'.$driver;
358
    }
359
}
360