Create command that generates a JSON file of vehicles and their models with their compatible battery. #212
This commit is contained in:
parent
ce520ebf51
commit
144156e377
1 changed files with 97 additions and 0 deletions
97
src/Command/GenerateBatteryCompatibilityCommand.php
Normal file
97
src/Command/GenerateBatteryCompatibilityCommand.php
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
use Doctrine\Common\Persistence\ObjectManager;
|
||||
|
||||
use App\Entity\VehicleManufacturer;
|
||||
use App\Entity\Vehicle;
|
||||
use App\Entity\Battery;
|
||||
use App\Entity\BatteryManufacturer;
|
||||
use App\Entity\BatteryModel;
|
||||
use App\Entity\BatterySize;
|
||||
|
||||
class GenerateBatteryCompatibilityCommand extends Command
|
||||
{
|
||||
protected $em;
|
||||
protected $vmfg_index;
|
||||
|
||||
public function __construct(ObjectManager $om)
|
||||
{
|
||||
$this->em = $om;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function populateVehicleManufacturerIndex()
|
||||
{
|
||||
$vms = $this->em->getRepository(VehicleManufacturer::class)->findAll();
|
||||
|
||||
$this->vmfg_index = [];
|
||||
foreach ($vms as $vm)
|
||||
{
|
||||
$mfg_name = $vm->getName();
|
||||
$this->vmfg_index[$mfg_name] = $vm;
|
||||
|
||||
// get vehicles from manufacturer
|
||||
$make_array = [];
|
||||
$vehicles = $vm->getVehicles();
|
||||
foreach ($vehicles as $vehicle)
|
||||
{
|
||||
$batteries = $vehicle->getBatteries();
|
||||
$comp_batt = [];
|
||||
foreach ($batteries as $battery)
|
||||
{
|
||||
// set to the first compatible battery found until a more expensive one is found
|
||||
$comp_batt['id'] = $battery->getID();
|
||||
$comp_batt['name'] = $battery->getModel()->getName() . ' ' .
|
||||
$battery->getSize()->getName();
|
||||
$comp_batt['image_url'] = $battery->getImageFile();
|
||||
//$comp_batt['description'] = $battery->getDescription();
|
||||
|
||||
// store the selling price for comparison
|
||||
$batt_price = $battery->getSellingPrice();
|
||||
|
||||
// find the most expensive compatible battery
|
||||
if ($battery->getSellingPrice() > $batt_price)
|
||||
{
|
||||
$comp_batt['id'] = $battery->getID();
|
||||
$comp_batt['name'] = $battery->getModel()->getName() . ' ' .
|
||||
$battery->getSize()->getName();
|
||||
$comp_batt['image_url'] = $battery->getImageFile();
|
||||
//$comp_batt['description'] = $battery->getDescription();
|
||||
}
|
||||
}
|
||||
$make_array[$vehicle->getMake()][$vehicle->getModelYearFormatted()] = $comp_batt;
|
||||
|
||||
}
|
||||
$this->vmfg_index[$mfg_name] = $make_array;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('battery:generate_compatibility_json')
|
||||
->setDescription('Generate a json file containing the vehicles and their compatible batteries')
|
||||
->setHelp('Generate a json file containing the vehicles and their compatible batteries')
|
||||
->addArgument('file', InputArgument::REQUIRED, 'Path and name for JSON file.');
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$filename = $input->getArgument('file');
|
||||
$this->populateVehicleManufacturerIndex();
|
||||
|
||||
$json_file = fopen($filename, 'w');
|
||||
fwrite($json_file, json_encode($this->vmfg_index, JSON_PRETTY_PRINT));
|
||||
fclose($json_file);
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue