AbstractStorage::factory()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 15
rs 9.2
cc 4
eloc 11
nc 6
nop 1
1
<?php
2
namespace jones\wschat\components;
3
4
use Yii;
5
use jones\wschat\collections\History;
6
7
/**
8
 * Class AbstractStorage
9
 *
10
 * Base class to create concrete implementer of message storing
11
 * @package jones\wschat\components
12
 */
13
abstract class AbstractStorage
14
{
15
    /**
16
     * Create instance of storage
17
     *
18
     * @access public
19
     * @static
20
     * @param string $storage default null
21
     * @return \jones\wschat\components\AbstractStorage
22
     */
23
    public static function factory($storage = null)
24
    {
25
        if (empty($storage)) {
26
            $components = Yii::$app->getComponents();
27
            $storage = !empty($components['mongodb']) ? 'mongodb' : Yii::$app->getDb()->driverName;
28
        }
29
        switch ($storage) {
30
            case 'mongodb':
31
                $class = new History();
32
                break;
33
            default:
34
                $class = new DbStorage();
35
        }
36
        return $class;
37
    }
38
39
    /**
40
     * Load chat history
41
     *
42
     * @access public
43
     * @param mixed $chatId
44
     * @param integer $limit
45
     * @return array
46
     */
47
    abstract public function getHistory($chatId, $limit = 10);
48
49
    /**
50
     * Store chat message
51
     *
52
     * @access public
53
     * @param array $params
54
     * @return boolean
55
     */
56
    abstract public function storeMessage(array $params);
57
}
58
59