Completed
Push — master ( ec1997...dcb85b )
by Edgar
10:53
created

PushNotification::autoDetectProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 0
1
<?php
2
namespace nstdio\yii2notymo;
3
4
use nstdio\notymo\MessageInterface;
5
use nstdio\notymo\PushNotification as PushImpl;
6
use nstdio\yii2notymo\provider\DataProvider;
7
use Yii;
8
use yii\base\InvalidConfigException;
9
use yii\base\Object;
10
use yii\helpers\ArrayHelper;
11
12
/**
13
 * Class PushNotification
14
 *
15
 * @author Edgar Asatryan <[email protected]>
16
 */
17
class PushNotification extends Object
18
{
19
    /**
20
     * @var DataProvider
21
     */
22
    public $dataProvider;
23
24
    /**
25
     * @var PushImpl
26
     */
27
    protected $push;
28
29
    public function __construct($config = [])
30
    {
31
        if (!isset($config['push'])) {
32
            throw new InvalidConfigException("push configuration required.");
33
        }
34
        if (!isset($config['dataProvider'])) {
35
            throw new InvalidConfigException("dataProvider configuration required.");
36
        }
37
        if (!isset($config['dataProvider']['class'])) {
38
            $config['dataProvider']['class'] = $this->autoDetectProvider();
39
        }
40
        if (isset($config['push']['skipApns'])) {
41
            $config['dataProvider']['apns'] = null;
42
        }
43
        if (isset($config['push']['skipGcm'])) {
44
            $config['dataProvider']['gcm'] = null;
45
        }
46
47
        $this->push = new PushImpl($config['push']);
48
49
        $this->dataProvider = Yii::createObject($config['dataProvider']);
50
51
        if (!($this->dataProvider instanceof DataProvider)) {
52
            throw new InvalidConfigException("dataProvider must extend " . DataProvider::className() . " class.");
53
        }
54
55
        unset($config['push'], $config['dataProvider']);
56
57
        parent::__construct($config);
58
    }
59
60
    protected function autoDetectProvider()
61
    {
62
        foreach ($this->getDbConnectionList() as $service => $providerName) {
63
            if (Yii::$app->get($service, false) !== null) {
64
                return $providerName;
65
            }
66
        }
67
    }
68
69
    public function getDbConnectionList()
70
    {
71
        return [
72
            'db'      => 'nstdio\yii2notymo\provider\SQLDataProvider',
73
            'mongodb' => 'nstdio\yii2notymo\provider\MongoDataProvider',
74
        ];
75
    }
76
77
    public function send(MessageInterface $message, $userId = null)
78
    {
79
        $oldTokens = $message->getToken();
80
        $tokens = $this->dataProvider->getTokens($userId);
81
82
        if ($this->dataProvider->apns !== null) {
83
            $apnsTokens = ArrayHelper::getColumn($tokens, $this->dataProvider->apns);
84
            if (!empty($apnsTokens)) {
85
                $message->setType(MessageInterface::TYPE_IOS);
86
                $message->addToken($apnsTokens);
87
                $this->push->enqueue($message);
88
            }
89
        }
90
91
        if ($this->dataProvider->gcm !== null) {
92
            $gcmTokens = ArrayHelper::getColumn($tokens, $this->dataProvider->gcm);
93
94
            if (!empty($gcmTokens)) {
95
                $gcmMessage = clone $message;
96
                $gcmMessage->setType(MessageInterface::TYPE_ANDROID);
97
                $gcmMessage->setToken(ArrayHelper::merge($oldTokens, $gcmTokens));
98
99
                $this->push->enqueue($gcmMessage);
100
            }
101
        }
102
103
        $this->push->send();
104
    }
105
}