命令中的Symfony2 Access Booking捆绑帮助程序

时间:2016-11-23 13:16:12

标签: php symfony console-application

我正在使用与MelifaroBookingBundle捆绑的Symfony 2.8,我需要通过命令从csv导入一些数据。我需要验证某个项目是否在特定日期预订,并在必要时进行预订。

根据MelifaroBookingBundle文档,可通过以下方式访问预订助手:

  

$这 - >获得('布克&#39)

但是,我无法在我的命令中找到如何从我的预订实体访问预订助手。请问有人帮帮我吗?

我的命令代码可以在下面找到:

    <?php

// src/AppBundle/Command/ImportCsvCommand.php
namespace AppBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Barzo\Password\Generator;
use Symfony\Component\Finder\Finder;

use AppBundle\Entity\User;
use AppBundle\Entity\Property;
use AppBundle\Entity\RoomType;
use AppBundle\Entity\Room;
use AppBundle\Entity\Booking;

use AppBundle\Repository\PropertyRepository;
use AppBundle\Repository\RoomTypeRepository;
use AppBundle\Repository\RoomRepository;
use AppBundle\Repository\BookingRepository;

class ImportCsvCommand extends ContainerAwareCommand 
{
    protected function configure()
    {
        $this
            // the name of the command (the part after "app/console")
            ->setName('app:import-csv')

            // the short description shown while running "php app/console list"
            ->setDescription('Imports a csv and generates necessary entities including: Owner, Property, RoomType, Room.')

            // the full command description shown when running the command with
            // the "--help" option
            ->setHelp("This command allows you to import a csv for database population...")
        ;
    }

    private $cvsParsingOptions = array(
        'finder_in' => 'app/Resources/csv_transmitted/',
        'finder_name' => 'Export.csv',
        'ignoreFirstLine' => false
    );

    /**
     * executes the csv 
     *
     * @throws \Exception
     *
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $folder = 'app/Resources/csv_transmitted/';

        $rooms = array();

        if ($handle = opendir($folder)) {
            while (false !== ($entry = readdir($handle))) {
                if ($entry != "." && $entry != ".." ) {
                    $path_parts = pathinfo($entry);
                    if ($path_parts['filename'] == '$$$') {
                        continue;
                    } else {
                        if ($path_parts['extension'] != '' || $path_parts['extension'] != null ) {
                            $this->cvsParsingOptions['finder_name'] = $path_parts['filename'] . '.' . $path_parts['extension'];
                        } else {
                            $this->cvsParsingOptions['finder_name'] = $path_parts['filename'];
                        }
                        // use the parseCSV() function
                        $csv = $this->parseCSV();

                        $em = $this->getContainer()->get('doctrine')->getManager();

                        foreach ($csv as $line) {
                            //assign column values to corresponding variables
                            $owner_id = $line[0];
                            $owner_name = $line[1];
                            $owner_email = $line[2];
                            $property_id = $line[3];
                            $property_name = $line[4];
                            $roomType_id = $line[5];
                            $roomType = $line[6];
                            $room_id = $line[7];
                            $room_num = $line[8];
                            $room_name = $line[9];
                            $date = date('Ymd', strtotime($line[10]));
                            $room_status = $line[11];

                            $owner_repository = $em->getRepository('AppBundle:User');

                            // query for a single owner by its primary key (usually "id")
                            $owner = $owner_repository->findByEmail($owner_email);
                            if (!$owner) {
                                $tokenGenerator = $this->getcontainer()->get('fos_user.util.token_generator');
                                $password = substr($tokenGenerator->generateToken(), 0, 12);

                                $email_parts = split('@', $owner_email);

                                $username = $email_parts[0];
                                if ($owner_repository->findByUsername($username)) {
                                    $username .= count($owner_repository->findByUsername($username) + 1);
                                }
                                exec('php C:\xampp\htdocs\pms\app\console fos:user:create '.$username.' '.$owner_email.' '.$password);

                                $emailfrom = 'no-reply@proximaweb.mu';
                                $emailto = $owner_email;
                                $subject = 'Your credentials to our PMS portal has been created';
                                $body = "Your credentials are as follows:\nusername: ".$username."\n Password: ".$password;

                                 $message = \Swift_Message::newInstance()
                                    ->setSubject($subject)
                                    ->setFrom($emailfrom)
                                    ->setTo($emailto)
                                    ->setBody('test')
                                ;
                                $this->getcontainer()->get('mailer')->send($message);

                            }

                            $property_repository = $em->getRepository('AppBundle:Property');
                            $property = $property_repository->find($property_id);

                            if (!$property) {
                                $property = new Property();
                                $property->setName($property_name);
                            } else {
                                $current_name = $property->getName();
                                if ($current_name !== $roomType) {
                                    $property->setName($property_name);
                                }
                            }

                            $roomType_repository = $em->getRepository('AppBundle:RoomType');
                            $roomType = $roomType_repository->find($roomType_id);

                            if (!$roomType) {
                                $roomType = new RoomType();
                                $roomType->setRoomType($roomType);
                            } else {
                                $current_name = $roomType->getRoomType();
                                if ($current_name !== $roomType) {
                                    $roomType->setRoomType($roomType);
                                }
                            }

                            $room_repository = $em->getRepository('AppBundle:Room');
                            $room = $room_repository->find($room_id);

                            if (!$room) {
                                $room = new Room();
                                $room->setRoomNum($room_num);
                                $room->setRoomName($owner_name);
                            } else {
                                $current_room_num = $room->getRoomNum();
                                if ($current_room_num !== $room_num) {
                                    $room->setRoomNum($room_num);
                                }

                                $current_room_name = $room->getRoomName();
                                if ($current_room_name !== $room_name) {
                                    $room->setRoomName($room_name);
                                }
                            }

                            $state = Booking()->get('booker')->isAvailableForDate($room, $date);
                            $booking_repository = $em->getRepository('AppBundle:Booking');

                            if ($room_status == 0) {
                                if (!$state) {
                                    $booking = $booking_repository->findOneBy(
                                        array('room_id' => $room->id, 'start' => $date->format('Y-m-d'), 'end' => $date->format('Y-m-d'))
                                    );
                                    $em->remove($booking);
                                    $em->flush();
                                }
                            }
                            else {
                                if ($state) {
                                    Booking()->get('booker')->book($room, $date->format('Y-m-d'), $date->format('Y-m-d'));
                                }
                            }

                            $em->persist($owner);
                            $em->persist($property);
                            $em->persist($roomType);
                            $em->persist($room);

                        }
                    }
                }
            }
            closedir($handle);
        }                                                                                                                                                                              
    }

    /**
     * Parse a csv file
     *
     * @return array
     * @throws \Exception
     *
     */
    private function parseCSV()
    {
        $ignoreFirstLine = $this->cvsParsingOptions['ignoreFirstLine'];
        $finder = new Finder();
        $finder->files()
            ->in($this->cvsParsingOptions['finder_in'])
            ->name($this->cvsParsingOptions['finder_name'])
            ->files();
        foreach ($finder as $file) {
            $csv = $file;
        }

        if(empty($csv)){
            throw new \Exception("NO CSV FILE");
        }

        $rows = array();
        if (($handle = fopen($csv->getRealPath(), "r")) !== FALSE) {
            $i = 0;
            while (($data = fgetcsv($handle, null, ",")) !== FALSE) {
                $i++;
                if ($ignoreFirstLine && $i == 1) {
                    continue;
                }
                $rows[] = $data;
            }
            fclose($handle);
        }
        return $rows;
    }
}

1 个答案:

答案 0 :(得分:1)

预订者是一项服务......因此您可以将其作为

进行访问
$this->getContainer()->get('booker')