![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/Ecombricks/InventoryInventoryCatalog/Model/ResourceModel/SourceItemOption/ |
<?php /** * Copyright © eComBricks. All rights reserved. * See LICENSE.txt for license details. */ declare(strict_types=1); namespace Ecombricks\InventoryInventoryCatalog\Model\ResourceModel\SourceItemOption; /** * Save source item options resource */ class Save { /** * Connection provider * * @var \Ecombricks\Framework\Model\ResourceModel\Db\ConnectionProvider */ protected $connectionProvider; /** * Table name * * @var string */ protected $tableName; /** * Constructor * * @param \Ecombricks\Framework\Model\ResourceModel\Db\ConnectionProvider $connectionProvider * @param string $tableName * @return void */ public function __construct( \Ecombricks\Framework\Model\ResourceModel\Db\ConnectionProvider $connectionProvider, string $tableName ) { $this->connectionProvider = $connectionProvider; $this->tableName = $tableName; } /** * Get columns SQL * * @param array $columns * @return string */ protected function getColumnsSql(array $columns): string { return implode(', ', array_map([$this->connectionProvider->getConnection(), 'quoteIdentifier'], $columns)); } /** * Get values SQL * * @param \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface[] $sourceItemOptions * @return string */ protected function getValuesSql(array $sourceItemOptions): string { return rtrim(str_repeat('(?, ?, ?), ', count($sourceItemOptions)), ', '); } /** * Get on duplicate SQL * * @param array $fields * @return string */ protected function getOnDuplicateSql(array $fields): string { $connection = $this->connectionProvider->getConnection(); $updateParts = []; foreach ($fields as $field) { $updateParts[] = sprintf('%1$s = VALUES(%1$s)', $connection->quoteIdentifier($field)); } return 'ON DUPLICATE KEY UPDATE '.implode(', ', $updateParts); } /** * Get data * * @param \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface[] $sourceItemOptions * @return array */ protected function getData(array $sourceItemOptions): array { $data = []; foreach ($sourceItemOptions as $sourceItemOption) { $data = array_merge($data, [ $sourceItemOption->getSourceCode(), $sourceItemOption->getSku(), $sourceItemOption->getValue(), ]); } return $data; } /** * Execute * * @param \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface[] $sourceItemOptions * @return \Ecombricks\InventoryInventoryCatalog\Model\ResourceModel\SourceItemOption\Save */ public function execute(array $sourceItemOptions): \Ecombricks\InventoryInventoryCatalog\Model\ResourceModel\SourceItemOption\Save { if (!count($sourceItemOptions)) { return $this; } $this->connectionProvider->getConnection()->query( sprintf( 'INSERT INTO %s (%s) VALUES %s %s', $this->connectionProvider->getTable($this->tableName), $this->getColumnsSql([ \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::SOURCE_CODE, \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::SKU, \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::VALUE, ]), $this->getValuesSql($sourceItemOptions), $this->getOnDuplicateSql([ \Ecombricks\InventoryInventoryCatalog\Api\Data\SourceItemOptionInterface::VALUE, ]) ), $this->getData($sourceItemOptions) ); return $this; } }