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

Export   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

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