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