Completed
Push — master ( 300c8f...39e7ce )
by Tõnis
03:12
created

Rejection::getRespondent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace andmemasin\surveybasemodels;
4
use andmemasin\myabstract\MyActiveRecord;
5
use yii;
6
7
/**
8
 *
9
 * @property integer $rejection_id
10
 * @property integer $survey_id
11
 * @property integer $respondent_id
12
 * @property string $email_address
13
 * @property string $type
14
 * @property string $bounce
15
 * @property string $time_rejected
16
 *
17
 * @property Respondent $respondent
18
 * @property Survey $survey
19
 * @property \stdClass $bounceObject The bounce as object
20
 * @property string $bounceReason
21
 * @property string $bounceReplyCode
22
 */
23
class Rejection extends MyActiveRecord
24
{
25
    const BOUNCE_TYPE_HARD = 'hard';
26
    const BOUNCE_TYPE_SOFT = 'soft';
27
    const BOUNCE_TYPE_COMPLAINT = 'complaint';
28
    const BOUNCE_TYPE_ANSWERED = 'answered';
29
    const BOUNCE_TYPE_OTHER = 'other';
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public static function tableName()
35
    {
36
        return 'rejection';
37
    }
38
    
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function rules()
44
    {
45
        return array_merge([
46
            [['survey_id', 'respondent_id'], 'integer'],
47
            [['email_address','time_rejected'], 'required'],
48
            [['bounce'], 'string'],
49
            [['time_rejected'], 'safe'],
50
            [['email_address'], 'string', 'max' => 255],
51
            [['email_address'], 'email'],
52
            [['type'], 'string', 'max' => 45],
53
            [['respondent_id'], 'exist', 'skipOnError' => true, 'targetClass' => Respondent::class, 'targetAttribute' => ['respondent_id' => 'respondent_id']],
54
            [['survey_id'], 'exist', 'skipOnError' => true, 'targetClass' => Survey::class, 'targetAttribute' => ['survey_id' => 'survey_id']],
55
        ],  parent::rules());
56
    }
57
58
59
    /**
60
     * @param string $email_address
61
     * @return bool
62
     */
63
    public static function hasBouncedHard($email_address){
64
        return (!empty(self::findHardBounces($email_address)));
65
    }
66
67
68
69
    /**
70
     * @param string $email_address
71
     * @return Rejection[]
72
     */
73
    public static function findHardBounces($email_address){
74
        return Rejection::find()
75
            ->andWhere(['email_address'=>$email_address, 'type' => self::BOUNCE_TYPE_HARD])
76
            ->all();
77
    }
78
79
80
    /**
81
     * Check whether there are rejections that match the respondent's token
82
     * @param string $code
83
     * @return bool
84
     */
85
    public static function rejectedByCode($code){
86
        $respondent = Respondent::findByToken($code);
87
        if($respondent){
0 ignored issues
show
introduced by
$respondent is of type andmemasin\surveybasemodels\Respondent, thus it always evaluated to true.
Loading history...
88
            $rejections = self::find()
89
                ->andWhere(['respondent_id'=>$respondent->primaryKey])
90
                ->all();
91
            if($rejections){
0 ignored issues
show
Bug Best Practice introduced by
The expression $rejections of type yii\db\ActiveRecordInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
92
                return true;
93
            }
94
        }
95
        return false;
96
    }
97
98
    /**
99
     * Check whether we have a bounce registered from this email
100
     * @param string $email_address
101
     * @param string $type
102
     * @return bool
103
     */
104
    public static function bouncedByEmailAddress($email_address,$type = self::BOUNCE_TYPE_HARD){
105
        $rejections = self::find()
106
            ->andWhere('email_address=:email_address', [':email_address' => $email_address])
107
            ->andWhere('type=:type', [':type' => $type])
108
            ->all();
109
        if($rejections){
0 ignored issues
show
Bug Best Practice introduced by
The expression $rejections of type yii\db\ActiveRecordInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
110
            return true;
111
        }
112
        return false;
113
    }
114
115
    /**
116
     * @param string $email_address
117
     * @return Rejection
118
     */
119
    public static function findByEmail($email_address){
120
        return Rejection::findOne(['email_address'=>$email_address]);
121
    }
122
123
    /**
124
     * @return \yii\db\ActiveQuery
125
     */
126
    public function getSurvey()
127
    {
128
        return $this->hasOne(Survey::class, ['survey_id' => 'survey_id']);
129
    }
130
131
    /**
132
     * @return \yii\db\ActiveQuery
133
     */
134
    public function getRespondent()
135
    {
136
        return $this->hasOne(Respondent::class, ['respondent_id' => 'respondent_id']);
137
    }
138
139
    public static function getBounceTypes(){
140
        return [
141
            self::BOUNCE_TYPE_COMPLAINT => Yii::t('app','Complaint'),
142
            self::BOUNCE_TYPE_SOFT => Yii::t('app','Soft bounce'),
143
            self::BOUNCE_TYPE_HARD => Yii::t('app','Hard bounce'),
144
            self::BOUNCE_TYPE_ANSWERED => Yii::t('app','Respondent has answered already'),
145
            self::BOUNCE_TYPE_OTHER => Yii::t('app','Other'),
146
        ];
147
    }
148
149
    public function getBounceObject()
150
    {
151
        $object = json_decode($this->bounce);
152
        if (!empty($object)) {
153
            return $object;
154
        }
155
        return null;
156
    }
157
158
    /**
159
     * @return null|string
160
     */
161
    public function getBounceReason()
162
    {
163
        if (!empty($this->bounceObject)) {
164
            if(isset($this->bounceObject->diagnosticcode)) {
165
                return $this->bounceObject->diagnosticcode;
166
            }
167
            if(isset($this->bounceObject->reason)) {
168
                return $this->bounceObject->reason;
169
            }
170
        }
171
        return null;
172
    }
173
174
    /**
175
     * @return null|string
176
     */
177
    public function getBounceReplyCode()
178
    {
179
        if (!empty($this->bounceObject)) {
180
            if(isset($this->bounceObject->deliverystatus)) {
181
                return $this->bounceObject->deliverystatus;
182
            }
183
        }
184
        return null;
185
    }
186
}
187