DbStorage   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 31.67 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 6
c 2
b 1
f 1
lcom 0
cbo 4
dl 19
loc 60
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A tableName() 0 4 1
A attributes() 7 7 1
A getHistory() 12 12 2
A storeMessage() 0 12 2

How to fix   Duplicated Code   

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:

1
<?php
2
namespace jones\wschat\components;
3
4
use yii;
5
use yii\db\Query;
6
use yii\db\Exception;
7
8
/**
9
 * Class DbStorage
10
 *
11
 * This class is database storage implementation for chat messages storing
12
 * @package jones\wschat\components
13
 */
14
class DbStorage extends AbstractStorage
15
{
16
    /**
17
     * Get name of table
18
     *
19
     * @access public
20
     * @static
21
     * @return string
22
     */
23
    public static function tableName()
24
    {
25
        return 'history';
26
    }
27
28
    /**
29
     * Get list of attributes
30
     *
31
     * @access public
32
     * @return array
33
     */
34 View Code Duplication
    public function attributes()
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...
35
    {
36
        return [
37
            'id', 'chat_id', 'chat_title', 'user_id', 'username', 'avatar_16',
38
            'avatar_32', 'timestamp', 'message'
39
        ];
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45 View Code Duplication
    public function getHistory($chatId, $limit = 10)
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...
46
    {
47
        $query = new Query();
48
        $query->select(['user_id', 'username', 'message', 'timestamp', 'avatar_16', 'avatar_32'])
49
            ->from(self::tableName())
50
            ->where(['chat_id' => $chatId]);
51
        $query->orderBy(['timestamp' => SORT_DESC]);
52
        if ($limit) {
53
            $query->limit($limit);
54
        }
55
        return $query->all();
56
    }
57
58
    /**
59
     * @inheritdoc
60
     */
61
    public function storeMessage(array $params)
62
    {
63
        try {
64
            Yii::$app->getDb()->createCommand()
65
                ->insert(self::tableName(), $params)
66
                ->execute();
67
        } catch (Exception $e) {
68
            Yii::error($e->getMessage());
69
            return false;
70
        }
71
        return true;
72
    }
73
}
74
75