DataProvider::init()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
nc 4
cc 5
eloc 7
nop 0
crap 5
1
<?php
2
namespace nstdio\yii2notymo\provider;
3
4
use yii\base\InvalidConfigException;
5
use yii\base\Object;
6
use yii\db\Query;
7
8
/**
9
 * Class DataProvider
10
 *
11
 * @package nstdio\yii2notymo\provider
12
 * @author  Edgar Asatryan <[email protected]>
13
 */
14
abstract class DataProvider extends Object
15
{
16
    /**
17
     * @var string The table name where device tokens stored.
18
     */
19
    public $table;
20
21
    /**
22
     * @var string
23
     */
24
    public $identifier;
25
26
    /**
27
     * @var string iOS device token table field name.
28
     */
29
    public $apns;
30
31
    /**
32
     * @var string Android device token table field name.
33
     */
34
    public $gcm;
35
36
    /**
37
     * @var Query
38
     */
39
    protected $query;
40
41
    /**
42
     * @param $identifiers
43
     *
44
     * @return array
45
     */
46 2
    public function getTokens($identifiers)
47
    {
48 2
        $this->query->from($this->table);
49
50 2
        $select = [];
51 2
        if ($this->apns !== null) {
52 2
            $select[] = $this->apns;
53 2
        }
54
55 2
        if ($this->gcm !== null) {
56 1
            $select[] = $this->gcm;
57 1
        }
58
59 2
        if (!empty($select)) {
60 2
            $this->query->select($select);
61 2
        }
62
63 2
        if (!empty($identifiers)) {
64 2
            $this->query->andWhere([$this->identifier => $identifiers]);
65 2
        }
66
67 2
        return $this->query->all();
68
    }
69
70 5
    public function init()
71
    {
72 5
        if (!$this->table) {
73 1
            throw new InvalidConfigException("You must specify table.");
74
        }
75
76 4
        if (!$this->identifier) {
77 1
            throw new InvalidConfigException("You must specify identifier.");
78
        }
79
80 3
        if (!$this->apns && !$this->gcm) {
81 1
            throw new InvalidConfigException("At least one of apns or gcm properties must be initialized.");
82
        }
83
    }
84
}