Skip to content

Commit 47b9517

Browse files
Upgrade Plugin for Cake 4.x (#37)
* Updated Dependencies * Refactored to Mailer Class * Updated PHPCS * Fix overrides + phpcs * Fix Custom Data Types * Fix PHPCS * Fix Tests * Update gitignore * Fix misc * Fix type hinting
1 parent e298e33 commit 47b9517

15 files changed

+146
-181
lines changed

.editorconfig

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ trim_trailing_whitespace = true
1414
end_of_line = crlf
1515

1616
[*.yml]
17-
indent_style = space
1817
indent_size = 2
18+
19+
[*.twig]
20+
insert_final_newline = false
21+
22+
[Makefile]
23+
indent_style = tab

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
vendor
22
.idea
33
.DS_Store
4-
composer.lock
4+
composer.lock
5+
/.phpunit.result.cache

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ email templates and watching the result.
99

1010
## Requirements ##
1111

12-
* CakePHP 3.7
12+
* CakePHP 4.x
1313

1414
## Installation ##
1515

composer.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
}
1313
],
1414
"require": {
15-
"cakephp/cakephp": "~3.6"
15+
"cakephp/cakephp": "~4.0"
1616
},
1717
"require-dev": {
18-
"phpunit/phpunit": "^5.7|^6.0",
19-
"cakephp/cakephp-codesniffer": "^3.0",
18+
"phpunit/phpunit": "^8.5",
19+
"cakephp/cakephp-codesniffer": "^4.0",
2020
"php-coveralls/php-coveralls": "^2.1"
2121
},
2222
"autoload": {
@@ -27,7 +27,8 @@
2727
"autoload-dev": {
2828
"psr-4": {
2929
"EmailQueue\\Test\\": "tests",
30-
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
30+
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests",
31+
"TestApp\\": "tests/test_app/src/"
3132
}
3233
}
3334
}

phpunit.xml.dist

-8
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
colors="true"
44
processIsolation="false"
55
stopOnFailure="false"
6-
syntaxCheck="false"
76
bootstrap="./tests/bootstrap.php"
87
>
98
<php>
@@ -34,13 +33,6 @@
3433
<whitelist processUncoveredFilesFromWhitelist="true">
3534
<directory suffix=".php">./src</directory>
3635
</whitelist>
37-
<blacklist>
38-
<directory suffix=".php">./vendor/</directory>
39-
<directory suffix=".ctp">./vendor/</directory>
40-
41-
<directory suffix=".php">./tests/</directory>
42-
<directory suffix=".ctp">./tests/</directory>
43-
</blacklist>
4436
</filter>
4537

4638
</phpunit>

src/Database/Type/JsonType.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace EmailQueue\Database\Type;
35

4-
use Cake\Database\Driver;
5-
use Cake\Database\Type\StringType;
6+
use Cake\Database\DriverInterface;
7+
use Cake\Database\Type\BaseType;
8+
use Cake\Database\Type\OptionalConvertInterface;
69

