Completed
Pull Request — master (#20)
by
unknown
02:56
created

Import   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 144
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A setType() 0 5 1
A setPath() 0 5 1
A setSites() 0 5 1
A setActionSection() 0 5 1
A setActionElement() 0 5 1
A execute() 0 12 1
A read() 0 16 3
A import() 0 14 1
1
<?php
2
3
namespace Notamedia\ConsoleJedi\Schema;
4
5
use Bitrix\Main\Loader;
6
use Notamedia\ConsoleJedi\Schema\Exception\ImportException;
7
8
/**
9
 * Class Import
10
 * @package Notamedia\ConsoleJedi\Schema
11
 */
12
class Import implements ActionInterface
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $config = [];
18
19
    /**
20
     * @var array
21
     */
22
    private $session = [];
23
24
    /**
25
     * @var \CIBlockXMLFile
26
     */
27
    protected $xml;
28
29
    /**
30
     * @var \CIBlockCMLImport
31
     */
32
    protected $import;
33
34
    public function __construct()
35
    {
36
        $this->config = [
37
            'type' => '',
38
            'lids' => [],
39
            'path' => '',
40
            'action_section' => 'A',
41
            'action_element' => 'A',
42
            'preview' => '',
43
            'interval' => 30
44
        ];
45
46
        Loader::includeModule('iblock');
47
        $this->xml = new \CIBlockXMLFile();
48
        $this->import = new \CIBlockCMLImport();
49
50
    }
51
52
    /**
53
     * @param string $type
54
     * @return $this
55
     */
56
    public function setType($type)
57
    {
58
        $this->config['type'] = $type;
59
        return $this;
60
    }
61
62
    /**
63
     * @param string $path
64
     * @return $this
65
     */
66
    public function setPath($path)
67
    {
68
        $this->config['path'] = $path;
69
        return $this;
70
    }
71
72
    /**
73
     * @param array $lids
74
     * @return $this
75
     */
76
    public function setSites($lids)
77
    {
78
        $this->config['lids'] = $lids;
79
        return $this;
80
    }
81
82
    /**
83
     * @param string $action
84
     * @return $this
85
     */
86
    public function setActionSection($action)
87
    {
88
        $this->config['action_section'] = $action;
89
        return $this;
90
    }
91
92
    /**
93
     * @param string $action
94
     * @return $this
95
     */
96
    public function setActionElement($action)
97
    {
98
        $this->config['action_element'] = $action;
99
        return $this;
100
    }
101
102
    /**
103
     * @return $this
104
     */
105
    public function execute()
106
    {
107
        $this->session = [
108
            "section_map" => false,
109
            "prices_map" => false,
110
        ];
111
112
        $this->read();
113
        $this->import();
114
115
        return $this;
116
    }
117
118
    /**
119
     * @throws ImportException
120
     */
121
    protected function read()
122
    {
123
        $handle = fopen($this->config['path'], "r");
124
125
        if (!$handle)
126
            throw new ImportException('Unable to open file, or file not exist');
127
128
        if (!$this->import->CheckIfFileIsCML($this->config['path'])) {
129
            throw new ImportException('File is not valid');
130
        }
131
132
        $this->xml->DropTemporaryTables();
133
        $this->xml->CreateTemporaryTables();
134
        $this->xml->ReadXMLToDatabase($handle, $this->session, $this->config['interval']);
135
        $this->xml->IndexTemporaryTables();
136
    }
137
138
    /**
139
     *
140
     */
141
    protected function import()
142
    {
143
        $this->import->Init($this->config, false, true, $this->config["preview"], false, true);
144
        $this->import->ImportMetaData([1, 2], $this->config["type"], $this->config["lids"]);
145
146
        $this->import->ImportSections();
147
        $this->import->DeactivateSections($this->config["action_section"]);
148
149
        $this->import->ReadCatalogData($this->session["section_map"], $this->session["prices_map"]);
150
        $this->import->ImportElements(time(), $this->config["interval"]);
151
        $this->import->DeactivateElement($this->config["action_element"], time(), $this->config["interval"]);
152
153
        $this->import->ImportProductSets();
154
    }
155
}