-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginManager.php
More file actions
148 lines (117 loc) · 5.14 KB
/
PluginManager.php
File metadata and controls
148 lines (117 loc) · 5.14 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Plugin\StockAlertMail;
use Doctrine\ORM\EntityManagerInterface;
use Eccube\Common\EccubeConfig;
use Eccube\Entity\MailTemplate;
use Eccube\Plugin\AbstractPluginManager;
use Plugin\StockAlertMail\Entity\StockAlertConfig;
use Psr\Container\ContainerInterface;
class PluginManager extends AbstractPluginManager
{
/** dtb_mail_template に登録するファイル名 */
public const MAIL_TEMPLATE_FILE_NAME = 'Mail/stock_alert.twig';
/** プラグインのデフォルトメール件名(送信時に "[ショップ名] " が先頭に付く) */
private const MAIL_SUBJECT = '在庫アラート通知';
public function enable(array $meta, ContainerInterface $container): void
{
$entityManager = $container->get('doctrine')->getManager();
$this->createInitialConfig($entityManager);
$this->createMailTemplate($entityManager, $container);
}
public function uninstall(array $meta, ContainerInterface $container): void
{
$entityManager = $container->get('doctrine')->getManager();
$this->deleteMailTemplate($entityManager, $container);
$conn = $entityManager->getConnection();
$schemaManager = $conn->createSchemaManager();
// FK参照元のlogテーブルを先に削除する
foreach (['plg_stock_alert_log', 'plg_stock_alert_config'] as $table) {
if ($schemaManager->tablesExist([$table])) {
$quotedTable = $conn->quoteIdentifier($table);
$conn->executeStatement("DROP TABLE {$quotedTable}");
}
}
}
private function createInitialConfig(EntityManagerInterface $entityManager): void
{
$repository = $entityManager->getRepository(StockAlertConfig::class);
if ($repository->find(1) !== null) {
return;
}
$config = new StockAlertConfig();
$config->setThreshold(5);
$config->setCreateDate(new \DateTime());
$config->setUpdateDate(new \DateTime());
$entityManager->persist($config);
$entityManager->flush();
}
private function createMailTemplate(EntityManagerInterface $entityManager, ContainerInterface $container): void
{
// まずテンプレート実体を保証(欠落時の自己修復)
$this->copyTwigTemplate($container);
$repository = $entityManager->getRepository(MailTemplate::class);
// 既に登録済みの場合はDB登録のみスキップ
if ($repository->findOneBy(['file_name' => self::MAIL_TEMPLATE_FILE_NAME]) !== null) {
return;
}
$mailTemplate = new MailTemplate();
$mailTemplate->setName('在庫アラートメール');
$mailTemplate->setFileName(self::MAIL_TEMPLATE_FILE_NAME);
$mailTemplate->setMailSubject(self::MAIL_SUBJECT);
$mailTemplate->setCreateDate(new \DateTime());
$mailTemplate->setUpdateDate(new \DateTime());
$entityManager->persist($mailTemplate);
$entityManager->flush();
}
private function deleteMailTemplate(EntityManagerInterface $entityManager, ContainerInterface $container): void
{
$repository = $entityManager->getRepository(MailTemplate::class);
$mailTemplate = $repository->findOneBy(['file_name' => self::MAIL_TEMPLATE_FILE_NAME]);
if ($mailTemplate !== null) {
$entityManager->remove($mailTemplate);
$entityManager->flush();
}
$this->removeTwigTemplate($container);
}
private function copyTwigTemplate(ContainerInterface $container): void
{
$targetPath = $this->getTwigTargetPath($container);
if (file_exists($targetPath)) {
return;
}
$sourcePath = __DIR__.'/Resource/template/Mail/stock_alert.twig';
if (!file_exists($sourcePath)) {
throw new \RuntimeException(sprintf('プラグインのテンプレートファイルが見つかりません: %s', $sourcePath));
}
$targetDir = dirname($targetPath);
if (!is_dir($targetDir) && !mkdir($targetDir, 0755, true)) {
throw new \RuntimeException(sprintf('テンプレートディレクトリの作成に失敗しました: %s', $targetDir));
}
if (!copy($sourcePath, $targetPath)) {
throw new \RuntimeException(sprintf('テンプレートのコピーに失敗しました: %s → %s', $sourcePath, $targetPath));
}
}
private function removeTwigTemplate(ContainerInterface $container): void
{
$targetPath = $this->getTwigTargetPath($container);
if (file_exists($targetPath)) {
unlink($targetPath);
}
}
private function getTwigTargetPath(ContainerInterface $container): string
{
/** @var EccubeConfig $eccubeConfig */
$eccubeConfig = $container->get(EccubeConfig::class);
return $eccubeConfig['eccube_theme_front_dir'].'/'.self::MAIL_TEMPLATE_FILE_NAME;
}
}