Completed
Pull Request — master (#178)
by Fèvre
02:40
created

Statistics   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 0
loc 92
rs 10
c 1
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A implementedEvents() 0 7 1
A newUserStats() 0 19 2
A updateGroupStats() 0 12 2
A _writeCache() 0 13 4
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
     * Prefix used for the Cache keys.
15
     *
16
     * @var string
17
     */
18
    protected $_prefix = 'statistics';
19
20
    /**
21
     * ImplementedEvents method.
22
     *
23
     * @return array
24
     */
25
    public function implementedEvents()
26
    {
27
        return [
28
            'Model.Users.register' => 'newUserStats',
29
            'Model.Groups.update' => 'updateGroupStats'
30
        ];
31
    }
32
33
    /**
34
     * Re-count the number of user and find the latest user and write it in the Cache.
35
     *
36
     * @param \Cake\Event\Event $event The event that was fired.
37
     *
38
     * @return array|false
39
     */
40
    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...
41
    {
42
        $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...
43
44
        $totalUsers = $this->Users->find()->count();
45
        $totalUsers = Number::format($totalUsers);
46
47
        $lastRegistered = $this->Users->find('short')->order(['Users.created' => 'DESC'])->first();
48
49
        $data = [];
50
        $data['TotalUsers'] = $totalUsers;
51
        $data['LastRegistered'] = $lastRegistered;
52
53
        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...
54
            return $data;
55
        }
56
57
        return false;
58
    }
59
60
    /**
61
     * Get the Groups and write it in the Cache.
62
     *
63
     * @param \Cake\Event\Event $event The event that was fired.
64
     *
65
     * @return array|false
66
     */
67
    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...
68
    {
69
        $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...
70
71
        $groups = $this->Groups->find('translations')->order(['Groups.id' => 'DESC'])->toArray();
72
73
        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...
74
            return $groups;
75
        }
76
77
        return false;
78
    }
79
80
    /**
81
     * Write the data into the Cache with the passed key.
82
     *
83
     * @param int|object|string $data The data to save in the Cache.
84
     * @param string $key The key to save the data.
85
     *
86
     * @return bool
87
     */
88
    protected function _writeCache($data, $key)
89
    {
90
        if (empty($data) || empty($key)) {
91
            return true;
92
        }
93
94
        $result = Cache::write($this->_prefix . $key, $data, 'forum');
95
        if ($result) {
96
            return true;
97
        }
98
99
        return false;
100
    }
101
}
102