ConfigurationController   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 7 1
A indexAction() 0 23 4
A saveFormData() 0 22 5
1
<?php
2
/**
3
     * @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.com.ua)
4
     * @author Aleksandr Torosh <[email protected]>
5
     */
6
7
namespace Cms\Controller;
8
9
use Application\Mvc\Controller;
10
use Cms\Form\ConfigurationForm;
11
use Cms\Model\Configuration;
12
13
class ConfigurationController extends Controller
14
{
15
16
    public function initialize()
17
    {
18
        $this->setAdminEnvironment();
19
        $this->helper->activeMenu()->setActive('admin-cms');
20
        $this->view->languages_disabled = true;
0 ignored issues
show
Bug introduced by
Accessing languages_disabled on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
21
22
    }
23
24
    public function indexAction()
25
    {
26
        $model = new Configuration();
27
28
        $form = new ConfigurationForm();
29
30
        if ($this->request->isPost()) {
31
            $post = $this->request->getPost();
32
            if ($form->isValid($post)) {
33
                if ($this->saveFormData($post)) {
34
                    $this->flash->success($this->helper->at('Configuration saved'));
35
                    $this->redirect($this->url->get() . 'cms/configuration');
36
                }
37
            } else {
38
                $this->flashErrors($form);
39
            }
40
        } else {
41
            $form->setEntity($model->buildFormData());
42
        }
43
44
        $this->view->form = $form;
0 ignored issues
show
Bug introduced by
Accessing form on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
45
        $this->helper->title($this->helper->at('CMS Configuration'), true);
46
    }
47
48
    public function saveFormData($post)
49
    {
50
        $result = true;
51
        foreach (Configuration::$keys as $key => $value) {
52
            $model = Configuration::findFirst("key = '$key'");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $model is correct as \Cms\Model\Configuration...First("key = '{$key}'") (which targets Phalcon\Mvc\Model::findFirst()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
53
            if (!$model) {
54
                $model = new Configuration();
55
                $model->setKey($key);
56
            }
57
            if (array_key_exists($key, $post)) {
58
                $model->setValue($post[$key]);
59
            } else {
60
                $model->setValue($value);
61
            }
62
            $model->updateCheckboxes($post);
63
            if (!$model->save()) {
64
                $result = false;
65
                $this->flashErrors($model);
66
            }
67
        }
68
        return $result;
69
    }
70
71
}