Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,6 @@ jobs:
--health-timeout=5s
--health-retries=3

mailhog:
image: mailhog/mailhog
ports:
- 1025:1025

steps:
- name: Checkout plugin repository
uses: actions/checkout@v6
Expand Down Expand Up @@ -151,7 +146,7 @@ jobs:
DATABASE_URL: ${{ matrix.database == 'mysql' && 'mysql://root:root@127.0.0.1:3306/eccube_test' || 'postgresql://postgres:postgres@127.0.0.1:5432/eccube_test' }}
DATABASE_SERVER_VERSION: ${{ matrix.database == 'mysql' && '8.0' || '15' }}
DATABASE_CHARSET: ${{ matrix.database == 'mysql' && 'utf8mb4' || 'UTF8' }}
MAILER_DSN: 'smtp://127.0.0.1:1025'
MAILER_DSN: 'null://null'
run: |
php bin/console doctrine:database:create --if-not-exists
php bin/console doctrine:schema:create
Expand All @@ -167,7 +162,7 @@ jobs:
DATABASE_URL: ${{ matrix.database == 'mysql' && 'mysql://root:root@127.0.0.1:3306/eccube_test' || 'postgresql://postgres:postgres@127.0.0.1:5432/eccube_test' }}
DATABASE_SERVER_VERSION: ${{ matrix.database == 'mysql' && '8.0' || '15' }}
DATABASE_CHARSET: ${{ matrix.database == 'mysql' && 'utf8mb4' || 'UTF8' }}
MAILER_DSN: 'smtp://127.0.0.1:1025'
MAILER_DSN: 'null://null'
run: |
vendor/bin/phpunit --exclude-group cache-clear,cache-clear-install,update-schema-doctrine,plugin-service app/Plugin/StockAlertMail/Tests/

