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

Exporter   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 165
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A setId() 0 5 1
A setPath() 0 5 1
A setSections() 0 5 1
A setElements() 0 5 1
A execute() 0 13 1
A export() 0 55 3
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\ExportException;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
/**
11
 * Export information block in xml file
12
 */
13
class Exporter implements ActionInterface
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $config = [];
19
20
    /**
21
     * @var array
22
     */
23
    private $session = [];
24
25
    /**
26
     * @var \CIBlockCMLExport
27
     */
28
    protected $export;
29
30
    /**
31
     * Prefix temp file
32
     *
33
     * @var string
34
     */
35
    protected $prefix = '.tmp';
36
37
    public function __construct()
38
    {
39
        $this->config = [
40
            'id' => '',
41
            'path' => '',
42
            'sections' => 'none',
43
            'elements' => 'none',
44
            'interval' => 0
45
        ];
46
47
        Loader::includeModule('iblock');
48
        $this->export = new \CIBlockCMLExport();
49
    }
50
51
    /**
52
     * Set id information block
53
     *
54
     * @param int $id
55
     * @return $this
56
     */
57
    public function setId($id)
58
    {
59
        $this->config['id'] = intval($id);
60
        return $this;
61
    }
62
63
    /**
64
     * Set file path to export
65
     *
66
     * @param string $path
67
     * @return $this
68
     */
69
    public function setPath($path)
70
    {
71
        $this->config['path'] = $path;
72
        return $this;
73
    }
74
75
    /**
76
     * Set settings export sections
77
     *
78
     * @param string $sections
79
     * @return $this
80
     */
81
    public function setSections($sections)
82
    {
83
        $this->config['sections'] = $sections;
84
        return $this;
85
    }
86
87
    /**
88
     * Set settings export elements
89
     *
90
     * @param string $elements
91
     * @return $this
92
     */
93
    public function setElements($elements)
94
    {
95
        $this->config['elements'] = $elements;
96
        return $this;
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function execute()
103
    {
104
        $absFilename = Path::convertSiteRelativeToAbsolute($this->config['path']);
105
106
        $this->session = [
107
            "property_map" => false,
108
            "section_map" => false,
109
            "work_dir" => pathinfo($absFilename, PATHINFO_DIRNAME) . DIRECTORY_SEPARATOR,
110
            "file_dir" => pathinfo($this->config['path'], PATHINFO_FILENAME) . "_files" . DIRECTORY_SEPARATOR,
111
        ];
112
113
        $this->export();
114
    }
115
116
    /**
117
     * Direct export
118
     *
119
     * @return $this
120
     * @throws ExportException
121
     */
122
    protected function export()
123
    {
124
        $filesystem = new Filesystem();
125
        $handle = fopen($this->config['path'] . $this->prefix, "w");
126
127
        $checkPermissions = true;
128
        if (PHP_SAPI == 'cli') {
129
            $checkPermissions = false;
130
        }
131
132
        if (!$this->export->Init(
133
            $handle,
134
            $this->config["id"],
135
            false,
136
            true,
137
            $this->session["work_dir"],
138
            $this->session["file_dir"],
139
            $checkPermissions
140
        )
141
        ) {
142
            throw new ExportException('Failed to initialize export');
143
        }
144
145
        $this->export->DoNotDownloadCloudFiles();
146
        $this->export->StartExport();
147
148
        $this->export->StartExportMetadata();
149
        $this->export->ExportProperties($this->session["property_map"]);
150
        $this->export->ExportSections(
151
            $this->session["section_map"],
152
            time(),
153
            $this->config['interval'],
154
            $this->config["sections"],
155
            $this->session["property_map"]
156
        );
157
        $this->export->EndExportMetadata();
158
159
        $this->export->StartExportCatalog();
160
        $this->export->ExportElements(
161
            $this->session["property_map"],
162
            $this->session["section_map"],
163
            time(),
164
            $this->config['interval'],
165
            0,
166
            $this->config["elements"]
167
        );
168
        $this->export->EndExportCatalog();
169
170
        $this->export->ExportProductSets();
171
        $this->export->EndExport();
172
173
        fclose($handle);
174
        $filesystem->remove($this->config['path']);
175
        $filesystem->rename($this->config['path'] . $this->prefix, $this->config['path'], true);
176
    }
177
}