CategoriesController::edit()   B
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 18

Duplication

Lines 10
Ratio 31.25 %

Importance

Changes 0
Metric Value
dl 10
loc 32
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 18
nc 4
nop 0
1
<?php
2
namespace App\Controller\Admin;
3
4
use App\Controller\AppController;
5
use Cake\I18n\I18n;
6
7
class CategoriesController extends AppController
8
{
9
10
    /**
11
     * Display all categories.
12
     *
13
     * @return void
14
     */
15
    public function index()
16
    {
17
        $this->loadModel('BlogCategories');
18
19
        $this->paginate = [
20
            'maxLimit' => 15
21
        ];
22
23
        $categories = $this->BlogCategories
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\Admin\CategoriesController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
24
            ->find()
25
            ->order([
26
                'created' => 'desc'
27
            ]);
28
29
        $categories = $this->paginate($categories);
30
        $this->set(compact('categories'));
31
    }
32
33
    /**
34
     * Add a category.
35
     *
36
     * @return \Cake\Network\Response|void
37
     */
38
    public function add()
39
    {
40
        $this->loadModel('BlogCategories');
41
42
        $this->BlogCategories->locale(I18n::defaultLocale());
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\Admin\CategoriesController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
        $category = $this->BlogCategories->newEntity($this->request->getParsedBody());
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\Admin\CategoriesController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
44
45
        if ($this->request->is('post')) {
46
            $category->setTranslations($this->request->getParsedBody());
47
48
            if ($this->BlogCategories->save($category)) {
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\Admin\CategoriesController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
49
                $this->Flash->success(__d('admin', 'The category has been created successfully !'));
50
51
                return $this->redirect(['action' => 'index']);
52
            }
53
        }
54
55
        $this->set(compact('category'));
56
    }
57
58
    /**
59
     * Edit a category.
60
     *
61
     * @return \Cake\Network\Response|void
62
     */
63
    public function edit()
64
    {
65
        $this->loadModel('BlogCategories');
66
67
        $this->BlogCategories->locale(I18n::defaultLocale());
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\Admin\CategoriesController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
68
        $category = $this->BlogCategories
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\Admin\CategoriesController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
69
            ->find('translations')
70
            ->where([
71
                'BlogCategories.id' => $this->request->id
72
            ])
73
            ->first();
74
75
        //Check if the category is found.
76
        if (empty($category)) {
77
            $this->Flash->error(__d('admin', 'This category doesn\'t exist or has been deleted.'));
78
79
            return $this->redirect(['action' => 'index']);
80
        }
81
82 View Code Duplication
        if ($this->request->is('put')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
            $this->BlogCategories->patchEntity($category, $this->request->getParsedBody());
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\Admin\CategoriesController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
84
            $category->setTranslations($this->request->getParsedBody());
85
86
            if ($this->BlogCategories->save($category)) {
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\Admin\CategoriesController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
87
                $this->Flash->success(__d('admin', 'This category has been updated successfully !'));
88
89
                return $this->redirect(['action' => 'index']);
90
            }
91
        }
92
93
        $this->set(compact('category'));
94
    }
95
96
    /**
97
     * Delete a category and all his comments and likes.
98
     *
99
     * @return \Cake\Network\Response
100
     */
101
    public function delete()
102
    {
103
        $this->loadModel('BlogCategories');
104
105
        $category = $this->BlogCategories
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\Admin\CategoriesController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
106
            ->find()
107
            ->where([
108
                'BlogCategories.id' => $this->request->id
109
            ])
110
            ->first();
111
112
        //Check if the category is found.
113
        if (empty($category)) {
114
            $this->Flash->error(__d('admin', 'This category doesn\'t exist or has been deleted.'));
115
116
            return $this->redirect(['action' => 'index']);
117
        }
118
119
        //Check if the category has one article or more
120
        if ($category->article_count >= 1) {
121
            $this->Flash->error(__d('admin', 'You can not deleted this category because one article or more is assigned to this category.'));
122
123
            return $this->redirect(['action' => 'index']);
124
        }
125
126
        if ($this->BlogCategories->delete($category)) {
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\Admin\CategoriesController>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
127
            $this->Flash->success(__d('admin', 'This category has been deleted successfully !'));
128
129
            return $this->redirect(['action' => 'index']);
130
        }
131
132
        $this->Flash->error(__d('admin', 'Unable to delete this category.'));
133
134
        return $this->redirect(['action' => 'index']);
135
    }
136
}
137