]> Raphaƫl G. Git Repositories - airbundle/blob - Command/AttributeCommand.php
New page title and description schema
[airbundle] / Command / AttributeCommand.php
1 <?php
2
3 namespace Rapsys\AirBundle\Command;
4
5 use Doctrine\Bundle\DoctrineBundle\Command\DoctrineCommand;
6 use Symfony\Component\Console\Input\InputInterface;
7 use Symfony\Component\Console\Output\OutputInterface;
8 use Rapsys\AirBundle\Entity\Session;
9
10 class AttributeCommand extends DoctrineCommand {
11 //Set failure constant
12 const FAILURE = 1;
13
14 ///Set success constant
15 const SUCCESS = 0;
16
17 ///Configure attribute command
18 protected function configure() {
19 //Configure the class
20 $this
21 //Set name
22 ->setName('rapsysair:attribute')
23 //Set description shown with bin/console list
24 ->setDescription('Attribute sessions')
25 //Set description shown with bin/console --help airlibre:attribute
26 ->setHelp('This command attribute sessions without application');
27 }
28
29 ///Process the attribution
30 protected function execute(InputInterface $input, OutputInterface $output) {
31 //Fetch doctrine
32 $doctrine = $this->getDoctrine();
33
34 //Get manager
35 $manager = $doctrine->getManager();
36
37 //Fetch sessions to attribute
38 $sessions = $doctrine->getRepository(Session::class)->findAllPendingApplication();
39
40 //Iterate on each session
41 foreach($sessions as $sessionId => $session) {
42 //Extract session id
43 if (!empty($sessionId)) {
44 //Fetch application id of the best candidate
45 if (!empty($application = $doctrine->getRepository(Session::class)->findBestApplicationById($sessionId))) {
46 //Set updated
47 $session->setUpdated(new \DateTime('now'));
48
49 //Set application_id
50 $session->setApplication($application);
51
52 //Queue session save
53 $manager->persist($session);
54
55 }
56 }
57 }
58
59 //Flush to get the ids
60 $manager->flush();
61
62 //Return success
63 return self::SUCCESS;
64 }
65 }