-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from FATCHIP-GmbH/0057389_implemented_cron_for…
…_stock_update 0057389: implemented cron for stock update
- Loading branch information
Showing
7 changed files
with
341 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
/** @noinspection SpellCheckingInspection */ | ||
|
||
namespace viaebShopwareAfterbuy\Commands; | ||
|
||
use viaebShopwareAfterbuy\Services\ReadData\ReadDataInterface; | ||
use viaebShopwareAfterbuy\Services\WriteData\WriteDataInterface; | ||
use Shopware\Commands\ShopwareCommand; | ||
use Shopware\Components\Plugin\CachedConfigReader; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class UpdateStock extends ShopwareCommand | ||
{ | ||
/** | ||
* @var ReadDataInterface | ||
*/ | ||
protected $readProductsService; | ||
|
||
/** | ||
* @var WriteDataInterface | ||
*/ | ||
protected $writeProductsService; | ||
|
||
protected $config; | ||
|
||
/** | ||
* | ||
* @param CachedConfigReader $configReader | ||
* @param string $pluginName | ||
*/ | ||
public function __construct(CachedConfigReader $configReader, string $pluginName) | ||
{ | ||
parent::__construct(); | ||
|
||
$this->config = $configReader->getByPluginName($pluginName); | ||
|
||
$this->readProductsService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.read_data.external.read_stock_service'); | ||
$this->writeProductsService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.write_data.internal.write_stock_service'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('Afterbuy:Update:Stock') | ||
->setDescription('Receive stock from Afterbuy') | ||
->setHelp(<<<EOF | ||
The <info>%command.name%</info> implements a command. | ||
EOF | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
/** | ||
* Structure for receiving and writing data | ||
* Should look everywhere the same. | ||
* Dependencies are handled via services.xml | ||
*/ | ||
|
||
/** | ||
* filter array is unused yet but can be implemented | ||
*/ | ||
$filter = array( | ||
'categories' => array(), | ||
'products' => array( | ||
'submitAll' => false | ||
) | ||
); | ||
|
||
if((int)$this->config['mainSystem'] == 2) { | ||
$products = $this->readProductsService->get($filter['products']); | ||
$output->writeln('Got Products: ' . count($products)); | ||
$this->writeProductsService->put($products); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** @noinspection SpellCheckingInspection */ | ||
|
||
namespace viaebShopwareAfterbuy\Services\ReadData\External; | ||
|
||
use Fatchip\Afterbuy\ApiClient; | ||
use viaebShopwareAfterbuy\Services\Helper\AfterbuyProductsHelper; | ||
use viaebShopwareAfterbuy\Services\ReadData\AbstractReadDataService; | ||
use viaebShopwareAfterbuy\Services\ReadData\ReadDataInterface; | ||
use viaebShopwareAfterbuy\ValueObjects\Article as ValueArticle; | ||
|
||
/** | ||
* Class ReadProductsService | ||
* @package viaebShopwareAfterbuy\Services\ReadData\External | ||
* @property AfterbuyProductsHelper $helper | ||
*/ | ||
class ReadStockService extends AbstractReadDataService implements ReadDataInterface | ||
{ | ||
/** | ||
* @param array $filter | ||
* | ||
* @return ValueArticle[] | ||
*/ | ||
public function get(array $filter) | ||
{ | ||
$data = $this->read($filter); | ||
|
||
return $this->transform($data); | ||
} | ||
|
||
/** | ||
* transforms api input into ValueArticle (targetEntity) | ||
* | ||
* @param array $products | ||
* | ||
* @return ValueArticle[] | ||
*/ | ||
public function transform(array $products) | ||
{ | ||
$this->logger->debug('Receiving stock from afterbuy', $products); | ||
|
||
if ($this->targetEntity === null) { | ||
return array(); | ||
} | ||
|
||
/** @var ValueArticle[] $valueArticles */ | ||
$valueArticles = array(); | ||
|
||
foreach ($products as $product) { | ||
|
||
if (empty($product)) { | ||
continue; | ||
} | ||
|
||
$valueArticles[] = new $this->targetEntity($product["ProductID"], intval($product["Quantity"])); | ||
} | ||
|
||
return $valueArticles; | ||
} | ||
|
||
|
||
/** | ||
* provides api data. dummy data as used here can be used in tests | ||
* | ||
* @param array $filter | ||
* | ||
* @return array | ||
*/ | ||
public function read(array $filter) | ||
{ | ||
$resource = new ApiClient($this->apiConfig, $this->logger); | ||
$data = $resource->getAllShopProductsFromAfterbuy($filter); | ||
|
||
if ( ! $data || empty($data)) { | ||
return array(); | ||
} | ||
|
||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace viaebShopwareAfterbuy\Services\WriteData\Internal; | ||
|
||
use Doctrine\ORM\OptimisticLockException; | ||
|
||
use Doctrine\ORM\ORMException; | ||
use viaebShopwareAfterbuy\Services\Helper\ShopwareArticleHelper; | ||
use viaebShopwareAfterbuy\Services\WriteData\AbstractWriteDataService; | ||
use viaebShopwareAfterbuy\Services\WriteData\WriteDataInterface; | ||
use viaebShopwareAfterbuy\ValueObjects\Stock; | ||
|
||
/** | ||
* @property ShopwareArticleHelper $helper | ||
*/ | ||
class WriteStockService extends AbstractWriteDataService implements WriteDataInterface | ||
{ | ||
/** | ||
* @param array $data | ||
* | ||
* @return array | ||
*/ | ||
public function put(array $data) | ||
{ | ||
$this->transform($data); | ||
|
||
return $this->send($data); | ||
} | ||
|
||
/** | ||
* transforms valueObject into final structure for storage | ||
* could may be moved into separate helper | ||
* | ||
* @param Stock[] $valueArticles | ||
* @return array|Stock[] | ||
*/ | ||
public function transform(array $valueArticles) | ||
{ | ||
$this->logger->debug('Importing stock', $valueArticles); | ||
|
||
foreach ($valueArticles as $article) { | ||
|
||
$detail = $this->helper->getArticleByExternalIdentifier($article->getIdentifyer()); | ||
|
||
if($detail === null || !is_int($article->getStock())) { | ||
continue; | ||
} | ||
|
||
$detail->setInStock($article->getStock()); | ||
|
||
try { | ||
$this->entityManager->persist($detail); | ||
} catch (ORMException $e) { | ||
$this->logger->error('Error storing stock', $article); | ||
} | ||
|
||
} | ||
|
||
return $valueArticles; | ||
} | ||
|
||
|
||
/** | ||
* @param array $targetData | ||
* | ||
* @return array | ||
*/ | ||
public function send($targetData) | ||
{ | ||
try { | ||
$this->entityManager->flush(); | ||
} catch (OptimisticLockException | ORMException $e) { | ||
$this->logger->error('Error storing stock', $targetData); | ||
exit('Error storing stock'); | ||
} | ||
|
||
return $targetData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
/** @noinspection SpellCheckingInspection */ | ||
|
||
namespace viaebShopwareAfterbuy\ValueObjects; | ||
|
||
class Stock extends AbstractValueObject | ||
{ | ||
/** | ||
* @var string $externalIdentifier | ||
*/ | ||
public $identifyer; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
public $stock; | ||
|
||
public function __construct(string $identifyer, int $stock) | ||
{ | ||
$this->stock = $stock; | ||
$this->identifyer = $identifyer; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getIdentifyer(): string | ||
{ | ||
return $this->identifyer; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getStock() | ||
{ | ||
return $this->stock; | ||
} | ||
} |