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

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