110 lines
3.8 KiB
PHP
110 lines
3.8 KiB
PHP
<?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\ORM\EntityManagerInterface;
|
|
|
|
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(EntityManagerInterface $em)
|
|
{
|
|
$this->em = $em;
|
|
|
|
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] = [];
|
|
|
|
// get vehicles from manufacturer
|
|
$make_array = [];
|
|
$vehicles = $vm->getVehicles();
|
|
foreach ($vehicles as $vehicle)
|
|
{
|
|
$batteries = $vehicle->getBatteries();
|
|
$comp_batt = [];
|
|
foreach ($batteries as $battery)
|
|
{
|
|
// check if battery is active
|
|
if (!($battery->isActive()))
|
|
continue;
|
|
|
|
// 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'] = '/assets/img/products/' . strtolower($battery->getModel()->getName()) . '.png';
|
|
$comp_batt['description'] = '';
|
|
//$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();
|
|
}
|
|
}
|
|
|
|
// check if no compatible batteries
|
|
if (!empty($comp_batt))
|
|
$make_array[$vehicle->getMake()][$vehicle->getModelYearFormatted()] = $comp_batt;
|
|
|
|
}
|
|
|
|
// check if empty
|
|
if (!empty($make_array))
|
|
$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);
|
|
|
|
return 0;
|
|
|
|
}
|
|
}
|