Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
sftpqueue.php
1<?php
2
4
5use \Bitrix\Main\Type\DateTime;
6use \Bitrix\Main\SystemException;
7use \Bitrix\Sale\TradingPlatform\Sftp;
8use \Bitrix\Main\ArgumentNullException;
9use \Bitrix\Sale\TradingPlatform\Logger;
10use \Bitrix\Sale\TradingPlatform\Ebay\Ebay;
11use \Bitrix\Sale\TradingPlatform\Ebay\Feed\QueueTable;
12use \Bitrix\Sale\TradingPlatform\Ebay\Feed\ResultsTable;
13
15{
16 // todo: check if the record alredy exist
17
18 protected $feedType;
19 protected $coverTag = null;
20 protected $schemeFileName = null;
21 protected $fileNameSalt;
22 protected $remotePath;
23 protected $siteId;
24 protected $timer = null;
25 protected $path;
26
27 public function __construct(array $params)
28 {
29 if(!isset($params["FEED_TYPE"]) || $params["FEED_TYPE"] == '')
30 throw new ArgumentNullException("FEED_TYPE");
31
32 if($this->feedType == "ORDER_ACK")
33 $this->feedType = "order-ack";
34 else
35 $this->feedType = mb_strtolower($params["FEED_TYPE"]);
36
37 if(!isset($params["SITE_ID"]) || $params["SITE_ID"] == '')
38 throw new ArgumentNullException("SITE_ID");
39
40 $this->siteId = $params["SITE_ID"];
41
42 if(isset($params["COVER_TAG"]) && $params["COVER_TAG"] <> '')
43 $this->coverTag = $params["COVER_TAG"];
44
45 if(isset($params["SCHEMA_FILE_NAME"]))
46 $this->schemeFileName = $params["SCHEMA_FILE_NAME"];
47
48 if(isset($params["TIMER"]))
49 $this->timer = $params["TIMER"];
50
51 $this->fileNameSalt = time();
52 $this->remotePath = "/store/".$this->feedType;
53 $this->path = \Bitrix\Sale\TradingPlatform\Ebay\Helper::getSftpPath()."/".$this->feedType;
54 }
55
56 protected function prepareFile($file)
57 {
58 $res = file_put_contents($file, '<?xml version="1.0" encoding="UTF-8"?>'."\n");
59
60 if(!$res)
61 throw new SystemException("Can't flush data feed \"".$this->feedType."\" to file ".$file);
62
63 if($this->coverTag !== null)
64 file_put_contents($file, "<".$this->coverTag.">\n", FILE_APPEND);
65 }
66
67 protected function flushData()
68 {
69 $fileXml = "";
70
71 $feedDataRes = QueueTable::getList(array(
72 "filter" => array(
73 "FEED_TYPE" => $this->feedType
74 )
75 ));
76
77 $filePrepared = false;
78
79 while($feedData = $feedDataRes->fetch())
80 {
81 if(!$filePrepared)
82 {
83 $fileXml = $this->path."/xml/".$this->feedType."_".$this->fileNameSalt.".xml";
84 $this->prepareFile($fileXml);
85 $filePrepared = true;
86 }
87
88 Ebay::log(Logger::LOG_LEVEL_DEBUG, "EBAY_DATA_PROCESSOR_SFTPQUEUE_FLUSHING", $this->feedType, print_r($feedData["DATA"],true), $this->siteId);
89
90 if(mb_strtolower(SITE_CHARSET) != 'utf-8')
91 $feedData["DATA"] = \Bitrix\Main\Text\Encoding::convertEncoding($feedData["DATA"], SITE_CHARSET, 'UTF-8');
92
93 $res = file_put_contents($fileXml, $feedData["DATA"], FILE_APPEND);
94
95 if($res !== false)
96 QueueTable::delete($feedData["ID"]);
97 else
98 throw new SystemException("Can't flush data feed \"".$this->feedType."\" to file ".$fileXml);
99 }
100
101 if($this->coverTag !== null && $filePrepared)
102 file_put_contents($fileXml, "</".$this->coverTag.">\n", FILE_APPEND);
103
104 return $fileXml;
105 }
106
107 public function process($data)
108 {
109 return $this->addData($data);
110 }
111
112 public function addData($data)
113 {
114 $result = QueueTable::add(array(
115 "FEED_TYPE" => $this->feedType,
116 "DATA" => $data
117 ));
118
119 return $result->isSuccess();
120 }
121
122 public function sendData()
123 {
124 $xmlFile = $this->flushData();
125
126 if(!$xmlFile)
127 return false;
128
129 $tmpFile = $this->packData($xmlFile);
130 $zipFile = new \Bitrix\Main\IO\File($tmpFile);
131 $zipFile->rename($this->path."/zip/".$this->feedType."_".$this->fileNameSalt.".zip");
132 $this->sendDataSftp();
133
134 $checkResultsInterval = 5; //min.
135 \Bitrix\Sale\TradingPlatform\Ebay\Agent::add('RESULTS', $this->siteId, $checkResultsInterval, true);
136
137 return true;
138 }
139
140 protected function packData($xmlFile)
141 {
142 $tmpDir = $this->path."/tmp";
143 $archiveName = $tmpDir."/".$this->feedType."_".$this->fileNameSalt.".zip";
144 $oArchiver = \CBXArchive::GetArchive($archiveName, "ZIP");
145 $oArchiver->SetOptions(array(
146 "REMOVE_PATH" => $this->path."/xml",
147 "ADD_PATH" => $this->feedType
148 ));
149
150 if($oArchiver->Pack($xmlFile))
151 \Bitrix\Main\IO\File::deleteFile($xmlFile);
152
153 return $archiveName;
154 }
155
156 protected function sendDataSftp()
157 {
158 $directory = new \Bitrix\Main\IO\Directory($this->path."/zip");
159
160 if(!$directory->isExists())
161 throw new SystemException("Directory".$this->path."/zip does not exist! ".__METHOD__);
162
163 $filesToSend = $directory->getChildren();
164
165 if(empty($filesToSend))
166 return false;
167
168 $sftp = \Bitrix\Sale\TradingPlatform\Ebay\Helper::getSftp($this->siteId);
169
170 if(!$sftp)
171 return false;
172
173 $sftp->connect();
174
175 for($i = 0; $i < count($filesToSend); $i++)
176 {
177 $directoryEntry = $filesToSend[$i];
178 $localPath = $directoryEntry->getPath();
179
180 if((!($directoryEntry instanceof \Bitrix\Main\IO\File)) || GetFileExtension($localPath) != "zip")
181 continue;
182
183 $remote = $this->remotePath."/".$directoryEntry->getName();
184
185 while(!$this->checkOuterConditions($sftp))
186 {
187 if($this->timer !== null && !$this->timer->check(15))
188 return false;
189
190 sleep(10);
191 }
192
193 if($sftp->uploadFile($localPath, $remote))
194 {
195 $directoryEntry->delete();
196 ResultsTable::add(array(
197 "FILENAME" => $directoryEntry->getName(),
198 "FEED_TYPE" => $this->feedType,
199 "UPLOAD_TIME" => DateTime::createFromTimestamp(time())
200 ));
201 Ebay::log(Logger::LOG_LEVEL_INFO, "EBAY_DATA_PROCESSOR_SFTPQUEUE_SEND", $remote, "File sent successfully.", $this->siteId);
202 }
203 }
204
205 return true;
206 }
207
208 protected function checkOuterConditions($sftp)
209 {
210 $files = $sftp->getFilesList($this->remotePath);
211
212 if(!empty($files))
213 return false;
214
215 if($this->feedType == "inventory" || $this->feedType == "image")
216 {
217 $filesProd = $sftp->getFilesList("/store/product");
218 $filesProdInProc = $sftp->getFilesList("/store/product/inprocess");
219
220 if(!empty($filesProd) || !empty($filesProdInProc))
221 return false;
222 }
223
224 return true;
225 }
226}
static createFromTimestamp($timestamp)
Definition datetime.php:246
static log($level, $type, $itemId, $description, $siteId)
Definition ebay.php:147