Completed
Push — master ( 9c742a...1858b1 )
by Fèvre
14s
created

PagesController::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
namespace App\Controller;
3
4
use App\Event\Statistics;
5
use Cake\Cache\Cache;
6
use Cake\Core\Configure;
7
use Cake\Event\Event;
8
use Cake\Network\Exception\NotFoundException;
9
10
class PagesController extends AppController
11
{
12
13
    /**
14
     * Initialization hook method.
15
     *
16
     * @return void
17
     */
18
    public function initialize()
19
    {
20
        parent::initialize();
21
22
        $this->loadComponent('RequestHandler');
23
    }
24
25
    /**
26
     * Beforefilter.
27
     *
28
     * @param Event $event The beforeFilter event that was fired.
29
     *
30
     * @return void
31
     */
32
    public function beforeFilter(Event $event)
33
    {
34
        parent::beforeFilter($event);
35
36
        $this->Auth->allow(['home', 'maintenance', 'acceptCookie', 'lang', 'terms']);
37
    }
38
39
    /**
40
     * Home page.
41
     *
42
     * @return void
43
     */
44
    public function home()
45
    {
46
        $this->loadModel('BlogArticles');
47
        $this->loadModel('BlogArticlesComments');
48
49
        $articles = $this->BlogArticles
0 ignored issues
show
Documentation introduced by
The property BlogArticles does not exist on object<App\Controller\PagesController>. 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...
50
            ->find()
51
            ->contain([
52
                'BlogCategories',
53
                'Users'
54
            ])
55
            ->order([
56
                'BlogArticles.created' => 'desc'
57
            ])
58
            ->limit(Configure::read('Home.articles'))
59
            ->where([
60
                'BlogArticles.is_display' => 1
61
            ]);
62
63
        $comments = $this->BlogArticlesComments
0 ignored issues
show
Documentation introduced by
The property BlogArticlesComments does not exist on object<App\Controller\PagesController>. 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...
64
            ->find()
65
            ->contain([
66
                'BlogArticles' => function ($q) {
67
                    return $q
68
                        ->select([
69
                            'title'
70
                        ]);
71
                },
72
                'Users' => function ($q) {
73
                    return $q->find('medium');
74
                }
75
            ])
76
            ->order([
77
                    'BlogArticlesComments.created' => 'desc'
78
            ])
79
            ->limit(Configure::read('Home.comments'))
80
            ->where([
81
                'BlogArticles.is_display' => 1
82
            ]);
83
84
        $statistics = $this->_buildStats([
85
            'Users' => 'Model.Users.register',
86
            'Articles' => 'Model.BlogArticles.new',
87
            'ArticlesLikes' => 'Model.BlogArticlesLikes.new',
88
            'ArticlesComments' => 'Model.BlogArticlesComments.new'
89
        ]);
90
91
        $this->set(compact('articles', 'comments', 'statistics'));
92
    }
93
94
    /**
95
     * Build the statistics for the home page.
96
     *
97
     * @param array $array The array of statistics to build.
98
     *
99
     * @return array
100
     */
101 View Code Duplication
    protected function _buildStats(array $array)
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...
102
    {
103
        $statistics = [];
104
105
        foreach ($array as $type => $event) {
106
            $statistics[$type] = Cache::remember($type, function () {
107
                $this->eventManager()->attach(new Statistics());
0 ignored issues
show
Documentation introduced by
new \App\Event\Statistics() is of type object<App\Event\Statistics>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The method Cake\Event\EventManager::attach() has been deprecated with message: 3.0.0 Use on() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
108
                $event = new Event($event);
0 ignored issues
show
Bug introduced by
The variable $event seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
109
                $this->eventManager()->dispatch($event);
110
111
                return $event->result;
112
            }, 'statistics');
113
        }
114
115
        return $statistics;
116
    }
117
118
    /**
119
     * The user accept the use of cookies.
120
     *
121
     * @throws \Cake\Network\Exception\NotFoundException When it's not an AJAX request.
122
     *
123
     * @return void
124
     */
125
    public function acceptCookie()
126
    {
127
        if (!$this->request->is('ajax')) {
128
            throw new NotFoundException();
129
        }
130
131
        $this->Cookie->configKey('allowCookies', [
132
            'expires' => '+1 year',
133
            'httpOnly' => true
134
        ]);
135
        $this->Cookie->write('allowCookies', 'true');
136
137
        $json = [];
138
        $json['message'] = __('Thanks for accepting to use the cookies !');
139
        $this->set(compact('json'));
140
141
        $this->set('_serialize', 'json');
142
    }
143
144
    /**
145
     * Redirect to the referer.
146
     *
147
     * @return void
148
     */
149
    public function lang()
150
    {
151
        $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...
152
    }
153
154
    /**
155
     * Display the maintenance page or a 404 if the site isn't in maintenance.
156
     *
157
     * @return void
158
     */
159
    public function maintenance()
160
    {
161
        if (Configure::read('Site.maintenance') === false) {
162
            throw new NotFoundException();
163
        }
164
    }
165
166
    /**
167
     * Display the Terms page.
168
     *
169
     * @return void
170
     */
171
    public function terms()
172
    {
173
    }
174
}
175