Skip to content

Commit

Permalink
Merge pull request #23 from FATCHIP-GmbH/0057389_implemented_cron_for…
Browse files Browse the repository at this point in the history
…_stock_update

0057389: implemented cron for stock update
  • Loading branch information
andrefatchip authored Jan 9, 2020
2 parents dd9a3b6 + 35c87ba commit 1ba4d8d
Show file tree
Hide file tree
Showing 7 changed files with 341 additions and 1 deletion.
83 changes: 83 additions & 0 deletions Commands/UpdateStock.php
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);
}
}
}
7 changes: 7 additions & 0 deletions Resources/cronjob.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,12 @@
<interval>86400</interval>
<disableOnError>false</disableOnError>
</cronjob>
<cronjob>
<name>Sync Stock</name>
<action>Shopware_CronJob_AfterbuyUpdateStock</action>
<active>false</active>
<interval>3600</interval>
<disableOnError>false</disableOnError>
</cronjob>

</cronjobs>
24 changes: 24 additions & 0 deletions Resources/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@
<argument type="string">%viaeb_shopware_afterbuy.plugin_name%</argument>
</service>

<service id="viaeb_shopware_afterbuy.commands.update_stock" class="viaebShopwareAfterbuy\Commands\UpdateStock">
<tag name="console.command" />
<argument type="service" id="shopware.plugin.cached_config_reader" />
<argument type="string">%viaeb_shopware_afterbuy.plugin_name%</argument>
</service>

<service id="viaeb_shopware_afterbuy.commands.update_orders" class="viaebShopwareAfterbuy\Commands\UpdateOrders">
<tag name="console.command" />
<argument type="service" id="shopware.plugin.cached_config_reader" />
Expand All @@ -119,6 +125,24 @@
<service class="viaebShopwareAfterbuy\Services\ReadData\External\ConnectionTestService" id="viaeb_shopware_afterbuy.services.read_data.external.connection_test_service" parent="viaeb_shopware_afterbuy.services.abstract_data_service">
</service>

<!-- Stock -->

<service class="viaebShopwareAfterbuy\Services\ReadData\External\ReadStockService" id="viaeb_shopware_afterbuy.services.read_data.external.read_stock_service" parent="viaeb_shopware_afterbuy.services.abstract_data_service">
<argument id="models" type="service"/>
<call method="setTarget">
<argument type="string">viaebShopwareAfterbuy\ValueObjects\Stock</argument>
</call>
</service>

<service class="viaebShopwareAfterbuy\Services\WriteData\Internal\WriteStockService" id="viaeb_shopware_afterbuy.services.write_data.internal.write_stock_service" parent="viaeb_shopware_afterbuy.services.abstract_data_service">
<argument id="models" type="service"/>
<call method="initHelper">
<argument type="service" id="viaeb_shopware_afterbuy.services.helper.shopware_article_helper"/>
<argument type="string">number</argument>
<argument>false</argument>
</call>
</service>

<!-- Products -->

<service class="viaebShopwareAfterbuy\Services\ReadData\External\ReadProductsService" id="viaeb_shopware_afterbuy.services.read_data.external.read_products_service" parent="viaeb_shopware_afterbuy.services.abstract_data_service">
Expand Down
80 changes: 80 additions & 0 deletions Services/ReadData/External/ReadStockService.php
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;
}
}
79 changes: 79 additions & 0 deletions Services/WriteData/Internal/WriteStockService.php
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;
}
}
30 changes: 29 additions & 1 deletion Subscriber/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ class Cron implements SubscriberInterface
* @var WriteDataInterface
*/
protected $writeProductsService;
/**
* @var ReadDataInterface
*/
protected $readStockService;

/**
* @var WriteDataInterface
*/
protected $writeStockService;


public function __construct(CachedConfigReader $configReader, string $pluginName)
{
Expand All @@ -69,6 +79,9 @@ public function __construct(CachedConfigReader $configReader, string $pluginName

$this->readOrderStatusService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.read_data.external.read_orders_service');
$this->writeOrderStatusService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.write_data.internal.write_status_service');

$this->readStockService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.read_data.external.read_stock_service');
$this->writeStockService = Shopware()->Container()->get('viaeb_shopware_afterbuy.services.write_data.internal.write_stock_service');
}
//shopware is data carrying system otherwise
else {
Expand All @@ -93,10 +106,25 @@ public static function getSubscribedEvents()
{
return array(
'Shopware_CronJob_AfterbuyUpdateProducts' => 'updateProducts',
'Shopware_CronJob_AfterbuyUpdateOrders' => 'updateOrders'
'Shopware_CronJob_AfterbuyUpdateOrders' => 'updateOrders',
'Shopware_CronJob_AfterbuyUpdateStock' => 'updateStock'
);
}

public function updateStock() {
$filter = array(
'categories' => array(),
'products' => array(
'submitAll' => false
)
);

if(!empty($this->readStockService)) {
$products = $this->readStockService->get($filter['products']);
$this->writeStockService->put($products);
}
}

/** @noinspection PhpUnused */
public function updateProducts(
/** @noinspection PhpUnusedParameterInspection */ Shopware_Components_Cron_CronJob $job
Expand Down
39 changes: 39 additions & 0 deletions ValueObjects/Stock.php
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;
}
}

0 comments on commit 1ba4d8d

Please sign in to comment.