1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stockbase\Integration\Cron; |
4
|
|
|
|
5
|
|
|
use Magento\Framework\ObjectManagerInterface; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Stockbase\Integration\StockbaseApi\Client\StockbaseClientFactory; |
8
|
|
|
use Stockbase\Integration\Model\Config\StockbaseConfiguration; |
9
|
|
|
use Stockbase\Integration\Model\ResourceModel\StockItem as StockItemResource; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Stockbase stock synchronization cron job. |
13
|
|
|
*/ |
14
|
|
|
class Sync |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var LoggerInterface |
18
|
|
|
*/ |
19
|
|
|
private $logger; |
20
|
|
|
/** |
21
|
|
|
* @var StockbaseClientFactory |
22
|
|
|
*/ |
23
|
|
|
private $stockbaseClientFactory; |
24
|
|
|
/** |
25
|
|
|
* @var ObjectManagerInterface |
26
|
|
|
*/ |
27
|
|
|
private $objectManager; |
28
|
|
|
/** |
29
|
|
|
* @var StockbaseConfiguration |
30
|
|
|
*/ |
31
|
|
|
private $config; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Sync constructor. |
35
|
|
|
* @param LoggerInterface $logger |
36
|
|
|
* @param ObjectManagerInterface $objectManager |
37
|
|
|
* @param StockbaseClientFactory $stockbaseClientFactory |
38
|
|
|
* @param StockbaseConfiguration $config |
39
|
|
|
*/ |
40
|
4 |
|
public function __construct( |
41
|
|
|
LoggerInterface $logger, |
42
|
|
|
ObjectManagerInterface $objectManager, |
43
|
|
|
StockbaseClientFactory $stockbaseClientFactory, |
44
|
|
|
StockbaseConfiguration $config |
45
|
|
|
) { |
46
|
4 |
|
$this->logger = $logger; |
47
|
4 |
|
$this->stockbaseClientFactory = $stockbaseClientFactory; |
48
|
4 |
|
$this->objectManager = $objectManager; |
49
|
4 |
|
$this->config = $config; |
50
|
4 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Executes the job. |
54
|
|
|
*/ |
55
|
4 |
|
public function execute() |
56
|
|
|
{ |
57
|
4 |
|
if (!$this->config->isModuleEnabled()) { |
58
|
1 |
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
$this->logger->info("Synchronizing Stockbase stock index..."); |
62
|
|
|
|
63
|
3 |
|
$client = $this->stockbaseClientFactory->create(); |
64
|
|
|
|
65
|
|
|
/** @var StockItemResource $stockItemResource */ |
66
|
3 |
|
$stockItemResource = $this->objectManager->create(StockItemResource::class); |
67
|
|
|
|
68
|
|
|
//$lastModifiedDate = $stockItemResource->getLastModifiedItemDate(); |
69
|
3 |
|
$lastModifiedDate = null; // Disabled due to unstable API |
70
|
|
|
|
71
|
3 |
|
$this->logger->info(sprintf( |
72
|
3 |
|
"Downloading Stockbase stock data since %s...", |
73
|
3 |
|
$lastModifiedDate !== null ? $lastModifiedDate->format('Y-m-d H:i:s') : 'the beginning' |
|
|
|
|
74
|
|
|
)); |
75
|
3 |
|
$stock = $client->getStock($lastModifiedDate); |
76
|
|
|
|
77
|
3 |
|
$this->logger->info("Updating local index..."); |
78
|
3 |
|
$total = $stockItemResource->updateFromStockObject($stock); |
79
|
|
|
|
80
|
3 |
|
if ($total > 0) { |
81
|
2 |
|
$this->logger->info("{$total} Stockbase items updated."); |
82
|
|
|
} else { |
83
|
1 |
|
$this->logger->info("No new updates were found."); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
$this->logger->info("Stockbase stock index synchronization complete."); |
87
|
3 |
|
} |
88
|
|
|
} |
89
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.