Completed
Push — master ( ec1dbf...510cdb )
by Gabriel
04:59 queued 51s
created

Sync::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 4
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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'
0 ignored issues
show
Bug introduced by
The method format cannot be called on $lastModifiedDate (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
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