Completed
Push — email ( e52752 )
by Fèvre
03:13
created

BlogController   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 756
Duplicated Lines 25.93 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 44
lcom 1
cbo 9
dl 196
loc 756
rs 8
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeFilter() 0 6 1
B index() 26 26 1
B category() 5 46 2
B article() 0 120 6
A quote() 29 56 3
A go() 48 48 3
B archive() 28 28 1
B search() 10 43 3
B articleLike() 16 72 5
B articleUnlike() 8 47 4
B deleteComment() 5 47 5
B getEditComment() 16 54 5
B editComment() 5 48 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like BlogController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use BlogController, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace App\Controller;
3
4
use App\Event\Badges;
5
use Cake\Core\Configure;
6
use Cake\Event\Event;
7
use Cake\Network\Exception\NotFoundException;
8
use Cake\Routing\Router;
9
10
class BlogController extends AppController
11
{
12
13
    /**
14
     * Components.
15
     *
16
     * @var array
17
     */
18
    public $components = [
19
        'RequestHandler'
20
    ];
21
22
    /**
23
     * BeforeFilter handle.
24
     *
25
     * @param Event $event The beforeFilter event that was fired.
26
     *
27
     * @return void
28
     */
29
    public function beforeFilter(Event $event)
30
    {
31
        parent::beforeFilter($event);
32
33
        $this->Auth->allow(['index', 'category', 'article', 'go', 'archive', 'search']);
34
    }
35
36
    /**
37
     * Display all Articles.
38
     *
39
     * @return void
40
     */
41 View Code Duplication
    public function index()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
42
    {
43
        $this->loadModel('BlogArticles');
44
        $this->paginate = [
45
            'maxLimit' => Configure::read('Blog.article_per_page')
46
        ];
47
48
        $articles = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. 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
            ->find()
50
            ->contain([
51
                'BlogCategories',
52
                'Users' => function ($q) {
53
                    return $q->find('short');
54
                }
55
            ])
56
            ->order([
57
                'BlogArticles.created' => 'desc'
58
            ])
59
            ->where([
60
                'BlogArticles.is_display' => 1
61
            ]);
62
63
        $articles = $this->paginate($articles);
64
65
        $this->set(compact('articles'));
66
    }
67
68
    /**
69
     * Display a specific category with all its articles.
70
     *
71
     * @return \Cake\Network\Response|void
72
     */
73
    public function category()
74
    {
75
        $this->loadModel('BlogCategories');
76
77
        $category = $this->BlogCategories
0 ignored issues
show
Documentation introduced by
The property BlogCategories does not exist on object<App\Controller\BlogController>. 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...
78
            ->find()
79
            ->where([
80
                'BlogCategories.id' => $this->request->id
81
            ])
82
            ->contain([
83
                'BlogArticles'
84
            ])
85
            ->first();
86
87
        //Check if the category is found.
88 View Code Duplication
        if (empty($category)) {
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...
89
            $this->Flash->error(__('This category doesn\'t exist or has been deleted.'));
90
91
            return $this->redirect(['action' => 'index']);
92
        }
93
94
        //Paginate all Articles.
95
        $this->loadModel('BlogArticles');
96
        $this->paginate = [
97
            'maxLimit' => Configure::read('Blog.article_per_page')
98
        ];
99
100
        $articles = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. 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...
101
            ->find()
102
            ->contain([
103
                'Users' => function ($q) {
104
                    return $q->find('short');
105
                }
106
            ])
107
            ->where([
108
                'BlogArticles.category_id' => $category->id,
109
                'BlogArticles.is_display' => 1
110
            ])
111
            ->order([
112
                'BlogArticles.created' => 'desc'
113
            ]);
114
115
        $articles = $this->paginate($articles);
116
117
        $this->set(compact('category', 'articles'));
118
    }
119
120
    /**
121
     * Display a specific article.
122
     *
123
     * @return \Cake\Network\Response|void
124
     */
125
    public function article()
126
    {
127
        $this->loadModel('BlogArticles');
128
129
        $article = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. 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...
130
            ->find()
131
            ->where([
132
                'BlogArticles.id' => $this->request->id,
133
                'BlogArticles.is_display' => 1
134
            ])
135
            ->contain([
136
                'BlogCategories',
137
                'BlogAttachments',
138
                'Users' => function ($q) {
139
                        return $q->find('full');
140
                }
141
            ])
142
            ->first();
143
144
        //Check if the article is found.
145
        if (is_null($article)) {
146
            $this->Flash->error(__('This article doesn\'t exist or has been deleted.'));
147
148
            return $this->redirect(['action' => 'index']);
149
        }
150
151
        $this->loadModel('BlogArticlesComments');
152
153
        //A comment has been posted.
154
        if ($this->request->is('post')) {
155
            //Check if the user is connected.
156
            if (!$this->Auth->user()) {
157
                return $this->Flash->error(__('You must be connected to post a comment.'));
158
            }
159
160
            $this->request->data['article_id'] = $article->id;
161
            $this->request->data['user_id'] = $this->Auth->user('id');
162
163
            $newComment = $this->BlogArticlesComments->newEntity($this->request->data, ['validate' => 'create']);
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
164
165
            //Attach Event.
166
            $this->BlogArticlesComments->eventManager()->attach(new Badges($this));
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
167
168
            if ($insertComment = $this->BlogArticlesComments->save($newComment)) {
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
169
                $this->Flash->success(__('Your comment has been posted successfully !'));
170
                //Redirect the user to the last page of the article.
171
                $this->redirect([
172
                    'action' => 'go',
173
                    $insertComment->id
174
                ]);
175
            }
176
        }
177
178
        //Paginate all comments related to the article.
179
        $this->paginate = [
180
            'maxLimit' => Configure::read('Blog.comment_per_page')
181
        ];
182
183
        $comments = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
184
            ->find()
185
            ->where([
186
                'BlogArticlesComments.article_id' => $article->id
187
            ])
188
            ->contain([
189
                'Users' => function ($q) {
190
                    return $q->find('medium');
191
                }
192
            ])
193
            ->order([
194
                'BlogArticlesComments.created' => 'asc'
195
            ]);
196
197
        $comments = $this->paginate($comments);
198
199
        //Select the like for the current auth user.
200
        $this->loadModel('BlogArticlesLikes');
201
        $like = $this->BlogArticlesLikes
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. 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...
202
            ->find()
203
            ->where([
204
                'user_id' => ($this->Auth->user()) ? $this->Auth->user('id') : null,
205
                'article_id' => $article->id
206
            ])
207
            ->first();
208
209
        //Build the newEntity for the comment form.
210
        $formComments = $this->BlogArticlesComments->newEntity();
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
211
212
        //Search related articles
213
        $keywords = preg_split("/([\s,\W])+/", $article->title);
214
215
        $articles = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. 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...
216
            ->find()
217
            ->contain([
218
                'BlogCategories',
219
            ])
220
            ->where([
221
                'BlogArticles.is_display' => 1,
222
                'BlogArticles.id !=' => $article->id
223
            ])
224
            ->andWhere([
225
                'BlogArticles.title RLIKE' => rtrim(implode('|', $keywords), '|')
226
            ]);
227
228
        //Current user.
229
        $this->loadModel('Users');
230
        $currentUser = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\BlogController>. 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...
231
            ->find()
232
            ->contain([
233
                'Groups' => function ($q) {
234
                    return $q->select(['id', 'is_staff']);
235
                }
236
            ])
237
            ->where([
238
                'Users.id' => $this->Auth->user('id')
239
            ])
240
            ->select(['id', 'group_id'])
241
            ->first();
242
243
        $this->set(compact('article', 'formComments', 'comments', 'like', 'articles', 'currentUser'));
244
    }
245
246
    /**
247
     * Quote a message.
248
     *
249
     * @param int $articleId Id of the article where is the message to quote.
250
     * @param int $commentId Id of the message to quote.
251
     *
252
     * @throws \Cake\Network\Exception\NotFoundException
253
     *
254
     * @return mixed
255
     */
256
    public function quote($articleId = null, $commentId = null)
257
    {
258
        if (!$this->request->is('ajax')) {
259
            throw new NotFoundException();
260
        }
261
262
        $this->loadModel('BlogArticlesComments');
263
264
        $comment = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
265
            ->find()
266
            ->where([
267
                'BlogArticlesComments.article_id' => $articleId,
268
                'BlogArticlesComments.id' => $commentId
269
            ])
270
            ->contain([
271
                'Users' => function ($q) {
272
                        return $q->find('short');
273
                }
274
            ])
275
            ->first();
276
277
        $json = [];
278
279 View Code Duplication
        if (!is_null($comment)) {
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...
280
            $comment->toArray();
281
282
            $url = Router::url(['action' => 'go', $comment->id]);
283
            $text = __("has said :");
284
285
            //Build the quote.
286
            $json['comment'] = <<<EOT
287
<div>
288
     <div>
289
        <a href="{$url}">
290
            <strong>{$comment->user->full_name} {$text}</strong>
291
        </a>
292
    </div>
293
    <blockquote>
294
        $comment->content
295
    </blockquote>
296
</div><p>&nbsp;</p><p>&nbsp;</p>
297
EOT;
298
299
            $json['error'] = false;
300
301
            $this->set(compact('json'));
302
        } else {
303
            $json['comment'] = __("This comment doesn't exist.");
304
            $json['error'] = true;
305
306
            $this->set(compact('json'));
307
        }
308
309
        //Send response in JSON.
310
        $this->set('_serialize', 'json');
311
    }
312
313
    /**
314
     * Redirect an user to an article, page and comment.
315
     *
316
     * @param int $commentId Id of the comment.
317
     *
318
     * @return \Cake\Network\Response
319
     */
320 View Code Duplication
    public function go($commentId = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
321
    {
322
        $this->loadModel('BlogArticlesComments');
323
324
        $comment = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
325
            ->find()
326
            ->contain([
327
                'BlogArticles'
328
            ])
329
            ->where([
330
                'BlogArticlesComments.id' => $commentId
331
            ])
332
            ->first();
333
334
        if (is_null($comment)) {
335
            $this->Flash->error(__("This comment doesn't exist or has been deleted."));
336
337
            return $this->redirect(['action' => 'index']);
338
        }
339
340
        $comment->toArray();
341
342
        //Count the number of message before this message.
343
        $messagesBefore = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
344
            ->find()
345
            ->where([
346
                'BlogArticlesComments.article_id' => $comment->article_id,
347
                'BlogArticlesComments.created <' => $comment->created
348
            ])
349
            ->count();
350
351
        //Get the number of messages per page.
352
        $messagesPerPage = Configure::read('Blog.comment_per_page');
353
354
        //Calculate the page.
355
        $page = floor($messagesBefore / $messagesPerPage) + 1;
356
357
        $page = ($page > 1) ? $page : 1;
358
359
        //Redirect the user.
360
        return $this->redirect([
361
            '_name' => 'blog-article',
362
            'slug' => $comment->blog_article->title,
363
            'id' => $comment->blog_article->id,
364
            '?' => ['page' => $page],
365
            '#' => 'comment-' . $commentId
366
        ]);
367
    }
368
369
    /**
370
     * Get all articles by a date formatted to "m-Y".
371
     *
372
     * @param string $date The date of the archive.
373
     *
374
     * @return void
375
     */
376 View Code Duplication
    public function archive($date = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
377
    {
378
        $this->loadModel('BlogArticles');
379
380
        $this->paginate = [
381
            'maxLimit' => Configure::read('Blog.article_per_page')
382
        ];
383
384
        $archives = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. 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...
385
            ->find()
386
            ->where([
387
                'DATE_FORMAT(BlogArticles.created,\'%m-%Y\')' => $date,
388
                'BlogArticles.is_display' => 1
389
            ])
390
            ->contain([
391
                'BlogCategories',
392
                'Users' => function ($q) {
393
                        return $q->find('short');
394
                }
395
            ])
396
            ->order([
397
                'BlogArticles.created' => 'desc'
398
            ]);
399
400
        $articles = $this->paginate($archives);
401
402
        $this->set(compact('articles', 'date'));
403
    }
404
405
    /**
406
     * Search articles.
407
     *
408
     * @return void
409
     */
410
    public function search()
411
    {
412
        $this->loadModel('BlogArticles');
413
414
        //Check the keyword to search. (For pagination)
415 View Code Duplication
        if (!empty($this->request->data['search'])) {
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...
416
            $keyword = $this->request->data['search'];
417
            $this->request->session()->write('Search.Blog.Keyword', $keyword);
418
        } else {
419
            if ($this->request->session()->read('Search.Blog.Keyword')) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->request->session(...('Search.Blog.Keyword') of type string|null is loosely compared to true; this is ambiguous if the string can be empty. 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 string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
420
                $keyword = $this->request->session()->read('Search.Blog.Keyword');
421
            } else {
422
                $keyword = '';
423
            }
424
        }
425
426
        //Pagination
427
        $this->paginate = [
428
            'maxLimit' => Configure::read('Blog.article_per_page')
429
        ];
430
431
        $articles = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. 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...
432
            ->find()
433
            ->contain([
434
                'Users' => function ($q) {
435
                    return $q->find('short');
436
                }
437
            ])
438
            ->where([
439
                'BlogArticles.is_display' => 1
440
            ])
441
            ->andWhere(function ($q) use ($keyword) {
442
                return $q
443
                    ->like('title', "%$keyword%");
444
            })
445
            ->order([
446
                'BlogArticles.created' => 'desc'
447
            ]);
448
449
        $articles = $this->paginate($articles);
450
451
        $this->set(compact('articles', 'keyword'));
452
    }
453
454
    /**
455
     * Like an article.
456
     *
457
     * @param int $articleId Id of the article to like.
458
     *
459
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
460
     *
461
     * @return void
462
     */
463
    public function articleLike($articleId = null)
464
    {
465
        if (!$this->request->is('ajax')) {
466
            throw new NotFoundException();
467
        }
468
469
        //Check if the user hasn't already liked this article.
470
        $this->loadModel('BlogArticlesLikes');
471
        $checkLike = $this->BlogArticlesLikes
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. 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...
472
            ->find()
473
            ->where([
474
                'BlogArticlesLikes.user_id' => $this->Auth->user('id'),
475
                'BlogArticlesLikes.article_id' => $articleId
476
            ])
477
            ->first();
478
479
        $json = [];
480
481 View Code Duplication
        if (!is_null($checkLike)) {
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...
482
            $json['message'] = __('You already like this article !');
483
            $json['error'] = true;
484
485
            $this->set(compact('json'));
486
487
            $this->set('_serialize', 'json');
488
        }
489
490
        //Check if the article exist.
491
        $this->loadModel('BlogArticles');
492
        $checkArticle = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\BlogController>. 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...
493
            ->find()
494
            ->where([
495
                'BlogArticles.id' => $articleId,
496
                'BlogArticles.is_display' => 1
497
            ])
498
            ->first();
499
500 View Code Duplication
        if (is_null($checkArticle)) {
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...
501
            $json['message'] = __("This article doesn't exist !");
502
            $json['error'] = true;
503
504
            $this->set(compact('json'));
505
506
            $this->set('_serialize', 'json');
507
        }
508
509
        //Prepare data to be saved.
510
        $data = [];
511
        $data['BlogArticlesLikes']['user_id'] = $this->Auth->user('id');
512
        $data['BlogArticlesLikes']['article_id'] = $articleId;
513
514
        $like = $this->BlogArticlesLikes->newEntity($data);
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. 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...
515
516
        if ($this->BlogArticlesLikes->save($like)) {
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. 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...
517
            $json['message'] = __('Thanks for {0} this article ! ', "<i class='fa fa-heart text-danger'></i>");
518
            $json['title'] = __('You {0} this article.', "<i class='fa fa-heart text-danger'></i>");
519
            $json['url'] = Router::url(
520
                [
521
                    'action' => 'articleUnlike',
522
                    $articleId
523
                ]
524
            );
525
            $json['error'] = false;
526
        } else {
527
            $json['message'] = __('An error occurred, please try again later.');
528
            $json['error'] = true;
529
        }
530
531
        $this->set(compact('json'));
532
533
        $this->set('_serialize', 'json');
534
    }
535
536
    /**
537
     * Unlike an article.
538
     *
539
     * @param int|null $articleId Id of the article to like.
540
     *
541
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
542
     *
543
     * @return void
544
     */
545
    public function articleUnlike($articleId = null)
546
    {
547
        if (!$this->request->is('ajax')) {
548
            throw new NotFoundException();
549
        }
550
551
        //Check if the user like this article.
552
        $this->loadModel('BlogArticlesLikes');
553
        $like = $this->BlogArticlesLikes
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. 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...
554
            ->find()
555
            ->contain([
556
                'BlogArticles'
557
            ])
558
            ->where([
559
                'BlogArticlesLikes.user_id' => $this->Auth->user('id'),
560
                'BlogArticlesLikes.article_id' => $articleId,
561
                'BlogArticles.is_display' => 1
562
            ])
563
            ->first();
564
565
        $json = [];
566
567 View Code Duplication
        if (is_null($like)) {
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...
568
            $json['message'] = __("You don't like this article !");
569
            $json['error'] = true;
570
571
            $this->set(compact('json'));
572
573
            $this->set('_serialize', 'json');
574
        }
575
576
        if ($this->BlogArticlesLikes->delete($like)) {
0 ignored issues
show
Documentation introduced by
The property BlogArticlesLikes does not exist on object<App\Controller\BlogController>. 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...
577
            $json['url'] = Router::url([
578
                                'action' => 'articleLike',
579
                                $articleId
580
                            ]);
581
            $json['title'] = __('Like {0}', "<i class='fa fa-heart text-danger'></i>");
582
            $json['error'] = false;
583
        } else {
584
            $json['message'] = __('An error occurred, please try again later.');
585
            $json['error'] = true;
586
        }
587
588
        $this->set(compact('json'));
589
590
        $this->set('_serialize', 'json');
591
    }
592
593
    /**
594
     * Delete a comment.
595
     *
596
     * @param int $id Id of the comment to delete.
597
     *
598
     * @return \Cake\Network\Response
599
     */
600
    public function deleteComment($id = null)
601
    {
602
        $this->loadModel('BlogArticlesComments');
603
604
        $comment = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
605
            ->find()
606
            ->contain([
607
                'BlogArticles'
608
            ])
609
            ->where([
610
                'BlogArticlesComments.id' => $id
611
            ])
612
            ->first();
613
614
        if (is_null($comment)) {
615
            $this->Flash->error(__("This comment doesn't exist or has been deleted !"));
616
617
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
618
        }
619
620
        //Current user.
621
        $this->loadModel('Users');
622
        $currentUser = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\BlogController>. 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...
623
            ->find()
624
            ->contain([
625
                'Groups' => function ($q) {
626
                    return $q->select(['id', 'is_staff']);
627
                }
628
            ])
629
            ->where([
630
                'Users.id' => $this->Auth->user('id')
631
            ])
632
            ->select(['id', 'group_id'])
633
            ->first();
634
635 View Code Duplication
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
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...
636
            $this->Flash->error(__("You don't have the authorization to delete this comment !"));
637
638
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
639
        }
640
641
        if ($this->BlogArticlesComments->delete($comment)) {
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
642
            $this->Flash->success(__("This comment has been deleted successfully !"));
643
        }
644
645
        return $this->redirect(['_name' => 'blog-article', 'slug' => $comment->blog_article->title, 'id' => $comment->blog_article->id, '?' => ['page' => $comment->blog_article->last_page]]);
646
    }
647
648
    /**
649
     * Get the form to edit a comment.
650
     *
651
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
652
     *
653
     * @return void
654
     */
655
    public function getEditComment()
656
    {
657
        if (!$this->request->is('ajax')) {
658
            throw new NotFoundException();
659
        }
660
661
        $this->loadModel('BlogArticlesComments');
662
        $comment = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
663
            ->find()
664
            ->where([
665
                'BlogArticlesComments.id' => $this->request->data['id']
666
            ])
667
            ->first();
668
669
        $json = [
670
            'error' => false,
671
            'errorMessage' => ''
672
        ];
673
674 View Code Duplication
        if (is_null($comment)) {
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...
675
            $json['error'] = true;
676
            $json['errorMessage'] = __("This comment doesn't exist or has been deleted !");
677
678
            $this->set(compact('json'));
679
680
            return;
681
        }
682
683
        //Current user.
684
        $this->loadModel('Users');
685
        $currentUser = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\BlogController>. 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...
686
            ->find()
687
            ->contain([
688
                'Groups' => function ($q) {
689
                    return $q->select(['id', 'is_staff']);
690
                }
691
            ])
692
            ->where([
693
                'Users.id' => $this->Auth->user('id')
694
            ])
695
            ->select(['id', 'group_id'])
696
            ->first();
697
698 View Code Duplication
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
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...
699
            $json['error'] = true;
700
            $json['errorMessage'] = __("You don't have the authorization to edit this comment !");
701
702
            $this->set(compact('json'));
703
704
            return;
705
        }
706
707
        $this->set(compact('json', 'comment'));
708
    }
709
710
    /**
711
     * Edit a comment.
712
     *
713
     * @param int $id Id of the comment.
714
     *
715
     * @return \Cake\Network\Response
716
     */
717
    public function editComment($id = null)
718
    {
719
        $this->loadModel('BlogArticlesComments');
720
721
        $comment = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
722
            ->find()
723
            ->contain([
724
                'BlogArticles'
725
            ])
726
            ->where([
727
                'BlogArticlesComments.id' => $id
728
            ])
729
            ->first();
730
731
        if (is_null($comment)) {
732
            $this->Flash->error(__("This comment doesn't exist or has been deleted !"));
733
734
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
735
        }
736
737
        //Current user.
738
        $this->loadModel('Users');
739
        $currentUser = $this->Users
0 ignored issues
show
Documentation introduced by
The property Users does not exist on object<App\Controller\BlogController>. 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...
740
            ->find()
741
            ->contain([
742
                'Groups' => function ($q) {
743
                    return $q->select(['id', 'is_staff']);
744
                }
745
            ])
746
            ->where([
747
                'Users.id' => $this->Auth->user('id')
748
            ])
749
            ->select(['id', 'group_id'])
750
            ->first();
751
752 View Code Duplication
        if ($comment->user_id != $this->Auth->user('id') && !$currentUser->group->is_staff) {
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...
753
            $this->Flash->error(__("You don't have the authorization to edit this comment !"));
754
755
            return $this->redirect($this->referer());
0 ignored issues
show
Bug introduced by
It seems like $this->referer() targeting Cake\Controller\Controller::referer() can also be of type object<Cake\Network\Request>; however, Cake\Controller\Controller::redirect() does only seem to accept string|array, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
756
        }
757
758
        $this->BlogArticlesComments->patchEntity($comment, $this->request->data());
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
759
        if ($this->BlogArticlesComments->save($comment)) {
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\BlogController>. 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...
760
            $this->Flash->success(__("This comment has been edited successfully !"));
761
        }
762
763
        return $this->redirect(['action' => 'go', $comment->id]);
764
    }
765
}
766