Completed
Pull Request — master (#20)
by
unknown
02:56
created

Export   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 152
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 152
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 12 1
A export() 0 55 3
1
<?php
2
3
namespace Notamedia\ConsoleJedi\Schema;
4
5
use Bitrix\Main\Loader;
6
use Notamedia\ConsoleJedi\Schema\Exception\ExportException;
7
use Symfony\Component\Filesystem\Filesystem;
8
9
/**
10
 * Class Export
11
 * @package Notamedia\ConsoleJedi\Schema
12
 */
13
class Export 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
     * @var string
32
     */
33
    protected $prefix = '.tmp';
34
35
    public function __construct()
36
    {
37
        $this->config = [
38
            'id' => '',
39
            'path' => '',
40
            'sections' => 'none',
41
            'elements' => 'none',
42
            'interval' => 30
43
        ];
44
45
        Loader::includeModule('iblock');
46
        $this->export = new \CIBlockCMLExport();
47
    }
48
49
    /**
50
     * @param int $id
51
     * @return $this
52
     */
53
    public function setId($id)
54
    {
55
        $this->config['id'] = intval($id);
56
        return $this;
57
    }
58
59
    /**
60
     * @param string $path
61
     * @return $this
62
     */
63
    public function setPath($path)
64
    {
65
        $this->config['path'] = $path;
66
        return $this;
67
    }
68
69
    /**
70
     * @param string $sections
71
     * @return $this
72
     */
73
    public function setSections($sections)
74
    {
75
        $this->config['sections'] = $sections;
76
        return $this;
77
    }
78
79
    /**
80
     * @param string $elements
81
     * @return $this
82
     */
83
    public function setElements($elements)
84
    {
85
        $this->config['elements'] = $elements;
86
        return $this;
87
    }
88
89
    /**
90
     * @return $this
91
     */
92
    public function execute()
93
    {
94
        $this->session = [
95
            "property_map" => false,
96
            "section_map" => false,
97
            "work_dir" => false,
98
            "file_dir" => false,
99
        ];
100
101
        $this->export();
102
        return $this;
103
    }
104
105
    /**
106
     * @return $this
107
     * @throws ExportException
108
     */
109
    protected function export()
110
    {
111
        $filesystem = new Filesystem();
112
        $handle = fopen($this->config['path'] . $this->prefix, "w");
113
114
        $checkPermissions = true;
115
        if (PHP_SAPI == 'cli') {
116
            $checkPermissions = false;
117
        }
118
119
        if (!$this->export->Init(
120
            $handle,
121
            $this->config["id"],
122
            false,
123
            true,
124
            $this->session["work_dir"],
125
            $this->session["file_dir"],
126
            $checkPermissions
127
        )
128
        ) {
129
            throw new ExportException('Failed to initialize export');
130
        }
131
132
        $this->export->DoNotDownloadCloudFiles();
133
        $this->export->StartExport();
134
135
        $this->export->StartExportMetadata();
136
        $this->export->ExportProperties($this->session["property_map"]);
137
        $this->export->ExportSections(
138
            $this->session["section_map"],
139
            time(),
140
            $this->config['interval'],
141
            $this->config["sections"],
142
            $this->session["property_map"]
143
        );
144
        $this->export->EndExportMetadata();
145
146
        $this->export->StartExportCatalog();
147
        $this->export->ExportElements(
148
            $this->session["property_map"],
149
            $this->session["section_map"],
150
            time(),
151
            $this->config['interval'],
152
            0,
153
            $this->config["elements"]
154
        );
155
        $this->export->EndExportCatalog();
156
157
        $this->export->ExportProductSets();
158
        $this->export->EndExport();
159
160
        fclose($handle);
161
        $filesystem->remove($this->config['path']);
162
        $filesystem->rename($this->config['path'] . $this->prefix, $this->config['path'], true);
163
    }
164
}