Expand Down
15 changes: 8 additions & 7 deletions Command/StockAlertCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$config = $this->configRepository->findOneBy([]);
$config = $this->configRepository->find(1);
if ($config === null) {
$io->error($this->translator->trans('stock_alert_mail.command.config_not_found'));

Expand All @@ -71,6 +71,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
->andWhere('pc.visible = true')
->andWhere('p.Status = :status')
->setParameter('status', ProductStatus::DISPLAY_SHOW)
->orderBy('p.id', 'ASC')
->getQuery()
->getResult();

Expand Down Expand Up @@ -108,12 +109,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$io->info($this->translator->trans('stock_alert_mail.command.alert_items_found', ['%count%' => count($newAlertItems)]));

// メール送信
$BaseInfo = $this->mailBuilder->getBaseInfo();
$toEmails = $this->mailBuilder->resolveToEmails($config);
$body = $this->mailBuilder->buildMailBody($config, $newAlertItems, $threshold);
$subject = $this->mailBuilder->buildMailSubject($config);

try {
$BaseInfo = $this->mailBuilder->getBaseInfo();
$toEmails = $this->mailBuilder->resolveToEmails($config);
$body = $this->mailBuilder->buildMailBody($newAlertItems, $threshold);
$subject = $this->mailBuilder->buildMailSubject();

$message = (new Email())
->subject($subject)
->from(new Address($BaseInfo->getEmail01(), $BaseInfo->getShopName()))
Expand All @@ -135,7 +136,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->entityManager->flush();

$io->success($this->translator->trans('stock_alert_mail.command.mail_sent', ['%emails%' => implode(', ', $toEmails)]));
} catch (\Exception $e) {
} catch (\Throwable $e) {
$io->error($this->translator->trans('stock_alert_mail.command.mail_failed', ['%message%' => $e->getMessage()]));

return Command::FAILURE;
Expand Down
52 changes: 51 additions & 1 deletion Controller/Admin/StockAlertConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,21 @@
use Plugin\StockAlertMail\Entity\StockAlertConfig;
use Plugin\StockAlertMail\Form\Type\Admin\StockAlertConfigType;
use Plugin\StockAlertMail\Repository\StockAlertConfigRepository;
use Plugin\StockAlertMail\Service\StockAlertMailBuilder;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;

class StockAlertConfigController extends AbstractController
{
public function __construct(
private readonly StockAlertConfigRepository $configRepository,
private readonly StockAlertMailBuilder $mailBuilder,
private readonly MailerInterface $mailer,
) {
}

Expand All @@ -36,7 +42,7 @@ public function __construct(
*/
public function index(Request $request): array|Response
{
$config = $this->configRepository->findOneBy([]);
$config = $this->configRepository->find(1);
if ($config === null) {
$config = new StockAlertConfig();
$config->setCreateDate(new \DateTime());
Expand All @@ -59,4 +65,48 @@ public function index(Request $request): array|Response
'form' => $form->createView(),
];
}

/**
* @Route("/%eccube_admin_route%/plugin/stock-alert/config/send-test", name="stock_alert_mail_admin_config_send_test", methods={"POST"})
*/
public function sendTest(Request $request): Response
{
if (!$this->isCsrfTokenValid('stock_alert_mail_send_test', $request->request->get('_token'))) {
$this->addError('admin.common.csrf_error', 'admin');

return $this->redirectToRoute('stock_alert_mail_admin_config');
}

$config = $this->configRepository->find(1);
if ($config === null) {
$this->addError('stock_alert_mail.admin.config.send_test.config_not_found', 'admin');

return $this->redirectToRoute('stock_alert_mail_admin_config');
}

$BaseInfo = $this->mailBuilder->getBaseInfo();
$toEmails = $this->mailBuilder->resolveToEmails($config);
$dummyItems = $this->mailBuilder->createDummyItems();
$subject = '[TEST] '.$this->mailBuilder->buildMailSubject();
$body = $this->mailBuilder->buildMailBody($dummyItems, $config->getThreshold());

try {
$message = (new Email())
->subject($subject)
->from(new Address($BaseInfo->getEmail01(), $BaseInfo->getShopName()))
->text($body);

foreach ($toEmails as $email) {
$message->addTo($email);
}

$this->mailer->send($message);

$this->addSuccess($this->translator->trans('stock_alert_mail.admin.config.send_test.success', ['%emails%' => implode(', ', $toEmails)]), 'admin');
} catch (\Exception $e) {
$this->addError($this->translator->trans('stock_alert_mail.admin.config.send_test.failed', ['%message%' => $e->getMessage()]), 'admin');
}

return $this->redirectToRoute('stock_alert_mail_admin_config');
}
}
112 changes: 0 additions & 112 deletions Controller/Admin/StockAlertMailTemplateController.php

This file was deleted.

2 changes: 0 additions & 2 deletions DoctrineMigrations/Version20260320000000.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public function up(Schema $schema): void
$table->addColumn('id', 'integer', ['unsigned' => true, 'autoincrement' => true]);
$table->addColumn('threshold', 'integer', ['default' => 5]);
$table->addColumn('alert_emails', 'string', ['length' => 1000, 'notnull' => false]);
$table->addColumn('mail_subject', 'string', ['length' => 500, 'notnull' => false]);
$table->addColumn('mail_body', 'text', ['notnull' => false]);
$table->addColumn('create_date', 'datetimetz');
$table->addColumn('update_date', 'datetimetz');
$table->setPrimaryKey(['id']);
Expand Down
62 changes: 62 additions & 0 deletions DoctrineMigrations/Version20260321000000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/*
* 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 DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* メールテンプレートを dtb_mail_template に移行するため、
* plg_stock_alert_config から mail_subject・mail_body カラムを削除する。
*/
final class Version20260321000000 extends AbstractMigration
{
public const TABLE = 'plg_stock_alert_config';

public function up(Schema $schema): void
{
if (!$schema->hasTable(self::TABLE)) {
return;
}

$table = $schema->getTable(self::TABLE);

if ($table->hasColumn('mail_subject')) {
$table->dropColumn('mail_subject');
}

if ($table->hasColumn('mail_body')) {
$table->dropColumn('mail_body');
}
}

public function down(Schema $schema): void
{
if (!$schema->hasTable(self::TABLE)) {
return;
}

$table = $schema->getTable(self::TABLE);

if (!$table->hasColumn('mail_subject')) {
$table->addColumn('mail_subject', 'string', ['length' => 500, 'notnull' => false]);
}

if (!$table->hasColumn('mail_body')) {
$table->addColumn('mail_body', 'text', ['notnull' => false]);
}
}
}
Loading