7-
class JsonType extends StringType
10+
class JsonType extends BaseType implements OptionalConvertInterface
811
{
912
/**
1013
* Decodes a JSON string
1114
*
1215
* @param mixed $value json string to decode
13-
* @param Driver $driver database driver
16+
* @param \Cake\Database\DriverInterface $driver database driver
1417
* @return mixed|null|string|void
1518
*/
16-
public function toPHP($value, Driver $driver)
19+
public function toPHP($value, DriverInterface $driver)
1720
{
1821
if ($value === null) {
1922
return;
@@ -41,10 +44,10 @@ public function marshal($value)
4144
* Returns the JSON representation of a value
4245
*
4346
* @param mixed $value string or object to encode
44-
* @param Driver $driver database driver
47+
* @param \Cake\Database\DriverInterface $driver database driver
4548
* @return null|string
4649
*/
47-
public function toDatabase($value, Driver $driver)
50+
public function toDatabase($value, DriverInterface $driver): ?string
4851
{
4952
return json_encode($value);
5053
}
@@ -54,7 +57,7 @@ public function toDatabase($value, Driver $driver)
5457
*
5558
* @return bool always true
5659
*/
57-
public function requiresToPhpCast()
60+
public function requiresToPhpCast(): bool
5861
{
5962
return true;
6063
}

src/Database/Type/SerializeType.php

+20-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace EmailQueue\Database\Type;
35

4-
use Cake\Database\Driver;
5-
use Cake\Database\Type\StringType;
6+
use Cake\Database\DriverInterface;
7+
use Cake\Database\Type\BaseType;
8+
use Cake\Database\Type\OptionalConvertInterface;
69

7-
class SerializeType extends StringType
10+
class SerializeType extends BaseType implements OptionalConvertInterface
811
{
912
/**
1013
* Creates a PHP value from a stored representation
1114
*
1215
* @param mixed $value to unserialize
13-
* @param Driver $driver database driver
16+
* @param \Cake\Database\DriverInterface $driver database driver
1417
* @return mixed|null|string|void
1518
*/
16-
public function toPHP($value, Driver $driver)
19+
public function toPHP($value, DriverInterface $driver)
1720
{
1821
if ($value === null) {
19-
return;
22+
return null;
2023
}
2124

2225
return unserialize($value);
@@ -26,11 +29,19 @@ public function toPHP($value, Driver $driver)
2629
* Generates a storable representation of a value
2730
*
2831
* @param mixed $value to serialize
29-
* @param Driver $driver database driver
32+
* @param \Cake\Database\Driver $driver database driver
3033
* @return null|string
3134
*/
32-
public function toDatabase($value, Driver $driver)
35+
public function toDatabase($value, DriverInterface $driver): ?string
3336
{
37+
if ($value === null || is_string($value)) {
38+
return $value;
39+
}
40+
41+
if (is_object($value) && method_exists($value, '__toString')) {
42+
return $value->__toString();
43+
}
44+
3445
return serialize($value);
3546
}
3647

@@ -50,7 +61,7 @@ public function marshal($value)
5061
*
5162
* @return bool always true
5263
*/
53-
public function requiresToPhpCast()
64+
public function requiresToPhpCast(): bool
5465
{
5566
return true;
5667
}

src/EmailQueue.php

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace EmailQueue;
35

46
use Cake\ORM\TableRegistry;

src/Model/Table/EmailQueueTable.php

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace EmailQueue\Model\Table;
35

46
use Cake\Core\Configure;
57
use Cake\Database\Expression\QueryExpression;
6-
use Cake\Database\Schema\TableSchema;
8+
use Cake\Database\Schema\TableSchemaInterface;
79
use Cake\Database\Type;
810
use Cake\I18n\FrozenTime;
911
use Cake\ORM\Table;
@@ -16,12 +18,12 @@
1618
*/
1719
class EmailQueueTable extends Table
1820
{
19-
const MAX_TEMPLATE_LENGTH = 100;
21+
public const MAX_TEMPLATE_LENGTH = 100;
2022

2123
/**
2224
* {@inheritdoc}
2325
*/
24-
public function initialize(array $config = [])
26+
public function initialize(array $config = []): void
2527
{
2628
Type::map('email_queue.json', JsonType::class);
2729
Type::map('email_queue.serialize', SerializeType::class);
@@ -31,9 +33,9 @@ public function initialize(array $config = [])
3133
'events' => [
3234
'Model.beforeSave' => [
3335
'created' => 'new',
34-
'modified' => 'always'
35-
]
36-
]
36+
'modified' => 'always',
37+
],
38+
],
3739
]
3840
);
3941
}
@@ -53,12 +55,12 @@ public function initialize(array $config = [])
5355
* - config : the name of the email config to be used for sending
5456
*
5557
* @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
5759
* @return bool
5860
*/
59-
public function enqueue($to, array $data, array $options = [])
61+
public function enqueue($to, array $data, array $options = []): bool
6062
{
61-
if (strlen($options['template']) > self::MAX_TEMPLATE_LENGTH) {
63+
if (array_key_exists('template', $options) && strlen($options['template']) > self::MAX_TEMPLATE_LENGTH) {
6264
throw new LengthException('`template` length must be less or equal to ' . self::MAX_TEMPLATE_LENGTH);
6365
}
6466

@@ -72,7 +74,7 @@ public function enqueue($to, array $data, array $options = [])
7274
'headers' => [],
7375
'template_vars' => $data,
7476
'config' => 'default',
75-
'attachments' => []
77+
'attachments' => [],
7678
];
7779

7880
$email = $options + $defaults;
@@ -101,11 +103,11 @@ public function enqueue($to, array $data, array $options = [])
101103
/**
102104
* Returns a list of queued emails that needs to be sent.
103105
*
104-
* @param int $size number of unset emails to return
106+
* @param int|string $size number of unset emails to return
105107
* @throws \Exception any exception raised in transactional callback
106108
* @return array list of unsent emails
107109
*/
108-
public function getBatch($size = 10)
110+
public function getBatch($size = 10): array
109111
{
110112
return $this->getConnection()->transactional(function () use ($size) {
111113
$emails = $this->find()
@@ -135,11 +137,11 @@ public function getBatch($size = 10)
135137
/**
136138
* Releases locks for all emails in $ids.
137139
*
138-
* @param array|Traversable $ids The email ids to unlock
140+
* @param array|\Traversable $ids The email ids to unlock
139141
*
140142
* @return void
141143
*/
142-
public function releaseLocks($ids)
144+
public function releaseLocks($ids): void
143145
{
144146
$this->updateAll(['locked' => false], ['id IN' => $ids]);
145147
}
@@ -149,7 +151,7 @@ public function releaseLocks($ids)
149151
*
150152
* @return void
151153
*/
152-
public function clearLocks()
154+
public function clearLocks(): void
153155
{
154156
$this->updateAll(['locked' => false], '1=1');
155157
}
@@ -160,7 +162,7 @@ public function clearLocks()
160162
* @param string $id queued email id
161163
* @return void
162164
*/
163-
public function success($id)
165+
public function success($id): void
164166
{
165167
$this->updateAll(['sent' => true], ['id' => $id]);
166168
}
@@ -172,26 +174,26 @@ public function success($id)
172174
* @param string $error message
173175
* @return void
174176
*/
175-
public function fail($id, $error = null)
177+
public function fail($id, $error = null): void
176178
{
177179
$this->updateAll(
178180
[
179181
'send_tries' => new QueryExpression('send_tries + 1'),
180-
'error' => $error
182+
'error' => $error,
181183
],
182184
[
183-
'id' => $id
185+
'id' => $id,
184186
]
185187
);
186188
}
187189

188190
/**
189191
* Sets the column type for template_vars and headers to json.
190192
*
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
193195
*/
194-
protected function _initializeSchema(TableSchema $schema)
196+
protected function _initializeSchema(TableSchemaInterface $schema): TableSchemaInterface
195197
{
196198
$type = Configure::read('EmailQueue.serialization_type') ?: 'email_queue.serialize';
197199
$schema->setColumnType('template_vars', $type);

src/Shell/PreviewShell.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace EmailQueue\Shell;
35

46
use Cake\Console\Shell;
57
use Cake\Core\Configure;
6-
use Cake\Mailer\Email;
8+
use Cake\Mailer\Mailer;
79
use Cake\ORM\TableRegistry;
810
use EmailQueue\Model\Table\EmailQueueTable;
911

@@ -23,7 +25,7 @@ public function main()
2325
$conditions['id IN'] = $this->args;
2426
}
2527

26-
$emailQueue = TableRegistry::get('EmailQueue', ['className' => EmailQueueTable::class]);
28+
$emailQueue = TableRegistry::getTableLocator()->get('EmailQueue', ['className' => EmailQueueTable::class]);
2729
$emails = $emailQueue->find()->where($conditions)->toList();
2830

2931
if (!$emails) {
@@ -57,7 +59,7 @@ public function preview($e)
5759
$headers = empty($e['headers']) ? [] : (array)$e['headers'];
5860
$theme = empty($e['theme']) ? '' : (string)$e['theme'];
5961

60-
$email = new Email($configName);
62+
$email = new Mailer($configName);
6163

6264
if (!empty($e['attachments'])) {
6365
$email->setAttachments($e['attachments']);
@@ -77,7 +79,7 @@ public function preview($e)
7779
->setTemplate($template)
7880
->setLayout($layout);
7981

80-
$return = $email->send();
82+
$return = $email->deliver();
8183

8284
$this->out('Content:');
8385
$this->hr();
@@ -91,6 +93,6 @@ public function preview($e)
9193
$this->hr();
9294
debug($e['template_vars']);
9395
$this->hr();
94-
$this->out();
96+
$this->out('');
9597
}
9698
}

0 commit comments

Comments
 (0)