UpgradeData::upgrade()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 2
dl 0
loc 27
ccs 0
cts 12
cp 0
crap 12
rs 9.488
c 0
b 0
f 0
1
<?php
2
3
4
namespace Stockbase\Integration\Setup;
5
6
use Magento\Catalog\Model\Product;
7
use Magento\Catalog\Setup\CategorySetupFactory;
8
use Magento\Framework\App\Config\ScopeConfigInterface;
9
use Magento\Framework\Setup\UpgradeDataInterface;
10
use Magento\Framework\Setup\ModuleContextInterface;
11
use Magento\Framework\Setup\ModuleDataSetupInterface;
12
use Magento\Config\Model\ResourceModel\Config as ConfigResourceModel;
13
use Stockbase\Integration\Model\Config\StockbaseConfiguration;
14
15
/**
16
 * Class UpgradeData
17
 */
18
class UpgradeData implements UpgradeDataInterface
19
{
20
    /**
21
     * @var CategorySetupFactory
22
     */
23
    private $categorySetupFactory;
24
    
25
    /**
26
     * @var ConfigResourceModel
27
     */
28
    private $configResource;
29
    
30
    /**
31
     * @var ScopeConfigInterface
32
     */
33
    private $config;
34
35
    /**
36
     * UpgradeData constructor.
37
     * @param CategorySetupFactory $categorySetupFactory
38
     * @param ScopeConfigInterface $config
39
     * @param ConfigResourceModel  $configResource
40
     */
41
    public function __construct(
42
        CategorySetupFactory $categorySetupFactory,
43
        ScopeConfigInterface $config,
44
        ConfigResourceModel $configResource
45
    ) {
46
        $this->categorySetupFactory = $categorySetupFactory;
47
        $this->configResource = $configResource;
48
        $this->config = $config;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
55
    {
56
        if (!$this->config->getValue(StockbaseConfiguration::CONFIG_ORDER_PREFIX)) {
57
            $this->configResource->saveConfig(
58
                StockbaseConfiguration::CONFIG_ORDER_PREFIX,
59
                uniqid('MAGE-'),
60
                ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
61
                0
62
            );
63
        }
64
        
65
        if (version_compare($context->getVersion(), '1.0.0') < 0) {
66
            $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);
67
            $entityTypeId = $categorySetup->getEntityTypeId(Product::ENTITY);
68
            
69
            $categorySetup->addAttribute($entityTypeId, 'stockbase_product', [
70
                'type' => 'int',
71
                'label' => 'Stockbase product',
72
                'input' => 'boolean',
73
                'required' => false,
74
                'visible_on_front' => false,
75
                'apply_to' => 'simple',
76
                'unique' => false,
77
                'group' => 'Stockbase',
78
            ]);
79
        }
80
    }
81
}
82