Completed
Push — master ( ff2b0b...f369f8 )
by Alexey
05:58
created

SubscribeForm   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 16
eloc 34
c 2
b 0
f 0
dl 0
loc 152
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getFileStatePath() 0 3 1
A save() 0 3 1
A getFileSubscribePath() 0 3 1
A getTimestamp() 0 3 1
A init() 0 4 1
A isTimer() 0 3 1
A isEmail() 0 3 1
A getEmails() 0 3 1
A attributeLabels() 0 4 1
A send() 0 16 3
A isSubscribe() 0 3 1
A rules() 0 7 1
A subscribe() 0 6 2
1
<?php
2
3
namespace common\components\maintenance\models;
4
5
use Yii;
6
use yii\base\Model;
7
use common\components\maintenance\interfaces\StateInterface;
8
use yii\helpers\ArrayHelper;
9
use Exception;
10
use yii\helpers\VarDumper;
11
12
/**
13
 * Class SubscribeForm
14
 * @package common\components\maintenance\models
15
 *
16
 * @property string $fileStatePath
17
 * @property array $emails
18
 * @property string $datetime
19
 * @property int $timestamp
20
 * @property string $dateFormat
21
 * @property string $fileSubscribePath
22
 * @property string $email
23
 */
24
class SubscribeForm extends Model
25
{
26
    /**
27
     * @var string
28
     */
29
    public $email;
30
    /**
31
     * @var StateInterface
32
     */
33
    protected $state;
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function init()
39
    {
40
        parent::init();
41
        $this->state = Yii::$container->get(StateInterface::class);
42
    }
43
44
    /**
45
     * @inheritDoc
46
     * @return array
47
     */
48
    public function rules()
49
    {
50
        return [
51
            ['email', 'trim'],
52
            ['email', 'required'],
53
            ['email', 'email'],
54
            ['email', 'string', 'max' => 255],
55
        ];
56
    }
57
58
    /**
59
     * @inheritDoc
60
     * @return array
61
     */
62
    public function attributeLabels()
63
    {
64
        return [
65
            'email' => Yii::t('app', 'Your email'),
66
        ];
67
    }
68
69
    /**
70
     * Sending notifications to followers
71
     *
72
     * @param array $emails
73
     * @return int
74
     */
75
    public function send($emails = [])
76
    {
77
        $emails = $emails ?: $this->emails;
78
        $messages = [];
79
        $mailer = Yii::$app->mailer;
80
        foreach ($emails as $email) {
81
            $messages[] = $mailer->compose([
82
                'html' => '@common/components/maintenance/mail/emailNotice-html',
83
                'text' => '@common/components/maintenance/mail/emailNotice-text'
84
            ], [])
85
                ->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
86
                ->setTo($email)
87
                ->setSubject(Yii::t('app', 'Notification of completion of technical work'));
88
89
        }
90
        return $mailer->sendMultiple($messages);
91
    }
92
93
    /**
94
     * Save follower
95
     * @return bool
96
     */
97
    public function subscribe()
98
    {
99
        if ($this->isEmail()) {
100
            return false;
101
        }
102
        return $this->save();
103
    }
104
105
    /**
106
     * Timestamp
107
     * @return int
108
     * @throws Exception
109
     */
110
    public function getTimestamp()
111
    {
112
        return $this->state->timestamp();
0 ignored issues
show
Bug introduced by
The method timestamp() does not exist on common\components\mainte...terfaces\StateInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\components\mainte...terfaces\StateInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
        return $this->state->/** @scrutinizer ignore-call */ timestamp();
Loading history...
113
    }
114
115
    /**
116
     * Emails subscribe
117
     * @return array
118
     */
119
    public function getEmails()
120
    {
121
        return $this->state->emails();
0 ignored issues
show
Bug introduced by
The method emails() does not exist on common\components\mainte...terfaces\StateInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\components\mainte...terfaces\StateInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

121
        return $this->state->/** @scrutinizer ignore-call */ emails();
Loading history...
122
    }
123
124
    /**
125
     * Check email is subscribe
126
     * @return bool
127
     */
128
    public function isEmail()
129
    {
130
        return ArrayHelper::isIn($this->email, $this->emails);
131
    }
132
133
    /**
134
     * Timer show/hide
135
     * @return bool
136
     */
137
    public function isTimer()
138
    {
139
        return $this->state->isTimer();
0 ignored issues
show
Bug introduced by
The method isTimer() does not exist on common\components\mainte...terfaces\StateInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\components\mainte...terfaces\StateInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

139
        return $this->state->/** @scrutinizer ignore-call */ isTimer();
Loading history...
140
    }
141
142
    /**
143
     * Subscribe form on/off
144
     * @return bool will return true if on subscribe
145
     */
146
    public function isSubscribe()
147
    {
148
        return $this->state->isSubscribe();
0 ignored issues
show
Bug introduced by
The method isSubscribe() does not exist on common\components\mainte...terfaces\StateInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\components\mainte...terfaces\StateInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
        return $this->state->/** @scrutinizer ignore-call */ isSubscribe();
Loading history...
149
    }
150
151
    /**
152
     * Save email in file
153
     * @return bool
154
     */
155
    protected function save()
156
    {
157
        return $this->state->save($this->email, $this->getFileSubscribePath());
0 ignored issues
show
Bug introduced by
The method save() does not exist on common\components\mainte...terfaces\StateInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to common\components\mainte...terfaces\StateInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
        return $this->state->/** @scrutinizer ignore-call */ save($this->email, $this->getFileSubscribePath());
Loading history...
158
    }
159
160
    /**
161
     * Maintenance file path
162
     * @return string
163
     */
164
    protected function getFileStatePath()
165
    {
166
        return $this->state->path;
0 ignored issues
show
Bug introduced by
Accessing path on the interface common\components\mainte...terfaces\StateInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
167
    }
168
169
    /**
170
     * Subscribe file path
171
     * @return string
172
     */
173
    protected function getFileSubscribePath()
174
    {
175
        return $this->state->subscribePath;
0 ignored issues
show
Bug introduced by
Accessing subscribePath on the interface common\components\mainte...terfaces\StateInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
176
    }
177
}
178