Skip to content

Commit 352c083

Browse files
committed
minor #133 Use Command as base for console command classes (phansys)
This PR was merged into the master branch. Discussion ---------- |Q |A | |--- |--- | |Branch |master| |Bug fix? |no |á |New feature? |no | |BC breaks? |no | |Deprecations?|no | |Tests pass? |yes | |Fixed tickets|n/a | |License |MIT | |Doc PR |n/a | Commits ------- 829a684 Use `Command` as base for console command classes
2 parents 3cd0fbd + 829a684 commit 352c083

3 files changed

Lines changed: 100 additions & 22 deletions

File tree

Command/AutoClosingCommand.php

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,64 @@
22

33
namespace Hackzilla\Bundle\TicketBundle\Command;
44

5+
use Doctrine\ORM\EntityManagerInterface;
6+
use Hackzilla\Bundle\TicketBundle\Entity\Ticket;
57
use Hackzilla\Bundle\TicketBundle\Entity\TicketMessage;
6-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8+
use Hackzilla\Bundle\TicketBundle\Manager\TicketManagerInterface;
9+
use Hackzilla\Bundle\TicketBundle\Manager\UserManagerInterface;
10+
use Symfony\Component\Console\Command\Command;
711
use Symfony\Component\Console\Input\InputArgument;
812
use Symfony\Component\Console\Input\InputInterface;
913
use Symfony\Component\Console\Input\InputOption;
1014
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\DependencyInjection\ContainerInterface;
16+
use Symfony\Component\Translation\TranslatorInterface;
1117

