<?php
namespace App\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use App\Entity\Product;
use App\Entity\Fx;
use App\Controller\ProductController;
class UpdateCommand extends ContainerAwareCommand {
protected function configure() {
$this
->setName('run:update')
->setDescription('Update product prices')
;
}
protected function execute(InputInterface $input, OutputInterface $output) {
$updateService = $this->getContainer()->get('product.update');
$em = $this->getContainer()->get('doctrine')->getManager();
$updateService->updateFx();
$latestFx = $em->getRepository('SAPriceMonitorBundle:Fx')->FindLatestFx();
$datetime = new \Datetime();
$now = $datetime->format('Y-m-d H:i:s');
$now = strtotime($now);
$allProducts = $updateService->listProducts();
foreach ($allProducts as $product) {
$currentStatus = $product->getStatuses()->first()->getStatus();
if ($currentStatus == 1 || $currentStatus == 5 || $currentStatus == 3) {
$lastPriceUpdate = $product->getPrices()->first()->getDate()->format('Y-m-d H:i:s');
$lastPriceUpdate = strtotime($lastPriceUpdate);
$difference = $now - $lastPriceUpdate;
$updateService->updateProduct($product, $em, $latestFx);
//$output->writeln($product->getName() . ' updated');
}
}
$em->flush();
}
}