Passed
Push — master ( 3614ed...50add2 )
by Vladimir
02:18
created

DbService::create()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
cc 4
eloc 11
nc 3
nop 1
1
<?php
2
/**
3
 * @link https://github.com/yiimaker/yii2-newsletter
4
 * @copyright Copyright (c) 2017 Yii Maker
5
 * @license BSD 3-Clause License
6
 */
7
8
namespace ymaker\newsletter\common\services;
9
10
use Yii;
11
use yii\base\Component;
12
use yii\base\InvalidConfigException;
13
use yii\data\ActiveDataProvider;
14
use yii\db\Connection;
15
use yii\di\Instance;
16
use ymaker\newsletter\common\events\SubscribeEvent;
17
use ymaker\newsletter\common\models\entities\NewsletterClient;
18
19
/**
20
 * Database service.
21
 *
22
 * @author Vladimir Kuprienko <[email protected]>
23
 * @since 1.0
24
 */
25
class DbService extends Component implements ServiceInterface
26
{
27
    const MODE_GENERIC = 1;
28
    const MODE_EMAIL = 2;
29
30
    /**
31
     * @var string|Connection
32
     */
33
    public $db = 'db';
34
    /**
35
     * @var int
36
     */
37
    private $_mode = self::MODE_EMAIL;
38
    /**
39
     * @var array
40
     */
41
    private $_errors = [];
42
    /**
43
     * @var NewsletterClient
44
     */
45
    private $_model;
46
47
48
    /**
49
     * Setter for mode.
50
     *
51
     * @param int $mode
52
     */
53
    public function setMode($mode)
54
    {
55
        $this->_mode = $mode;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getErrors()
62
    {
63
        return $this->_errors;
64
    }
65
66
    /**
67
     * @inheritdoc
68
     */
69
    public function init()
70
    {
71
        if ($this->_mode !== self::MODE_GENERIC && $this->_mode !== self::MODE_EMAIL) {
72
            throw new InvalidConfigException('Invalid mode config!');
73
        }
74
        $this->db = Instance::ensure($this->db, Connection::class);
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function getModel()
81
    {
82
        $this->_model = new NewsletterClient();
83
        return $this->_model;
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function getDataProvider()
90
    {
91
        return new ActiveDataProvider([
92
            'query' => NewsletterClient::find()->orderBy(['created_at' => SORT_ASC]),
93
            'db' => $this->db,
94
        ]);
95
    }
96
97
    /**
98
     * @inheritdoc
99
     */
100
    public function create(array $data)
101
    {
102
        $this->defineMode();
103
        if ($this->checkModel($data)) {
104
            try {
105
                if ($this->_model->insert(false)) {
106
                    $this->trigger(SubscribeEvent::EVENT_AFTER_SUBSCRIBE, Yii::createObject([
107
                        'class' => SubscribeEvent::class,
108
                        'contacts' => $this->_model->contacts,
109
                    ]));
110
                    return true;
111
                }
112
            } catch (\Exception $ex) {
113
                throw $ex;
114
            }
115
        }
116
        return false;
117
    }
118
119
    /**
120
     * Define mode for model.
121
     */
122
    protected function defineMode()
123
    {
124
        switch ($this->_mode) {
125
            case self::MODE_GENERIC:
126
                $this->_model->setScenario(NewsletterClient::SCENARIO_DEFAULT);
127
                break;
128
            case self::MODE_EMAIL:
129
                $this->_model->setScenario(NewsletterClient::SCENARIO_CONTACTS_EMAIL);
130
                break;
131
        }
132
    }
133
134
    /**
135
     * Process post data.
136
     *
137
     * @param array $data
138
     * @return bool
139
     */
140
    protected function checkModel(array $data)
141
    {
142
        if ($this->_model->load($data)) {
143
            if ($this->_model->validate()) {
144
                return true;
145
            }
146
            $this->_errors = $this->_model->getErrors();
147
        }
148
        return false;
149
    }
150
}
151