Advertisement
Guest User

Untitled

a guest
Aug 14th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use App\OcProduct;
  6. use ElForastero\Transliterate\Transliteration;
  7. use Illuminate\Console\Command;
  8.  
  9. /**
  10. * Class ParseProductsXml
  11. * @package App\Console\Commandsw
  12. */
  13. abstract class ParseAbstract extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature;
  21.  
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description;
  28. protected $mpn;
  29. protected $url;
  30. protected $xml;
  31.  
  32. /**
  33. * Create a new command instance.
  34. *
  35. * @return void
  36. */
  37. public function __construct()
  38. {
  39. parent::__construct();
  40. try {
  41. if ($this->url !== null) {
  42. $content = file_get_contents($this->url);
  43. if (!empty($content)) {
  44. $this->xml = new \SimpleXMLElement($content);
  45. }
  46. }
  47. } catch (\Throwable $throwable) {
  48. //$this->error($throwable->getMessage());
  49. }
  50. }
  51.  
  52. /**
  53. * @param string $string
  54. * @return string
  55. */
  56. public static function sanitizeString(string $string): string
  57. {
  58. // Replace wrong
  59. $string = str_replace('&amp;quot;', '', $string);
  60.  
  61. // Remove tabs
  62. $string = trim(preg_replace('/\t+/', '', $string));
  63.  
  64. // Remove unneeded spaces
  65. $string = preg_replace('/\s+/', ' ', $string);
  66.  
  67. return $string;
  68. }
  69.  
  70. /**
  71. * @param string $string
  72. * @return int
  73. */
  74. public static function getQty(string $string): int
  75. {
  76. if ($string === 'достаточное кол-во') {
  77. return 10;
  78. }
  79. return (integer)preg_replace('/[^0-9,.]/', '', $string);
  80. }
  81.  
  82. /**
  83. * @param string $string
  84. * @param int $productId
  85. * @return string
  86. */
  87. public static function transliterate(string $string, int $productId): string
  88. {
  89. $string = $productId . '-' . Transliteration::make($string, ['type' => 'url', 'lowercase' => true]);
  90.  
  91. $string = trim(str_limit($string, 255, ''));
  92.  
  93. return rtrim($string, '-');
  94. }
  95.  
  96. /**
  97. * @param OcProduct $product
  98. * @param string $title
  99. * @return null|string
  100. */
  101. public function getProductName(OcProduct $product, string $title): string
  102. {
  103. if (optional($product->description)->name === '' || optional($product->description)->name === null) {
  104. return $this->optimizeWhiteSpaces($title);
  105. }
  106. return $this->optimizeWhiteSpaces(optional($product->description)->name);
  107. }
  108.  
  109. private function optimizeWhiteSpaces(string $string): string
  110. {
  111. return preg_replace('!\s+!', ' ', $string);
  112. }
  113.  
  114. /**
  115. * @param OcProduct $product
  116. * @param string $title
  117. * @return string
  118. */
  119. public function getProductMetaTitle(OcProduct $product, string $title): string
  120. {
  121. $description = optional($product->description);
  122.  
  123. $title = $this->sanitizeMeta($title);
  124. if ($description->meta_title === '' || $description->meta_title === null) {
  125. return "$title недорого купить в Москве| MaXtreme";
  126. }
  127. return $this->sanitizeMeta($description->meta_title);
  128. }
  129.  
  130. /**
  131. * @param string $title
  132. * @return mixed
  133. */
  134. private function sanitizeMeta(string $title): string
  135. {
  136. $title = str_replace(['<', '>', '{', '}', '*', '\'', '"', ','], ' ', $title);
  137. // Removing multiple spaces
  138. $title = $this->optimizeWhiteSpaces($title);
  139. return $title;
  140. }
  141.  
  142. /**
  143. * @param OcProduct $product
  144. * @return string
  145. */
  146. public function getProductMetaDescription(OcProduct $product): string
  147. {
  148. $title = $this->sanitizeMeta(optional($product->description)->name);
  149. return "Купить $title дешево онлайн! $title с бесплатной доставкой по Москве! $title заказать со скидкой в интернет магазине тренажеров Maxtreme! Консультация специалиста, отзывы, скидки.";
  150. }
  151.  
  152. /**
  153. * @param OcProduct $product
  154. * @return string
  155. */
  156. public function getProductMetaKeyword(OcProduct $product): string
  157. {
  158. $description = \optional($product->description);
  159. $title = $this->sanitizeMeta($description->name);
  160. return "$title Москва, $title купить, $title интернет магазин, $title со скидкой, $title по акции, $title недорого";
  161. }
  162.  
  163. /**
  164. * @param int $qty
  165. * @return int 7 - in stock, 5 - out of stock
  166. */
  167. public function getProductStockStatus(int $qty): int
  168. {
  169. return (bool)$qty ? 7 : 5;
  170. }
  171.  
  172. /**
  173. * @param array $productData
  174. * @return OcProduct
  175. */
  176. public function insertProduct(array $productData): OcProduct
  177. {
  178. $productData['sku'] = str_replace(['.', ','], '', $productData['sku']);
  179. $productData['model'] = str_replace(['.', ','], '', $productData['model']);
  180.  
  181. $product = OcProduct::where('sku', $productData['sku'])->first();
  182.  
  183. if ($product === null || $product->mpn === null || $product->mpn === $this->mpn) {
  184. $product = OcProduct::updateOrCreate(
  185. [
  186. 'sku' => $productData['sku'],
  187. ], $productData
  188. );
  189. } elseif ($product->price > $productData['price'] && (int)$productData['quantity'] > 0) {
  190. $product = OcProduct::updateOrCreate(
  191. [
  192. 'sku' => $productData['sku'],
  193. ], $productData
  194. );
  195. }
  196.  
  197. return $product;
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement