1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 
<?php
/* --------------------------------------------------------------
   ProductRepositoryReader.inc.php 2018-02-14
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2018 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/**
 * Class ProductRepositoryReader
 *
 * @category   System
 * @package    Product
 * @subpackage Repositories
 */
class ProductRepositoryReader implements ProductRepositoryReaderInterface
{
    /**
     * DB Connection.
     *
     * @var CI_DB_query_builder
     */
    protected $db;
    
    /**
     * Product Factory
     *
     * @var ProductFactory
     */
    protected $productFactory;
    
    
    /**
     * Customer Status Provider
     *
     * @var CustomerStatusProviderInterface
     */
    protected $customerStatusProvider;
    
    
    /**
     * ProductRepositoryReader constructor.
     *
     * @param CI_DB_query_builder             $db
     * @param ProductFactoryInterface         $productFactory
     * @param CustomerStatusProviderInterface $customerStatusProvider
     */
    public function __construct(CI_DB_query_builder $db,
                                ProductFactoryInterface $productFactory,
                                CustomerStatusProviderInterface $customerStatusProvider)
    {
        $this->db                     = $db;
        $this->productFactory         = $productFactory;
        $this->customerStatusProvider = $customerStatusProvider;
    }
    
    
    /**
     * Returns a product entity instance by the given product id.
     *
     * @param IdType $productId Id of product entity.
     *
     * @throws UnexpectedValueException
     * @throws InvalidArgumentException
     *
     * @return StoredProductInterface Product entity with the expected product id.
     */
    public function getById(IdType $productId)
    {
        $product = $this->db->get_where('products', array('products_id' => $productId->asInt()))->row_array();
        
        $productDescriptionQuery = $this->db->select('products_description.*, languages.code AS language_code')
                                            ->from('products_description')
                                            ->join('languages',
                                                   'languages.languages_id = products_description.language_id', 'inner')
                                            ->where('products_description.products_id', $productId->asInt());
        
        $productDescription = $productDescriptionQuery->get()->result_array();
        
        if($product === null)
        {
            throw new UnexpectedValueException('The requested product was not found in database (ID: '
                                               . $productId->asInt() . ')');
        }
        
        if(count($productDescription) === 0)
        {
            throw new UnexpectedValueException('The requested product description was not found in database (ID: '
                                               . $productId->asInt() . ')');
        }
        
        // Set quantity unit id of this product
        $product['quantity_unit_id'] = 0;
        $quantityUnit                = $this->db->get_where('products_quantity_unit',
                                                            ['products_id' => $productId->asInt()])->row_array();
        if($product !== null)
        {
            $product['quantity_unit_id'] = $quantityUnit['quantity_unit_id'];
        }
        
        return $this->_createProductByArray($product, $productDescription);
    }
    
    
    /**
     * Returns the count of all product entries.
     *
     * @return int
     */
    public function getProductsCount()
    {
        return $this->db->count_all('products');
    }
    
    /*
     | -----------------------------------------------------------------------------------------------------------------
     | Helper Methods
     | -----------------------------------------------------------------------------------------------------------------
     */
    
    /**
     * Create Product by Array
     *
     * Creates and returns a StoredProduct.
     *
     * @param array $product            Product query result.
     * @param array $productDescription Product description query result.
     *
     * @throws InvalidArgumentException
     * @throws UnexpectedValueException
     *
     * @return StoredProduct StoredProduct object.
     */
    protected function _createProductByArray(array $product, array $productDescription)
    {
        $storedProduct = $this->productFactory->createStoredProduct(new IdType($product['products_id']));
        
        // Set language specific data
        foreach($productDescription as $row)
        {
            $languageCode = new LanguageCode(new NonEmptyStringType($row['language_code']));
            
            $storedProduct->setName(new StringType((string)$row['products_name']), $languageCode);
            $storedProduct->setDescription(new StringType((string)$row['products_description']), $languageCode);
            $storedProduct->setShortDescription(new StringType((string)$row['products_short_description']),
                                                $languageCode);
            $storedProduct->setKeywords(new StringType((string)$row['products_keywords']), $languageCode);
            $storedProduct->setMetaTitle(new StringType((string)$row['products_meta_title']), $languageCode);
            $storedProduct->setMetaDescription(new StringType((string)$row['products_meta_description']),
                                               $languageCode);
            $storedProduct->setMetaKeywords(new StringType((string)$row['products_meta_keywords']), $languageCode);
            $storedProduct->setInfoUrl(new StringType((string)$row['products_url']), $languageCode);
            $storedProduct->setUrlKeywords(new StringType((string)$row['gm_url_keywords']), $languageCode);
            $storedProduct->setCheckoutInformation(new StringType((string)$row['checkout_information']), $languageCode);
            $storedProduct->setViewedCount(new IntType((int)$row['products_viewed']), $languageCode);
        }
        
        $storedProduct->setActive(new BoolType((bool)$product['products_status']));
        $storedProduct->setSortOrder(new IntType((int)$product['products_sort']));
        $storedProduct->setOrderedCount(new IntType((int)$product['products_ordered']));
        $storedProduct->setProductModel(new StringType((string)$product['products_model']));
        $storedProduct->setEan(new StringType((string)$product['products_ean']));
        $storedProduct->setPrice(new DecimalType((float)$product['products_price']));
        $storedProduct->setTaxClassId(new IdType((int)$product['products_tax_class_id']));
        $storedProduct->setQuantity(new DecimalType((float)$product['products_quantity']));
        $storedProduct->setWeight(new DecimalType((float)$product['products_weight']));
        $storedProduct->setDiscountAllowed(new DecimalType((float)$product['products_discount_allowed']));
        $storedProduct->setShippingCosts(new DecimalType((float)$product['nc_ultra_shipping_costs']));
        $storedProduct->setShippingTimeId(new IdType((int)$product['products_shippingtime']));
        $storedProduct->setProductTypeId(new IdType((int)$product['product_type']));
        $storedProduct->setManufacturerId(new IdType((int)$product['manufacturers_id']));
        $storedProduct->setQuantityUnitId(new IdType((int)$product['quantity_unit_id']));
        $storedProduct->setFsk18(new BoolType((bool)$product['products_fsk18']));
        $storedProduct->setVpeActive(new BoolType((bool)$product['products_vpe_status']));
        $storedProduct->setVpeId(new IdType((int)$product['products_vpe']));
        $storedProduct->setVpeValue(new DecimalType((float)$product['products_vpe_value']));
        $storedProduct->setAddedDateTime(new EmptyDateTime($product['products_date_added']));
        $storedProduct->setAvailableDateTime(new EmptyDateTime($product['products_date_available']));
        $storedProduct->setLastModifiedDateTime(new EmptyDateTime($product['products_last_modified']));
        
        return $storedProduct;
    }
}