Skip to content

Commit 04d74e7

Browse files
Merge pull request #10 from FVSoftwareDeveloper/master
Bundle updgrade from symfony 4.4 to 5.4
2 parents c53f695 + 97c49ee commit 04d74e7

File tree

5 files changed

+94
-34
lines changed

5 files changed

+94
-34
lines changed

Command/CheckCommand.php

+19-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,24 @@
22

33
namespace NTI\EmailBundle\Command;
44

5-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
5+
use Symfony\Component\Console\Command\Command;
66
use Symfony\Component\Console\Input\InputInterface;
77
use Symfony\Component\Console\Output\OutputInterface;
88
use NTI\EmailBundle\Service\Mailer;
9+
use Symfony\Component\DependencyInjection\ContainerInterface;
910

10-
class CheckCommand extends ContainerAwareCommand
11+
class CheckCommand extends Command
1112
{
13+
private $container;
14+
15+
public function __construct(ContainerInterface $container)
16+
{
17+
$this->container = $container;
18+
19+
parent::__construct();
20+
}
21+
22+
1223
protected function configure()
1324
{
1425
$this
@@ -19,7 +30,12 @@ protected function configure()
1930

2031
protected function execute(InputInterface $input, OutputInterface $output)
2132
{
22-
$this->getContainer()->get('nti.mailer')->check($output);
33+
try{
34+
$this->container->get('nti.mailer')->check($output);
35+
return 0;
36+
} catch(\Exception $e){
37+
return 1;
38+
}
2339
}
2440

2541
}

Command/ResendCommand.php

+46-20
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,37 @@
33
namespace NTI\EmailBundle\Command;
44

55
use NTI\EmailBundle\Entity\Email;
6+
use NTI\EmailBundle\Repository\EmailRepository;
67
use NTI\EmailBundle\Entity\Smtp;
78
use Swift_Spool;
8-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9+
use Doctrine\ORM\EntityManagerInterface;
10+
use Symfony\Component\Console\Command\Command;
911
use Symfony\Component\Console\Input\InputArgument;
1012
use Symfony\Component\Console\Input\InputInterface;
1113
use Symfony\Component\Console\Input\InputOption;
1214
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
1316

14-
class ResendCommand extends ContainerAwareCommand
17+
class ResendCommand extends Command
1518
{
19+
private $container;
20+
21+
/** @var EntityManager */
22+
private $em;
23+
24+
/** @var EmailRepository */
25+
private $emailRepository;
26+
27+
public function __construct(ContainerInterface $container, EntityManagerInterface $em, EmailRepository $emailRepository)
28+
{
29+
$this->container = $container;
30+
$this->em = $em;
31+
$this->emailRepository = $emailRepository;
32+
33+
parent::__construct();
34+
}
35+
36+
1637
protected function configure()
1738
{
1839
$this
@@ -24,22 +45,27 @@ protected function configure()
2445

2546
protected function execute(InputInterface $input, OutputInterface $output)
2647
{
27-
$id = $input->getArgument("emailId");
28-
if(!$id) {
29-
$output->writeln("<error>The Email ID is required.</error>");
30-
return;
31-
}
32-
33-
$em = $this->getContainer()->get('doctrine')->getManager();
34-
$email = $em->getRepository('NTIEmailBundle:Email')->find($id);
35-
if(!$email) {
36-
$output->writeln("<error>The Email was not found.</error>");
37-
return;
38-
}
39-
40-
$smtpService = $this->getContainer()->get('nti.mailer');
41-
42-
$smtpService->resend($email);
43-
$output->writeln("<success>The Email was moved to the queue.</success>");
48+
try{
49+
50+
$id = $input->getArgument("emailId");
51+
if(!$id) {
52+
$output->writeln("<error>The Email ID is required.</error>");
53+
return;
54+
}
55+
56+
$email = $this->emailRepository->find($id);
57+
if(!$email) {
58+
$output->writeln("<error>The Email was not found.</error>");
59+
return;
60+
}
61+
62+
$smtpService = $this->container->get('nti.mailer');
63+
64+
$smtpService->resend($email);
65+
$output->writeln("<success>The Email was moved to the queue.</success>");
66+
return 0;
67+
} catch(\Exception $e){
68+
return 1;
69+
}
4470
}
45-
}
71+
}

Repository/EmailRepository.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
namespace NTI\EmailBundle\Repository;
44

55
use NTI\EmailBundle\Entity\Email;
6+
use Doctrine\Persistence\ManagerRegistry;
7+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
68

79
/**
810
* EmailRepository
911
*
1012
* This class was generated by the Doctrine ORM. Add your own custom
1113
* repository methods below.
1214
*/
13-
class EmailRepository extends \Doctrine\ORM\EntityRepository
15+
class EmailRepository extends ServiceEntityRepository
1416
{
17+
public function __construct(ManagerRegistry $registry)
18+
{
19+
parent::__construct($registry, Email::class);
20+
}
21+
1522
/**
1623
* Return the last-checked $limit emails
1724
* that are not sent to get an update.

Resources/config/services.yml

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
services:
2+
_defaults:
3+
autowire: true # Automatically injects dependencies in your services.
4+
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
25
nti.mailer:
36
class: NTI\EmailBundle\Service\Mailer
4-
arguments: ["@service_container", "@twig"]
7+
arguments: ["@service_container", "@twig", "@doctrine.orm.entity_manager"]
58
public: true
9+
autowire: true
610
nti.form.smtptype:
711
class: NTI\EmailBundle\Form\SmtpType
812
arguments: ["@doctrine.orm.entity_manager"]
913
public: true
1014
tags:
1115
- { name: form.type }
16+
autowire: true
1217
NTI\EmailBundle\Command\:
1318
resource: '../../Command/*'
14-
tags: ['console.command']
19+
tags: ['console.command']
20+
NTI\EmailBundle\Repository\:
21+
resource: '../../Repository/*'
22+
autowire: true

Service/Mailer.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,13 @@ class Mailer {
2929
/** @var string $devBcc */
3030
private $devBcc;
3131

32-
public function __construct(ContainerInterface $container, Environment $templating) {
32+
/** @var EntityManager */
33+
private $em;
34+
35+
public function __construct(ContainerInterface $container, Environment $templating, EntityManagerInterface $em) {
3336
$this->container = $container;
3437
$this->templating = $templating;
38+
$this->em = $em;
3539

3640
$devMode = $this->container->getParameter('nti_email.dev_mode');
3741
$this->devMode = is_array($devMode) && isset($devMode["enabled"]) && $devMode["enabled"] == "true";
@@ -130,9 +134,8 @@ public function testConfiguration(Smtp $smtp) {
130134
*/
131135
public function check(OutputInterface $output = null) {
132136

133-
$em = $this->container->get('doctrine')->getManager();
134137

135-
$configurations = $em->getRepository(Smtp::class)->findAll();
138+
$configurations = $this->em->getRepository(Smtp::class)->findAll();
136139

137140
/** @var Smtp $smtp */
138141
foreach ($configurations as $smtp){
@@ -174,7 +177,7 @@ public function handleSmtpSpool(Smtp $smtp, OutputInterface $output = null){
174177
$output->writeln("Sent ".$sent." emails with config: {$smtp->getUniqueId()}.");
175178

176179
// Check email statuses
177-
$emails = $em->getRepository(Email::class)->findEmailsToCheckByConfigName($smtp->getUniqueId());
180+
$emails = $this->em->getRepository(Email::class)->findEmailsToCheckByConfigName($smtp->getUniqueId());
178181
if(count($emails) <= 0) {
179182
$output->writeln("No emails to check with config: {$smtp->getUniqueId()}....");
180183
return;
@@ -230,7 +233,7 @@ public function handleSmtpSpool(Smtp $smtp, OutputInterface $output = null){
230233
}
231234
}
232235
try {
233-
$em->flush();
236+
$this->em->flush();
234237
} catch (\Exception $ex) {
235238
$this->container->get('logger')->log(\Monolog\Logger::CRITICAL, StringUtilities::BeautifyException($ex));
236239
}
@@ -368,7 +371,7 @@ private function processEmail($from, $to, $cc = array(), $bcc = array(), $subjec
368371
/** @var Smtp $smtp */
369372
$uniqueId = (is_array($from) && count($from) > 0) ? key($from) : $from;
370373
if(!$smtp) {
371-
$smtp = $em->getRepository(Smtp::class)->findOneBy(array("uniqueId" => strtolower($uniqueId)));
374+
$smtp = $this->em->getRepository(Smtp::class)->findOneBy(array("uniqueId" => strtolower($uniqueId)));
372375
}
373376

374377
if (!$smtp) {
@@ -438,8 +441,8 @@ private function processEmail($from, $to, $cc = array(), $bcc = array(), $subjec
438441
$email->setStatus(Email::STATUS_FAILURE);
439442
}
440443

441-
$em->persist($email);
442-
$em->flush();
444+
$this->em->persist($email);
445+
$this->em->flush();
443446

444447
@unlink($tempSpoolPath."/".$filename);
445448
@rmdir($tempSpoolPath);

0 commit comments

Comments
 (0)