Completed
Pull Request — master (#20)
by
unknown
01:51
created

Import   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

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