<?php declare(strict_types=1); /* * This file is part of vonRotenberg WMFGO Cevisio Bundle. * * (c) vonRotenberg * * @license proprietary */ namespace vonRotenberg\WmfgoCevisioBundle\Cron; use Contao\CoreBundle\Controller\AbstractController; use Contao\Date; use Contao\System; use Doctrine\DBAL\Connection; use Psr\Log\LoggerInterface; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Finder\Finder; use Symfony\Component\Serializer\Encoder\CsvEncoder; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\SerializerInterface; use vonRotenberg\ShopwareApiBundle\API\Shopware; use vonRotenberg\WmfgoCevisioBundle\Model\Import\ProductModel; class ShopwareImportProductsJob extends AbstractController { /** @var LoggerInterface */ private $logger; /** @var SerializerInterface */ private $serializer; /** @var SerializerInterface */ private $shopware; private $csvContext = []; /** @var Finder */ private $finder; /** @var Connection */ private $db; /** * Remote * @var Connection */ private $rdb; public function __construct(Finder $finder, Shopware $shopware, Connection $db, LoggerInterface $logger) { $this->finder = $finder; $this->db = $db; $this->logger = $logger; $this->shopware = $shopware; $encoder = [new CsvEncoder()]; $normalizer = [new ObjectNormalizer()]; $this->serializer = new Serializer($normalizer,$encoder); $this->csvContext = [ 'csv_delimiter' => ';', 'csv_enclosure' => '"', ]; } public function import(string $scope, ?SymfonyStyle &$io = null): void { $intCounter = 0; $isCli = false; if (strtolower($scope) == 'cli' && $io !== null) { $isCli = true; } $translator = System::getContainer()->get('translator'); $translator->setLocale('de'); $projectDir = System::getContainer()->getParameter('kernel.project_dir'); $srcDir = $projectDir.'/import/products'; if (!file_exists($srcDir)) { mkdir($srcDir, 0777,true); } $Today = new Date(); $importFiles = $this->finder->files()->in($srcDir)->name('*.csv'); if (!$importFiles->count()) { return; } foreach ($importFiles as $importFile) { $data = $this->serializer->decode($importFile->getContents(), 'csv',$this->csvContext); if ($isCli) $io->progressStart(count($data)); foreach ($data as $row) { /** @var ProductModel $Product */ $Product = $this->serializer->denormalize($row,ProductModel::class); $arrData = [ 'taxId' => '018e65c0485071508949c072f8dc18bd', 'ean' => $Product->getEan(), 'price' => [ [ 'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca', 'gross' => $Product->getPreis(), 'net' => $Product->getPreis()/119*100, 'linked' => true ] ], 'stock' => 9999999, 'name' => 'vrImport__'.$Product->getName(), 'customFields' => [ 'custom_wine_attributes_jahrgang' => $Product->getJahrgang() ] ]; if (!$this->shopware->addOrUpdateProductBySku($Product->getSku(), $arrData)) { if ($isCli) $io->error('Could not update'); } if ($isCli) { $io->progressAdvance(); } } if ($isCli) $io->progressFinish(); } } }