Skip to content

Commit

Permalink
[7-12] 未処理お問い合わせ通知コマンドのインタラクティブ化
Browse files Browse the repository at this point in the history
  • Loading branch information
hidenorigoto committed Dec 28, 2015
1 parent f275685 commit 296955b
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 14 deletions.
49 changes: 49 additions & 0 deletions docs/lists/ch07/07-12.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
...
use Symfony\Component\Console\Question\Question;

class NotifyUnprocessedInquiryCommand extends ContainerAwareCommand
{
...

protected function execute(InputInterface $input, OutputInterface $output)
{
...

if (count($inquiryList) > 0) {

$output->writeln(count($inquiryList) . '件の未処理お問い合わせがあります');

if ($this->confirmSend($input, $output)) {
$this->sendMail($inquiryList, $output);
}
} else {
$output->writeln("未処理なし");
}
}

private function confirmSend($input, $output)
{
$qHelper = $this->getHelper('question');

$question = new Question('通知メールを送信しますか? [y/n]: ', null);
$question->setValidator(function ($answer) {
$answer = strtolower(substr($answer, 0, 1));
if (!in_array($answer, ['y', 'n'])) {
throw new \RuntimeException(
'yまたはnを入力してください'
);
}
return $answer;
});
$question->setMaxAttempts(3);

return $qHelper->ask($input, $output, $question) == 'y';
}

private function sendMail($inquiryList, $output)
{
$container = $this->getContainer();
$templating = $container->get('templating');
...
}
}
57 changes: 43 additions & 14 deletions src/AppBundle/Command/NotifyUnprocessedInquiryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class NotifyUnprocessedInquiryCommand extends ContainerAwareCommand
{
Expand All @@ -27,24 +28,52 @@ protected function execute(InputInterface $input, OutputInterface $output)
$inquiryList = $inquiryRepository->findUnprocessed();

if (count($inquiryList) > 0) {
$templating = $container->get('templating');

$message = \Swift_Message::newInstance()
->setSubject('[CS] 未処理お問い合わせ通知')
->setFrom('webmaster@example.com')
->setTo('admin@example.com')
->setBody(
$templating->render(
'mail/admin_unprocessedInquiryList.txt.twig',
['inquiryList' => $inquiryList]
)
);

$container->get('mailer')->send($message);
$output->writeln(count($inquiryList) . '件の未処理お問い合わせがあります');

$output->writeln(count($inquiryList) . "件の未処理を通知");
if ($this->confirmSend($input, $output)) {
$this->sendMail($inquiryList, $output);
}
} else {
$output->writeln("未処理なし");
}
}

private function confirmSend($input, $output)
{
$qHelper = $this->getHelper('question');

$question = new Question('通知メールを送信しますか? [y/n]: ', null);
$question->setValidator(function ($answer) {
$answer = strtolower(substr($answer, 0, 1));
if (!in_array($answer, ['y', 'n'])) {
throw new \RuntimeException(
'yまたはnを入力してください'
);
}
return $answer;
});
$question->setMaxAttempts(3);

return $qHelper->ask($input, $output, $question) == 'y';
}

private function sendMail($inquiryList, $output)
{
$container = $this->getContainer();
$templating = $container->get('templating');

$message = \Swift_Message::newInstance()
->setSubject('[CS] 未処理お問い合わせ通知')
->setFrom('webmaster@example.com')
->setTo('admin@example.com')
->setBody(
$templating->render(
'mail/admin_unprocessedInquiryList.txt.twig',
['inquiryList' => $inquiryList]
)
);

$container->get('mailer')->send($message);
}
}

0 comments on commit 296955b

Please sign in to comment.