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/Ecombricks/InventoryInventoryReservations/Model/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/Ecombricks/InventoryInventoryReservations/Model/ReservationBuilder.php
<?php
/**
 * Copyright © eComBricks. All rights reserved.
 * See LICENSE.txt for license details.
 */
declare(strict_types=1);

namespace Ecombricks\InventoryInventoryReservations\Model;

/**
 * Reservation builder
 */
class ReservationBuilder extends \Magento\InventoryReservations\Model\ReservationBuilder 
    implements \Ecombricks\InventoryInventoryReservations\Model\ReservationBuilderInterface 
{
    
    /**
     * Source code
     * 
     * @var string
     */
    protected $sourceCode;
    
    /**
     * SKU
     * 
     * @var string
     */
    protected $sku;

    /**
     * Quantity
     * 
     * @var float
     */
    protected $quantity;

    /**
     * Metadata
     * 
     * @var string
     */
    protected $metadata;
    
    /**
     * Object manager
     * 
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $objectManager;
    
    /**
     * Snake to camel case converter
     * 
     * @var \Magento\InventoryReservations\Model\SnakeToCamelCaseConverter
     */
    protected $snakeToCamelCaseConverter;
    
    /**
     * Validation result factory
     * 
     * @var \Magento\Framework\Validation\ValidationResultFactory
     */
    protected $validationResultFactory;
    
    /**
     * Constructor
     * 
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param \Magento\InventoryReservations\Model\SnakeToCamelCaseConverter $snakeToCamelCaseConverter
     * @param \Magento\Framework\Validation\ValidationResultFactory $validationResultFactory
     * @return void
     */
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager,
        \Magento\InventoryReservations\Model\SnakeToCamelCaseConverter $snakeToCamelCaseConverter,
        \Magento\Framework\Validation\ValidationResultFactory $validationResultFactory
    )
    {
        parent::__construct(
            $objectManager,
            $snakeToCamelCaseConverter,
            $validationResultFactory
        );
        $this->objectManager = $objectManager;
        $this->snakeToCamelCaseConverter = $snakeToCamelCaseConverter;
        $this->validationResultFactory = $validationResultFactory;
    }

    /**
     * Set source code
     * 
     * @param string $sourceCode
     * @return \Magento\InventoryReservationsApi\Model\ReservationBuilderInterface
     */
    public function setSourceCode(string $sourceCode): \Ecombricks\InventoryInventoryReservations\Model\ReservationBuilderInterface
    {
        $this->sourceCode = $sourceCode;
        return $this;
    }
    
    /**
     * Set SKU
     * 
     * @param string $sku
     * @return \Magento\InventoryReservationsApi\Model\ReservationBuilderInterface
     */
    public function setSku(string $sku): \Magento\InventoryReservationsApi\Model\ReservationBuilderInterface
    {
        $this->sku = $sku;
        return $this;
    }

    /**
     * Set quantity
     * 
     * @param float $quantity
     * @return \Magento\InventoryReservationsApi\Model\ReservationBuilderInterface
     */
    public function setQuantity(float $quantity): \Magento\InventoryReservationsApi\Model\ReservationBuilderInterface
    {
        $this->quantity = $quantity;
        return $this;
    }

    /**
     * Set metadata
     * 
     * @param string|null $metadata
     * @return \Magento\InventoryReservationsApi\Model\ReservationBuilderInterface
     */
    public function setMetadata(string $metadata = null): \Magento\InventoryReservationsApi\Model\ReservationBuilderInterface
    {
        $this->metadata = $metadata;
        return $this;
    }
    
    /**
     * Validate
     * 
     * @return \Magento\Framework\Validation\ValidationResult
     */
    protected function validate()
    {
        $errors = [];
        if (null === $this->sourceCode) {
            $errors[] = __('"%field" can not be empty.', ['field' => \Ecombricks\InventoryInventoryReservations\Model\ReservationInterface::SOURCE_CODE]);
        }
        if (null === $this->sku || '' === trim($this->sku)) {
            $errors[] = __('"%field" can not be empty.', ['field' => \Ecombricks\InventoryInventoryReservations\Model\ReservationInterface::SKU]);
        }
        if (null === $this->quantity) {
            $errors[] = __('"%field" can not be null.', ['field' => \Ecombricks\InventoryInventoryReservations\Model\ReservationInterface::QUANTITY]);
        }
        return $this->validationResultFactory->create(['errors' => $errors]);
    }
    
    /**
     * Convert array keys from snake to camel case
     *
     * @param array $array
     * @return array
     */
    protected function convertArrayKeysFromSnakeToCamelCase(array $array): array
    {
        $convertedArrayKeys = $this->snakeToCamelCaseConverter->convert(array_keys($array));
        return array_combine($convertedArrayKeys, array_values($array));
    }
    
    /**
     * Reset
     * 
     * @return void
     */
    protected function reset()
    {
        $this->sourceCode = null;
        $this->sku = null;
        $this->quantity = null;
        $this->metadata = null;
    }
    
    /**
     * Build
     * 
     * @return \Magento\InventoryReservationsApi\Model\ReservationInterface
     * @throws \Magento\Framework\Validation\ValidationException
     */
    public function build(): \Magento\InventoryReservationsApi\Model\ReservationInterface
    {
        $validationResult = $this->validate();
        if (!$validationResult->isValid()) {
            throw new \Magento\Framework\Validation\ValidationException(__('Validation error'), null, 0, $validationResult);
        }
        $data = [
            \Ecombricks\InventoryInventoryReservations\Model\ReservationInterface::RESERVATION_ID => null,
            \Ecombricks\InventoryInventoryReservations\Model\ReservationInterface::SOURCE_CODE => $this->sourceCode,
            \Ecombricks\InventoryInventoryReservations\Model\ReservationInterface::SKU => $this->sku,
            \Ecombricks\InventoryInventoryReservations\Model\ReservationInterface::QUANTITY => $this->quantity,
            \Ecombricks\InventoryInventoryReservations\Model\ReservationInterface::METADATA => $this->metadata,
        ];
        $arguments = $this->convertArrayKeysFromSnakeToCamelCase($data);
        $reservation = $this->objectManager->create(\Ecombricks\InventoryInventoryReservations\Model\ReservationInterface::class, $arguments);
        $this->reset();
        return $reservation;
    }

}

Spamworldpro Mini