Completed
Push — master ( 59e207...c2af96 )
by Nik
03:00
created

Importer::setSites()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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