Completed
Push — master ( 747d09...5b690c )
by Alexey
05:04
created

TemplateController::editFileAction()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 14
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 18
rs 9.2
1
<?php
2
3
/**
4
 * Template controller
5
 *
6
 * This controller help for create and edit template and template pages
7
 *
8
 * @author Alexey Krupskiy <[email protected]>
9
 * @link http://inji.ru/
10
 * @copyright 2015 Alexey Krupskiy
11
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
12
 */
13
class TemplateController extends \Controller
14
{
15
    public function indexAction($templateName)
16
    {
17
        $template = \View\Template::get($templateName, \App::$primary);
18
        $this->view->setTitle($templateName);
19
        $this->view->page(['content' => 'template/edit', 'data' => compact('template')]);
20
    }
21
22
    function editFileAction($templateName)
1 ignored issue
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
editFileAction uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
23
    {
24
        $template = \View\Template::get($templateName, \App::$primary);
25
        if (!empty($_GET['path']) && file_exists($template->path . '/' . Tools::parsePath($_GET['path']))) {
26
            $code = file_get_contents("php://input");
27
            if (!empty($code)) {
28
                $result = new Server\Result();
29
                $result->successMsg = 'Файл сохранен';
30
                $content = file_put_contents($template->path . '/' . Tools::parsePath($_GET['path']), $code);
0 ignored issues
show
Unused Code introduced by
$content is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
31
                $result->send();
32
            }
33
            $content = file_get_contents($template->path . '/' . Tools::parsePath($_GET['path']));
34
            $this->libs->loadLib('Ace');
35
            $this->view->page(['content' => 'template/edit', 'data' => compact('template', 'content')]);
36
        } else {
37
            $this->view->page(['content' => 'chooseFile', 'data' => compact('template')]);
38
        }
39
    }
40
41
}
42