Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Console\Commands;
- use App\OcProduct;
- use ElForastero\Transliterate\Transliteration;
- use Illuminate\Console\Command;
- /**
- * Class ParseProductsXml
- * @package App\Console\Commandsw
- */
- abstract class ParseAbstract extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature;
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description;
- protected $mpn;
- protected $url;
- protected $xml;
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- try {
- if ($this->url !== null) {
- $content = file_get_contents($this->url);
- if (!empty($content)) {
- $this->xml = new \SimpleXMLElement($content);
- }
- }
- } catch (\Throwable $throwable) {
- //$this->error($throwable->getMessage());
- }
- }
- /**
- * @param string $string
- * @return string
- */
- public static function sanitizeString(string $string): string
- {
- // Replace wrong
- $string = str_replace('&quot;', '', $string);
- // Remove tabs
- $string = trim(preg_replace('/\t+/', '', $string));
- // Remove unneeded spaces
- $string = preg_replace('/\s+/', ' ', $string);
- return $string;
- }
- /**
- * @param string $string
- * @return int
- */
- public static function getQty(string $string): int
- {
- if ($string === 'достаточное кол-во') {
- return 10;
- }
- return (integer)preg_replace('/[^0-9,.]/', '', $string);
- }
- /**
- * @param string $string
- * @param int $productId
- * @return string
- */
- public static function transliterate(string $string, int $productId): string
- {
- $string = $productId . '-' . Transliteration::make($string, ['type' => 'url', 'lowercase' => true]);
- $string = trim(str_limit($string, 255, ''));
- return rtrim($string, '-');
- }
- /**
- * @param OcProduct $product
- * @param string $title
- * @return null|string
- */
- public function getProductName(OcProduct $product, string $title): string
- {
- if (optional($product->description)->name === '' || optional($product->description)->name === null) {
- return $this->optimizeWhiteSpaces($title);
- }
- return $this->optimizeWhiteSpaces(optional($product->description)->name);
- }
- private function optimizeWhiteSpaces(string $string): string
- {
- return preg_replace('!\s+!', ' ', $string);
- }
- /**
- * @param OcProduct $product
- * @param string $title
- * @return string
- */
- public function getProductMetaTitle(OcProduct $product, string $title): string
- {
- $description = optional($product->description);
- $title = $this->sanitizeMeta($title);
- if ($description->meta_title === '' || $description->meta_title === null) {
- return "$title недорого купить в Москве| MaXtreme";
- }
- return $this->sanitizeMeta($description->meta_title);
- }
- /**
- * @param string $title
- * @return mixed
- */
- private function sanitizeMeta(string $title): string
- {
- $title = str_replace(['<', '>', '{', '}', '*', '\'', '"', ','], ' ', $title);
- // Removing multiple spaces
- $title = $this->optimizeWhiteSpaces($title);
- return $title;
- }
- /**
- * @param OcProduct $product
- * @return string
- */
- public function getProductMetaDescription(OcProduct $product): string
- {
- $title = $this->sanitizeMeta(optional($product->description)->name);
- return "Купить $title дешево онлайн! $title с бесплатной доставкой по Москве! $title заказать со скидкой в интернет магазине тренажеров Maxtreme! Консультация специалиста, отзывы, скидки.";
- }
- /**
- * @param OcProduct $product
- * @return string
- */
- public function getProductMetaKeyword(OcProduct $product): string
- {
- $description = \optional($product->description);
- $title = $this->sanitizeMeta($description->name);
- return "$title Москва, $title купить, $title интернет магазин, $title со скидкой, $title по акции, $title недорого";
- }
- /**
- * @param int $qty
- * @return int 7 - in stock, 5 - out of stock
- */
- public function getProductStockStatus(int $qty): int
- {
- return (bool)$qty ? 7 : 5;
- }
- /**
- * @param array $productData
- * @return OcProduct
- */
- public function insertProduct(array $productData): OcProduct
- {
- $productData['sku'] = str_replace(['.', ','], '', $productData['sku']);
- $productData['model'] = str_replace(['.', ','], '', $productData['model']);
- $product = OcProduct::where('sku', $productData['sku'])->first();
- if ($product === null || $product->mpn === null || $product->mpn === $this->mpn) {
- $product = OcProduct::updateOrCreate(
- [
- 'sku' => $productData['sku'],
- ], $productData
- );
- } elseif ($product->price > $productData['price'] && (int)$productData['quantity'] > 0) {
- $product = OcProduct::updateOrCreate(
- [
- 'sku' => $productData['sku'],
- ], $productData
- );
- }
- return $product;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement