-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMailQueueDeleteBehavior.php
More file actions
100 lines (89 loc) · 2.98 KB
/
MailQueueDeleteBehavior.php
File metadata and controls
100 lines (89 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* メールキュー削除 Behavior
*
* @author Noriko Arai <arai@nii.ac.jp>
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('ModelBehavior', 'Model');
/**
* メールキュー削除 Behavior
*
* @author Mitsuru Mutaguchi <mutaguchi@opensource-workshop.jp>
* @package NetCommons\Mails\Model\Behavior
*/
class MailQueueDeleteBehavior extends ModelBehavior {
/**
* @var bool 削除済み
*/
private $__isDeleted = null;
/**
* setup
*
* @param Model $model モデル
* @param array $settings 設定値
* @return void
* @link http://book.cakephp.org/2.0/ja/models/behaviors.html#ModelBehavior::setup
*/
public function setup(Model $model, $settings = array()) {
$this->settings[$model->alias] = $settings;
$this->__isDeleted = false;
}
/**
* beforeDelete
* コンテンツが削除されたら、キューに残っているメールも削除
*
* @param Model $model モデル
* @param bool $cascade If true records that depend on this record will also be deleted
* @return mixed False if the operation should abort. Any other result will continue.
* @throws InternalErrorException
* @link http://book.cakephp.org/2.0/ja/models/behaviors.html#ModelBehavior::beforedelete
* @link http://book.cakephp.org/2.0/ja/models/callback-methods.html#beforedelete
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function beforeDelete(Model $model, $cascade = true) {
// 多言語のコンテンツを key を使って、Model::deleteAll() で削除した場合を想定
// 削除済みなら、もう処理をしない
if ($this->__isDeleted) {
return;
}
// コンテンツキー取得
$content = $model->find('first', array(
'recursive' => -1,
'conditions' => array($model->alias . '.id' => $model->id),
'callbacks' => false,
));
$contentKey = $content[$model->alias]['key'];
$this->deleteQueue($model, $contentKey);
$this->__isDeleted = true;
return true;
}
/**
* キュー削除
*
* @param Model $model モデル
* @param string $value 削除条件の値
* @param string $deleteColum 削除カラム
* @return void
* @throws InternalErrorException
*/
public function deleteQueue(Model $model, $value, $deleteColum = 'content_key') {
$model->loadModels([
'MailQueue' => 'Mails.MailQueue',
'MailQueueUser' => 'Mails.MailQueueUser',
]);
// キュー 削除
$conditions = array($model->MailQueue->alias . '.' . $deleteColum => $value);
if (! $model->MailQueue->deleteAll($conditions, false)) {
throw new InternalErrorException('Failed - MailQueue ' . __METHOD__);
}
// キューの配信先 削除
$conditions = array($model->MailQueueUser->alias . '.' . $deleteColum => $value);
if (! $model->MailQueueUser->deleteAll($conditions, false)) {
throw new InternalErrorException('Failed - MailQueueUser ' . __METHOD__);
}
}
}