From fba5d9cb86e5df86ae79ab3636c1c55503159858 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Thu, 21 Nov 2019 09:27:35 +0800 Subject: [PATCH] Add method for getting default vehicle because of em->clear #274 --- .../CreateCustomerFromWarrantyCommand.php | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 841be142..38758990 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -27,10 +27,21 @@ class CreateCustomerFromWarrantyCommand extends Command protected $em; protected $cust_index; + protected $cvu_mfg_id; + protected $cvu_brand_id; + public function __construct(ObjectManager $em) { $this->em = $em; + // get the default ids from .env + // TODO: DO NOT USE $_ENV + $dotenv = new Dotenv(); + $dotenv->loadEnv(__DIR__.'/../../.env'); + + $this->cvu_mfg_id = $_ENV['CVU_MFG_ID']; + $this->cvu_brand_id = $_ENV['CVU_BRAND_ID']; + parent::__construct(); } @@ -41,16 +52,9 @@ class CreateCustomerFromWarrantyCommand extends Command ->setHelp('Creates customers from existing warranties.') ->addArgument('file', InputArgument::REQUIRED, 'Path to the output CSV file with the warranties.'); } + protected function execute(InputInterface $input, OutputInterface $output) { - // get the default ids from .env - // TODO: DO NOT USE $_ENV - $dotenv = new Dotenv(); - $dotenv->loadEnv(__DIR__.'/../../.env'); - - $cvu_mfg_id = $_ENV['CVU_MFG_ID']; - $cvu_brand_id = $_ENV['CVU_BRAND_ID']; - $csv_file = $input->getArgument('file'); // attempt to open file @@ -69,13 +73,6 @@ class CreateCustomerFromWarrantyCommand extends Command $total_cust_added = 0; $total_cv_added = 0; - // get default vehicle - $default_vehicle = $this->em->getRepository(Vehicle::class)->find($cvu_brand_id); - if (empty($default_vehicle)) - { - $output->writeln("Need to add vehicle with default values."); - return; - } /* // load all customers @@ -183,7 +180,7 @@ class CreateCustomerFromWarrantyCommand extends Command // customer exists but not customer vehicle // add customer vehicle to existing customer with unknown manufacturer and make error_log('new vehicle - ' . $w_plate_number); - $this->createCustomerVehicle($customer, $default_vehicle, $w_plate_number); + $this->createCustomerVehicle($customer, $this->getDefaultVehicle(), $w_plate_number); $total_cv_added++; } } @@ -208,7 +205,7 @@ class CreateCustomerFromWarrantyCommand extends Command $this->em->persist($new_cust); - $this->createCustomerVehicle($new_cust, $default_vehicle, $w_plate_number); + $this->createCustomerVehicle($new_cust, $this->getDefaultVehicle(), $w_plate_number); // add latest customer to hash $this->cust_index[$w_mobile_num][] = $new_cust; @@ -262,6 +259,19 @@ class CreateCustomerFromWarrantyCommand extends Command $output->writeln('Total customer vehicles added: ' . $total_cv_added); } + protected function getDefaultVehicle() + { + // get default vehicle + $default_vehicle = $this->em->getRepository(Vehicle::class)->find($this->cvu_brand_id); + if ($default_vehicle == null) + { + $output->writeln("Need to add vehicle with default values."); + return null; + } + + return $default_vehicle; + } + protected function findCustomerByNumber($number) { $customers = $this->em->getRepository(Customer::class)->findBy(['phone_mobile' => $number]);