12-
class AutoClosingCommand extends ContainerAwareCommand
18+
class AutoClosingCommand extends Command
1319
{
1420
protected static $defaultName = 'ticket:autoclosing';
1521

22+
/**
23+
* @var TicketManagerInterface
24+
*/
25+
private $ticketManager;
26+
27+
/**
28+
* @var UserManagerInterface
29+
*/
30+
private $userManager;
31+
32+
/**
33+
* @var EntityManagerInterface
34+
*/
35+
private $entityManager;
36+
37+
/**
38+
* @var string
39+
*/
40+
private $locale = 'en';
41+
42+
/**
43+
* @var TranslatorInterface
44+
*/
45+
private $translator;
46+
47+
/**
48+
* BC: Replace 5th argument with "Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface" after bumping to "symfony/dependency-injection:^4.1".
49+
*/
50+
public function __construct(TicketManagerInterface $ticketManager, UserManagerInterface $userManager, EntityManagerInterface $entityManager, TranslatorInterface $translator, ContainerInterface $container)
51+
{
52+
parent::__construct();
53+
54+
$this->ticketManager = $ticketManager;
55+
$this->userManager = $userManager;
56+
$this->entityManager = $entityManager;
57+
$this->translator = $translator;
58+
if ($container->hasParameter('locale')) {
59+
$this->locale = $container->getParameter('locale');
60+
}
61+
}
62+
1663
/**
1764
* {@inheritdoc}
1865
*/
@@ -41,30 +88,26 @@ protected function configure()
4188
*/
4289
protected function execute(InputInterface $input, OutputInterface $output)
4390
{
44-
$ticket_manager = $this->getContainer()->get('hackzilla_ticket.ticket_manager');
45-
$userManager = $this->getContainer()->get('fos_user.user_manager');
46-
$ticketRepository = $this->getContainer()->get('doctrine')->getRepository('HackzillaTicketBundle:Ticket');
91+
$ticketRepository = $this->entityManager->getRepository(Ticket::class);
4792

48-
$locale = $this->getContainer()->getParameter('locale') ? $this->getContainer()->getParameter('locale') : 'en';
49-
$translator = $this->getContainer()->get('translator');
50-
$translator->setLocale($locale);
93+
$this->translator->setLocale($this->locale);
5194

5295
$username = $input->getArgument('username');
5396

5497
$resolved_tickets = $ticketRepository->getResolvedTicketOlderThan($input->getOption('age'));
5598

5699
foreach ($resolved_tickets as $ticket) {
57-
$message = $ticket_manager->createMessage()
100+
$message = $this->ticketManager->createMessage()
58101
->setMessage(
59-
$translator->trans('MESSAGE_STATUS_CHANGED', ['%status%' => $translator->trans('STATUS_CLOSED', [], 'HackzillaTicketBundle')], 'HackzillaTicketBundle')
102+
$this->translator->trans('MESSAGE_STATUS_CHANGED', ['%status%' => $this->translator->trans('STATUS_CLOSED', [], 'HackzillaTicketBundle')], 'HackzillaTicketBundle')
60103
)
61104
->setStatus(TicketMessage::STATUS_CLOSED)
62105
->setPriority($ticket->getPriority())
63-
->setUser($userManager->findUserByUsername($username))
106+
->setUser($this->userManager->findUserByUsername($username))
64107
->setTicket($ticket);
65108

66109
$ticket->setStatus(TicketMessage::STATUS_CLOSED);
67-
$ticket_manager->updateTicket($ticket, $message);
110+
$this->ticketManager->updateTicket($ticket, $message);
68111

69112
$output->writeln('The ticket "'.$ticket->getSubject().'" has been closed.');
70113
}

Command/TicketManagerCommand.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,40 @@
22

33
namespace Hackzilla\Bundle\TicketBundle\Command;
44

5+
use FOS\UserBundle\Model\UserManagerInterface;
56
use Hackzilla\Bundle\TicketBundle\Entity\TicketMessage;
6-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7+
use Hackzilla\Bundle\TicketBundle\Manager\TicketManagerInterface;
8+
use Symfony\Component\Console\Command\Command;
79
use Symfony\Component\Console\Input\InputArgument;
810
use Symfony\Component\Console\Input\InputInterface;
911
use Symfony\Component\Console\Input\InputOption;
1012
use Symfony\Component\Console\Output\OutputInterface;
1113

12-
class TicketManagerCommand extends ContainerAwareCommand
14+
class TicketManagerCommand extends Command
1315
{
1416
protected static $defaultName = 'ticket:create';
1517

18+
/**
19+
* @var TicketManagerInterface
20+
*/
21+
private $ticketManager;
22+
23+
/**
24+
* @var UserManagerInterface
25+
*/
26+
private $userManager;
27+
28+
public function __construct(TicketManagerInterface $ticketManager, UserManagerInterface $userManager = null)
29+
{
30+
if (!$userManager) {
31+
throw new \InvalidArgumentException(sprintf('%s requires a user manager in order to set the user for the ticket message. Is "friendsofsymfony/user-bundle" installed and enabled in your project?', self::class));
32+
}
33+
parent::__construct();
34+
35+
$this->ticketManager = $ticketManager;
36+
$this->userManager = $userManager;
37+
}
38+
1639
/**
1740
* {@inheritdoc}
1841
*/
@@ -46,20 +69,16 @@ protected function configure()
4669
*/
4770
protected function execute(InputInterface $input, OutputInterface $output)
4871
{
49-
$userManager = $this->getContainer()->get('fos_user.user_manager');
50-
51-
$ticketManager = $this->getContainer()->get('hackzilla_ticket.ticket_manager');
52-
53-
$ticket = $ticketManager->createTicket()
72+
$ticket = $this->ticketManager->createTicket()
5473
->setSubject($input->getArgument('subject'));
5574

56-
$message = $ticketManager->createMessage()
75+
$message = $this->ticketManager->createMessage()
5776
->setMessage($input->getArgument('message'))
5877
->setStatus(TicketMessage::STATUS_OPEN)
5978
->setPriority($input->getOption('priority'))
60-
->setUser($userManager->findUserByUsername('system'));
79+
->setUser($this->userManager->findUserByUsername('system'));
6180

62-
$ticketManager->updateTicket($ticket, $message);
81+
$this->ticketManager->updateTicket($ticket, $message);
6382

6483
$output->writeln(
6584
"Ticket with subject '".$ticket->getSubject()."' has been created with ticketnumber #".$ticket->getId().''

Resources/config/services.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ services:
1414
- '@security.authorization_checker'
1515
public: true
1616

17+
Hackzilla\Bundle\TicketBundle\Manager\UserManagerInterface:
18+
alias: hackzilla_ticket.user_manager
19+
1720
hackzilla_ticket.user_repository:
1821
class: Doctrine\ORM\EntityRepository
1922
factory: ['@doctrine.orm.default_entity_manager', getRepository]
@@ -30,6 +33,9 @@ services:
3033
- [ setTranslator, ['@translator'] ]
3134
public: true
3235

36+
Hackzilla\Bundle\TicketBundle\Manager\TicketManagerInterface:
37+
alias: hackzilla_ticket.ticket_manager
38+
3339
hackzilla_ticket.form.type.ticket:
3440
class: Hackzilla\Bundle\TicketBundle\Form\Type\TicketType
3541
arguments:
@@ -73,10 +79,20 @@ services:
7379

7480
hackzilla_ticket.command.autoclosing:
7581
class: Hackzilla\Bundle\TicketBundle\Command\AutoClosingCommand
82+
arguments:
83+
- '@hackzilla_ticket.ticket_manager'
84+
- '@hackzilla_ticket.user_manager'
85+
- '@doctrine'
86+
- '@translator'
87+
# BC: Replace 5th argument with "@parameter_bag" after bumping "symfony/dependency-injection" to "^4.1"
88+
- '@service_container'
7689
tags:
7790
- { name: console.command, command: ticket:autoclosing }
7891

7992
hackzilla_ticket.command.create:
8093
class: Hackzilla\Bundle\TicketBundle\Command\TicketManagerCommand
94+
arguments:
95+
- '@hackzilla_ticket.ticket_manager'
96+
- '@?fos_user.user_manager'
8197
tags:
8298
- { name: console.command, command: ticket:create }

0 commit comments

Comments
 (0)