Spamworldpro Mini Shell
Spamworldpro


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/old/vendor/magento/module-catalog/Model/ResourceModel/Product/Website/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/vendor/magento/module-catalog/Model/ResourceModel/Product/Website/Link.php
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Catalog\Model\ResourceModel\Product\Website;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\App\ResourceConnection;

/**
 * Class Link used for assign website to the product
 */
class Link
{
    /**
     * @var \Magento\Framework\App\ResourceConnection
     */
    private $resourceConnection;

    /**
     * Link constructor.
     * @param ResourceConnection $resourceConnection
     */
    public function __construct(
        ResourceConnection $resourceConnection
    ) {
        $this->resourceConnection = $resourceConnection;
    }

    /**
     * Retrieve associated with product websites ids
     *
     * @param int $productId
     * @return array
     */
    public function getWebsiteIdsByProductId($productId)
    {
        $connection = $this->resourceConnection->getConnection();

        $select = $connection->select()->from(
            $this->getProductWebsiteTable(),
            'website_id'
        )->where(
            'product_id = ?',
            (int) $productId
        );

        return $connection->fetchCol($select);
    }

    /**
     * Return true - if websites was changed, and false - if not
     *
     * @param ProductInterface $product
     * @param array $websiteIds
     * @return bool
     */
    public function saveWebsiteIds(ProductInterface $product, array $websiteIds)
    {
        $productId = (int) $product->getId();
        return $this->updateProductWebsite($productId, $websiteIds);
    }

    /**
     * Get Product website table
     *
     * @return string
     */
    private function getProductWebsiteTable()
    {
        return $this->resourceConnection->getTableName('catalog_product_website');
    }

    /**
     * Update product website table
     *
     * @param int $productId
     * @param array $websiteIds
     * @return bool
     */
    public function updateProductWebsite(int $productId, array $websiteIds): bool
    {
        $connection = $this->resourceConnection->getConnection();

        $oldWebsiteIds = $this->getWebsiteIdsByProductId($productId);
        $insert = array_diff($websiteIds, $oldWebsiteIds);
        $delete = array_diff($oldWebsiteIds, $websiteIds);

        if (!empty($insert)) {
            $data = [];
            foreach ($insert as $websiteId) {
                $data[] = ['product_id' => $productId, 'website_id' => (int)$websiteId];
            }
            $connection->insertMultiple($this->getProductWebsiteTable(), $data);
        }

        if (!empty($delete)) {
            foreach ($delete as $websiteId) {
                $condition = ['product_id = ?' => $productId, 'website_id = ?' => (int)$websiteId];
                $connection->delete($this->getProductWebsiteTable(), $condition);
            }
        }

        if (!empty($insert) || !empty($delete)) {
            return true;
        }

        return false;
    }
}

Spamworldpro Mini