EditorController::editor()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 37
rs 9.328
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Modules\Appearance\Editor;
4
5
use Rudolf\Component\Alerts\Alert;
6
use Rudolf\Component\Alerts\AlertsCollection;
7
use Rudolf\Framework\Controller\AdminController;
8
9
class EditorController extends AdminController
10
{
11
    /**
12
     * Editor.
13
     *
14
     * @param string $file Filename in base64, default templates/_header.html.php
15
     *
16
     * @return void
17
     * @throws \Exception
18
     */
19
    public function editor($file)
20
    {
21
        if ('/admin/appearance/editor' === $file) {
22
            $this->redirectTo('/admin/appearance/editor/file/dGVtcGxhdGVzL19oZWFkLmh0bWwucGhw');
23
        }
24
25
        $model = new EditorModel();
26
27
        $filesList['templates'] = $model->getFilesListByPath('templates');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$filesList was never initialized. Although not strictly required by PHP, it is generally a good practice to add $filesList = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
28
        $filesList['css'] = $model->getFilesListByPath('css');
29
        $filesList['js'] = $model->getFilesListByPath('js');
30
31
        $filename = base64_decode($file);
32
33
        // if data was send
34
        if (isset($_POST['save'])) {
35
            /** @var array $_POST */
36
            if ($model->saveFile($filename, $_POST['content'])) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $model->saveFile($filename, $_POST['content']) of type false|integer is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
37
                AlertsCollection::add(new Alert(
38
                    'success',
39
                    'Pomyślnie zapisano plik szablonu '.$filename.'.'
40
                ));
41
            } else {
42
                AlertsCollection::add(new Alert(
43
                    'error',
44
                    'Coś poszło nie tak podczas zapisywania pliku '.$filename.'. '.
45
                    'Sprawdź prawa dostępu'
46
                ));
47
            }
48
49
            $this->redirectTo($file);
50
        }
51
52
        $view = new EditorView();
53
        $view->editor($filesList, $model->getFileInfo($filename));
54
        $view->render();
55
    }
56
}
57