1
1
<?php
2
+ declare (strict_types=1 );
3
+
2
4
namespace EmailQueue \Model \Table ;
3
5
4
6
use Cake \Core \Configure ;
5
7
use Cake \Database \Expression \QueryExpression ;
6
- use Cake \Database \Schema \TableSchema ;
8
+ use Cake \Database \Schema \TableSchemaInterface ;
7
9
use Cake \Database \Type ;
8
10
use Cake \I18n \FrozenTime ;
9
11
use Cake \ORM \Table ;
16
18
*/
17
19
class EmailQueueTable extends Table
18
20
{
19
- const MAX_TEMPLATE_LENGTH = 100 ;
21
+ public const MAX_TEMPLATE_LENGTH = 100 ;
20
22
21
23
/**
22
24
* {@inheritdoc}
23
25
*/
24
- public function initialize (array $ config = [])
26
+ public function initialize (array $ config = []): void
25
27
{
26
28
Type::map ('email_queue.json ' , JsonType::class);
27
29
Type::map ('email_queue.serialize ' , SerializeType::class);
@@ -31,9 +33,9 @@ public function initialize(array $config = [])
31
33
'events ' => [
32
34
'Model.beforeSave ' => [
33
35
'created ' => 'new ' ,
34
- 'modified ' => 'always '
35
- ]
36
- ]
36
+ 'modified ' => 'always ' ,
37
+ ],
38
+ ],
37
39
]
38
40
);
39
41
}
@@ -53,12 +55,12 @@ public function initialize(array $config = [])
53
55
* - config : the name of the email config to be used for sending
54
56
*
55
57
* @throws \Exception any exception raised in transactional callback
56
- * @throws LengthException If `template` option length is greater than maximum allowed length
58
+ * @throws \ LengthException If `template` option length is greater than maximum allowed length
57
59
* @return bool
58
60
*/
59
- public function enqueue ($ to , array $ data , array $ options = [])
61
+ public function enqueue ($ to , array $ data , array $ options = []): bool
60
62
{
61
- if (strlen ($ options ['template ' ]) > self ::MAX_TEMPLATE_LENGTH ) {
63
+ if (array_key_exists ( ' template ' , $ options ) && strlen ($ options ['template ' ]) > self ::MAX_TEMPLATE_LENGTH ) {
62
64
throw new LengthException ('`template` length must be less or equal to ' . self ::MAX_TEMPLATE_LENGTH );
63
65
}
64
66
@@ -72,7 +74,7 @@ public function enqueue($to, array $data, array $options = [])
72
74
'headers ' => [],
73
75
'template_vars ' => $ data ,
74
76
'config ' => 'default ' ,
75
- 'attachments ' => []
77
+ 'attachments ' => [],
76
78
];
77
79
78
80
$ email = $ options + $ defaults ;
@@ -101,11 +103,11 @@ public function enqueue($to, array $data, array $options = [])
101
103
/**
102
104
* Returns a list of queued emails that needs to be sent.
103
105
*
104
- * @param int $size number of unset emails to return
106
+ * @param int|string $size number of unset emails to return
105
107
* @throws \Exception any exception raised in transactional callback
106
108
* @return array list of unsent emails
107
109
*/
108
- public function getBatch ($ size = 10 )
110
+ public function getBatch ($ size = 10 ): array
109
111
{
110
112
return $ this ->getConnection ()->transactional (function () use ($ size ) {
111
113
$ emails = $ this ->find ()
@@ -135,11 +137,11 @@ public function getBatch($size = 10)
135
137
/**
136
138
* Releases locks for all emails in $ids.
137
139
*
138
- * @param array|Traversable $ids The email ids to unlock
140
+ * @param array|\ Traversable $ids The email ids to unlock
139
141
*
140
142
* @return void
141
143
*/
142
- public function releaseLocks ($ ids )
144
+ public function releaseLocks ($ ids ): void
143
145
{
144
146
$ this ->updateAll (['locked ' => false ], ['id IN ' => $ ids ]);
145
147
}
@@ -149,7 +151,7 @@ public function releaseLocks($ids)
149
151
*
150
152
* @return void
151
153
*/
152
- public function clearLocks ()
154
+ public function clearLocks (): void
153
155
{
154
156
$ this ->updateAll (['locked ' => false ], '1=1 ' );
155
157
}
@@ -160,7 +162,7 @@ public function clearLocks()
160
162
* @param string $id queued email id
161
163
* @return void
162
164
*/
163
- public function success ($ id )
165
+ public function success ($ id ): void
164
166
{
165
167
$ this ->updateAll (['sent ' => true ], ['id ' => $ id ]);
166
168
}
@@ -172,26 +174,26 @@ public function success($id)
172
174
* @param string $error message
173
175
* @return void
174
176
*/
175
- public function fail ($ id , $ error = null )
177
+ public function fail ($ id , $ error = null ): void
176
178
{
177
179
$ this ->updateAll (
178
180
[
179
181
'send_tries ' => new QueryExpression ('send_tries + 1 ' ),
180
- 'error ' => $ error
182
+ 'error ' => $ error,
181
183
],
182
184
[
183
- 'id ' => $ id
185
+ 'id ' => $ id,
184
186
]
185
187
);
186
188
}
187
189
188
190
/**
189
191
* Sets the column type for template_vars and headers to json.
190
192
*
191
- * @param TableSchema $schema The table description
192
- * @return TableSchema
193
+ * @param \Cake\Database\Schema\TableSchemaInterface $schema The table description
194
+ * @return \Cake\Database\Schema\ TableSchema
193
195
*/
194
- protected function _initializeSchema (TableSchema $ schema )
196
+ protected function _initializeSchema (TableSchemaInterface $ schema ): TableSchemaInterface
195
197
{
196
198
$ type = Configure::read ('EmailQueue.serialization_type ' ) ?: 'email_queue.serialize ' ;
197
199
$ schema ->setColumnType ('template_vars ' , $ type );
0 commit comments