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

Statistics::newArticleStats()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
namespace App\Event;
3
4
use Cake\Cache\Cache;
5
use Cake\Event\Event;
6
use Cake\Event\EventListenerInterface;
7
use Cake\I18n\Number;
8
use Cake\ORM\TableRegistry;
9
10
class Statistics implements EventListenerInterface
11
{
12
13
    /**
14
     * ImplementedEvents method.
15
     *
16
     * @return array
17
     */
18
    public function implementedEvents()
19
    {
20
        return [
21
            'Model.Users.register' => 'newUserStats',
22
            'Model.Groups.update' => 'updateGroupStats',
23
            'Model.BlogArticles.new' => 'newArticleStats',
24
            'Model.BlogArticlesLikes.new' => 'newArticleLikeStats',
25
            'Model.BlogArticlesComments.new' => 'newArticleCommentStats'
26
        ];
27
    }
28
29
    /**
30
     * Count the articles and write it in the Cache.
31
     *
32
     * @param \Cake\Event\Event $event The event that was fired.
33
     *
34
     * @return array|false
35
     */
36 View Code Duplication
    public function newArticleStats(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
37
    {
38
        $this->BlogArticles = TableRegistry::get('BlogArticles');
0 ignored issues
show
Bug introduced by
The property BlogArticles does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
40
        $articles = $this->BlogArticles->find()->count();
41
        $articles = Number::format($articles);
42
43
        if ($this->_writeCache($articles, 'Articles')) {
44
            return $articles;
45
        }
46
47
        return false;
48
    }
49
50
    /**
51
     * Count the article's comments and write it in the Cache.
52
     *
53
     * @param \Cake\Event\Event $event The event that was fired.
54
     *
55
     * @return array|false
56
     */
57 View Code Duplication
    public function newArticleCommentStats(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
58
    {
59
        $this->BlogArticlesComments = TableRegistry::get('BlogArticlesComments');
0 ignored issues
show
Bug introduced by
The property BlogArticlesComments does not seem to exist. Did you mean BlogArticles?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
60
61
        $comments = $this->BlogArticlesComments->find()->count();
0 ignored issues
show
Bug introduced by
The property BlogArticlesComments does not seem to exist. Did you mean BlogArticles?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62
        $comments = Number::format($comments);
63
64
        if ($this->_writeCache($comments, 'ArticlesComments')) {
65
            return $comments;
66
        }
67
68
        return false;
69
    }
70
71
    /**
72
     * Count the article's likes and write it in the Cache.
73
     *
74
     * @param \Cake\Event\Event $event The event that was fired.
75
     *
76
     * @return array|false
77
     */
78 View Code Duplication
    public function newArticleLikeStats(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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...
79
    {
80
        $this->BlogArticlesLikes = TableRegistry::get('BlogArticlesLikes');
0 ignored issues
show
Bug introduced by
The property BlogArticlesLikes does not seem to exist. Did you mean BlogArticles?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
81
82
        $likes = $this->BlogArticlesLikes->find()->count();
0 ignored issues
show
Bug introduced by
The property BlogArticlesLikes does not seem to exist. Did you mean BlogArticles?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
83
        $likes = Number::format($likes);
84
85
        if ($this->_writeCache($likes, 'ArticlesLikes')) {
86
            return $likes;
87
        }
88
89
        return false;
90
    }
91
92
    /**
93
     * Re-count the number of user and find the latest user and write it in the Cache.
94
     *
95
     * @param \Cake\Event\Event $event The event that was fired.
96
     *
97
     * @return array|false
98
     */
99
    public function newUserStats(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        $this->Users = TableRegistry::get('Users');
0 ignored issues
show
Bug introduced by
The property Users does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
102
103
        $totalUsers = $this->Users->find()->count();
104
        $totalUsers = Number::format($totalUsers);
105
106
        $lastRegistered = $this->Users->find('short')->order(['Users.created' => 'DESC'])->first();
107
108
        $data = [];
109
        $data['TotalUsers'] = $totalUsers;
110
        $data['LastRegistered'] = $lastRegistered;
111
112
        if ($this->_writeCache($data, 'Users')) {
0 ignored issues
show
Documentation introduced by
$data is of type array<string,*,{"LastRegistered":"*"}>, but the function expects a integer|object|string.

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...
113
            return $data;
114
        }
115
116
        return false;
117
    }
118
119
    /**
120
     * Get the Groups and write it in the Cache.
121
     *
122
     * @param \Cake\Event\Event $event The event that was fired.
123
     *
124
     * @return array|false
125
     */
126
    public function updateGroupStats(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
127
    {
128
        $this->Groups = TableRegistry::get('Groups');
0 ignored issues
show
Bug introduced by
The property Groups does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
129
130
        $groups = $this->Groups->find('translations')->order(['Groups.id' => 'DESC'])->toArray();
131
132
        if ($this->_writeCache($groups, 'Groups')) {
0 ignored issues
show
Documentation introduced by
$groups is of type array, but the function expects a integer|object|string.

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...
133
            return $groups;
134
        }
135
136
        return false;
137
    }
138
139
    /**
140
     * Write the data into the Cache with the passed key.
141
     *
142
     * @param int|object|string $data The data to save in the Cache.
143
     * @param string $key The key to save the data.
144
     *
145
     * @return bool
146
     */
147
    protected function _writeCache($data, $key)
148
    {
149
        if (empty($data) || empty($key)) {
150
            return true;
151
        }
152
153
        $result = Cache::write($key, $data, 'statistics');
154
        if ($result) {
155
            return true;
156
        }
157
158
        return false;
159
    }
160
}
161