Completed
Pull Request — master (#20)
by
unknown
03:04
created

Importer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 163
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 163
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 13 1
A read() 0 16 3
A import() 0 21 1
1
<?php
2
3
namespace Notamedia\ConsoleJedi\Iblock;
4
5
use Bitrix\Main\Loader;
6
use Bitrix\Main\IO\Path;
7
use Notamedia\ConsoleJedi\Iblock\Exception\ImportException;
8
9
/**
10
 * Import information block from xml file
11
 */
12
class Importer 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' => 'Y',
43
            'interval' => 0
44
        ];
45
46
        Loader::includeModule('iblock');
47
        $this->xml = new \CIBlockXMLFile();
48
        $this->import = new \CIBlockCMLImport();
49
50
    }
51
52
    /**
53
     * Set type information block
54
     *
55
     * @param string $type
56
     * @return $this
57
     */
58
    public function setType($type)
59
    {
60
        $this->config['type'] = $type;
61
        return $this;
62
    }
63
64
    /**
65
     * Set file path to import
66
     *
67
     * @param string $path
68
     * @return $this
69
     */
70
    public function setPath($path)
71
    {
72
        $this->config['path'] = $path;
73
        return $this;
74
    }
75
76
    /**
77
     * Set binding sites for importing information block
78
     * @param array $lids
79
     * @return $this
80
     */
81
    public function setSites($lids)
82
    {
83
        $this->config['lids'] = $lids;
84
        return $this;
85
    }
86
87
    /**
88
     * What doing with existing section
89
     *
90
     * @param string $action
91
     * @return $this
92
     */
93
    public function setActionSection($action)
94
    {
95
        $this->config['action_section'] = $action;
96
        return $this;
97
    }
98
99
    /**
100
     * What doing with existing element
101
     *
102
     * @param string $action
103
     * @return $this
104
     */
105
    public function setActionElement($action)
106
    {
107
        $this->config['action_element'] = $action;
108
        return $this;
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function execute()
115
    {
116
        $absFilename = Path::convertSiteRelativeToAbsolute($this->config['path']);
117
118
        $this->session = [
119
            "section_map" => false,
120
            "prices_map" => false,
121
            "work_dir" => pathinfo($absFilename, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR
122
        ];
123
124
        $this->read();
125
        $this->import();
126
    }
127
128
    /**
129
     * Read file
130
     *
131
     * @throws ImportException
132
     */
133
    protected function read()
134
    {
135
        $handle = fopen($this->config['path'], "r");
136
137
        if (!$handle)
138
            throw new ImportException('Unable to open file, or file not exist');
139
140
        if (!$this->import->CheckIfFileIsCML($this->config['path'])) {
141
            throw new ImportException('File is not valid');
142
        }
143
144
        $this->xml->DropTemporaryTables();
145
        $this->xml->CreateTemporaryTables();
146
        $this->xml->ReadXMLToDatabase($handle, $this->session, $this->config['interval']);
147
        $this->xml->IndexTemporaryTables();
148
    }
149
150
    /**
151
     * Direct import
152
     */
153
    protected function import()
154
    {
155
        $this->import->Init(
156
            $this->config,
157
            $this->session['work_dir'],
158
            true,
159
            $this->config["preview"],
160
            false,
161
            true
162
        );
163
        $this->import->ImportMetaData([1, 2], $this->config["type"], $this->config["lids"]);
164
165
        $this->import->ImportSections();
166
        $this->import->DeactivateSections($this->config["action_section"]);
167
168
        $this->import->ReadCatalogData($this->session["section_map"], $this->session["prices_map"]);
169
        $this->import->ImportElements(time(), $this->config["interval"]);
170
        $this->import->DeactivateElement($this->config["action_element"], time(), $this->config["interval"]);
171
172
        $this->import->ImportProductSets();
173
    }
174
}