1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace andmemasin\surveybasemodels; |
4
|
|
|
|
5
|
|
|
use andmemasin\myabstract\MyActiveRecord; |
6
|
|
|
use yii; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* |
10
|
|
|
* @property integer $rejection_id |
11
|
|
|
* @property integer $survey_id |
12
|
|
|
* @property integer $respondent_id |
13
|
|
|
* @property string $email_address |
14
|
|
|
* @property string $type |
15
|
|
|
* @property string $bounce |
16
|
|
|
* @property string $time_rejected |
17
|
|
|
* |
18
|
|
|
* @property Respondent $respondent |
19
|
|
|
* @property Survey $survey |
20
|
|
|
* @property \stdClass $bounceObject The bounce as object |
21
|
|
|
* @property string $bounceReason |
22
|
|
|
* @property string $bounceReplyCode |
23
|
|
|
*/ |
24
|
|
|
class Rejection extends MyActiveRecord |
25
|
|
|
{ |
26
|
|
|
const BOUNCE_TYPE_HARD = 'hard'; |
27
|
|
|
const BOUNCE_TYPE_SOFT = 'soft'; |
28
|
|
|
const BOUNCE_TYPE_COMPLAINT = 'complaint'; |
29
|
|
|
const BOUNCE_TYPE_ANSWERED = 'answered'; |
30
|
|
|
const BOUNCE_TYPE_OTHER = 'other'; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
protected $respondentClass = Respondent::class; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public static function tableName() |
39
|
|
|
{ |
40
|
|
|
return 'rejection'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function rules() |
48
|
|
|
{ |
49
|
|
|
return array_merge([ |
50
|
|
|
[['survey_id', 'respondent_id'], 'integer'], |
51
|
|
|
[['time_rejected'], 'required'], |
52
|
|
|
[['bounce'], 'string'], |
53
|
|
|
[['time_rejected'], 'safe'], |
54
|
|
|
[['email_address'], 'string', 'max' => 255], |
55
|
|
|
[['email_address'], 'email'], |
56
|
|
|
[['type'], 'string', 'max' => 45], |
57
|
|
|
[['respondent_id'], 'exist', 'skipOnError' => true, 'targetClass' => Respondent::class, 'targetAttribute' => ['respondent_id' => 'respondent_id']], |
58
|
|
|
[['survey_id'], 'exist', 'skipOnError' => true, 'targetClass' => Survey::class, 'targetAttribute' => ['survey_id' => 'survey_id']], |
59
|
|
|
], parent::rules()); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $email_address |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public static function hasBouncedHard($email_address){ |
68
|
|
|
return (!empty(self::findHardBounces($email_address))); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $email_address |
75
|
|
|
* @return Rejection[] |
76
|
|
|
*/ |
77
|
|
|
public static function findHardBounces($email_address){ |
78
|
|
|
return Rejection::find() |
79
|
|
|
->andWhere(['email_address'=>$email_address, 'type' => self::BOUNCE_TYPE_HARD]) |
80
|
|
|
->all(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check whether we have a bounce registered from this email |
85
|
|
|
* @param string $email_address |
86
|
|
|
* @param string $type |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public static function bouncedByEmailAddress($email_address,$type = self::BOUNCE_TYPE_HARD){ |
90
|
|
|
$rejections = self::find() |
91
|
|
|
->andWhere('email_address=:email_address', [':email_address' => $email_address]) |
92
|
|
|
->andWhere('type=:type', [':type' => $type]); |
93
|
|
|
return $rejections->count() > 0; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $email_address |
98
|
|
|
* @return Rejection |
99
|
|
|
*/ |
100
|
|
|
public static function findByEmail($email_address){ |
101
|
|
|
return Rejection::findOne(['email_address'=>$email_address]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return \yii\db\ActiveQuery |
106
|
|
|
*/ |
107
|
|
|
public function getSurvey() |
108
|
|
|
{ |
109
|
|
|
return $this->hasOne(Survey::class, ['survey_id' => 'survey_id']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return \yii\db\ActiveQuery |
114
|
|
|
*/ |
115
|
|
|
public function getRespondent() |
116
|
|
|
{ |
117
|
|
|
return $this->hasOne($this->respondentClass, ['respondent_id' => 'respondent_id']); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public static function getBounceTypes(){ |
121
|
|
|
return [ |
122
|
|
|
self::BOUNCE_TYPE_COMPLAINT => Yii::t('app','Complaint'), |
123
|
|
|
self::BOUNCE_TYPE_SOFT => Yii::t('app','Soft bounce'), |
124
|
|
|
self::BOUNCE_TYPE_HARD => Yii::t('app','Hard bounce'), |
125
|
|
|
self::BOUNCE_TYPE_ANSWERED => Yii::t('app','Respondent has answered already'), |
126
|
|
|
self::BOUNCE_TYPE_OTHER => Yii::t('app','Other'), |
127
|
|
|
]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getBounceObject() |
131
|
|
|
{ |
132
|
|
|
$object = json_decode($this->bounce); |
133
|
|
|
if (empty($object)) { |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
return $object; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return null|string |
141
|
|
|
*/ |
142
|
|
|
public function getBounceReason() |
143
|
|
|
{ |
144
|
|
|
if (empty($this->bounceObject)) { |
145
|
|
|
return null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if(isset($this->bounceObject->diagnosticcode)) { |
149
|
|
|
return $this->bounceObject->diagnosticcode; |
150
|
|
|
} |
151
|
|
|
if(isset($this->bounceObject->reason)) { |
152
|
|
|
return $this->bounceObject->reason; |
153
|
|
|
} |
154
|
|
|
return null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @return null|string |
159
|
|
|
*/ |
160
|
|
|
public function getBounceReplyCode() |
161
|
|
|
{ |
162
|
|
|
if (empty($this->bounceObject)) { |
163
|
|
|
return null; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if(isset($this->bounceObject->deliverystatus)) { |
167
|
|
|
return $this->bounceObject->deliverystatus; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return null; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|