DbStorage::attributes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 4
c 1
b 0
f 1
nc 1
nop 0
dl 7
loc 7
rs 9.4285
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