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

/* --------------------------------------------------------------
   QuantityUnitStorage.inc.php 2017-08-08
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2017 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/**
 * Class QuantityUnitStorage
 *
 * @category   System
 * @package    QuantityUnit
 * @subpackage Repositories
 */
class QuantityUnitStorage implements QuantityUnitStorageInterface
{
    /**
     * @var \CI_DB_query_builder
     */
    protected $queryBuilder;
    
    /**
     * @var \LanguageProvider
     */
    protected $languageProvider;
    
    
    /**
     * QuantityUnitStorage constructor.
     *
     * @param \CI_DB_query_builder $queryBuilder
     * @param \LanguageProvider    $languageProvider
     */
    public function __construct(CI_DB_query_builder $queryBuilder, LanguageProvider $languageProvider)
    {
        $this->queryBuilder     = $queryBuilder;
        $this->languageProvider = $languageProvider;
    }
    
    
    /**
     * Returns quantity unit entity data by the given id.
     *
     * @param \IdType $quantityUnitId
     *
     * @throws \EntityNotFoundException If no entity was found by provided id.
     * @return array
     */
    public function getById(IdType $quantityUnitId)
    {
        $rawData = $this->queryBuilder->select()
                                      ->from('quantity_unit')
                                      ->join('quantity_unit_description',
                                             'quantity_unit.quantity_unit_id = quantity_unit_description.quantity_unit_id')
                                      ->where('quantity_unit.quantity_unit_id', $quantityUnitId->asInt())
                                      ->get()
                                      ->result_array() ? : [];
        
        if(count($rawData) === 0)
        {
            throw new EntityNotFoundException('Customer group entity was not found with provided id "'
                                              . $quantityUnitId->asInt() . '"');
        }
        
        $result     = ['id' => $quantityUnitId->asInt()];
        $namesArray = [];
        
        foreach($rawData as $data)
        {
            $namesArray[$data['language_id']] = $data['unit_name'];
        }
        $result['names'] = $namesArray;
        
        return $result;
    }
    
    
    /**
     * Returns all quantity unit entities data as array.
     *
     * @return array
     */
    public function getAll()
    {
        $rawData = $this->queryBuilder->select()
                                      ->from('quantity_unit')
                                      ->join('quantity_unit_description',
                                             'quantity_unit.quantity_unit_id = quantity_unit_description.quantity_unit_id')
                                      ->order_by('quantity_unit.quantity_unit_id')
                                      ->get()
                                      ->result_array();
        
        $formattedArray = [];
        $names          = [];
        
        foreach($rawData as $data)
        {
            if(isset($oldId) && $oldId !== (int)$data['quantity_unit_id'])
            {
                $formattedArray[] = $names;
                $names            = [];
            }
            $oldId = (int)$data['quantity_unit_id'];
            
            $names['id']                          = $oldId;
            $names['names'][$data['language_id']] = $data['unit_name'];
        }
        if(count($rawData) > 0)
        {
            $formattedArray[] = $names;
        }
        
        return $formattedArray;
    }
    
    
    /**
     * Saves quantity unit entity data in database.
     *
     * @param \QuantityUnitInterface $quantityUnit Quantity unit to be saved.
     *
     * @return \QuantityUnitStorageInterface Same instance for chained method calls.
     */
    public function save(QuantityUnitInterface $quantityUnit)
    {
        $this->queryBuilder->insert('quantity_unit', ['quantity_unit_id' => null]);
        $quantityUnitId = $this->queryBuilder->insert_id();
        
        foreach($quantityUnit->getNames() as $languageCode => $name)
        {
            $this->queryBuilder->insert('quantity_unit_description', [
                'quantity_unit_id' => $quantityUnitId,
                'language_id'      => $this->languageProvider->getIdByCode(MainFactory::create('LanguageCode',
                                                                                               new StringType($languageCode))),
                'unit_name'        => $name
            ]);
        }
        
        $quantityUnit->setId(new IdType($quantityUnitId));
        
        return $this;
    }
    
    
    /**
     * Updates quantity unit entity data in database.
     *
     * @param \QuantityUnitInterface $quantityUnit Quantity unit to be updated.
     *
     * @return \QuantityUnitStorageInterface Same instance for chained method calls.
     */
    public function update(QuantityUnitInterface $quantityUnit)
    {
        foreach($quantityUnit->getNames() as $languageCode => $name)
        {
            $languageId = $this->languageProvider->getIdByCode(new LanguageCode(new StringType($languageCode)));
            
            $this->queryBuilder->update('quantity_unit_description', ['unit_name' => $name],
                                        ['quantity_unit_id' => $quantityUnit->getId(), 'language_id' => $languageId]);
        }
        
        return $this;
    }
    
    
    /**
     * Deletes quantity unit entity data in database.
     *
     * @param \QuantityUnitInterface $quantityUnit Quantity unit to be delete.
     *
     * @return \QuantityUnitStorageInterface Same instance for chained method calls.
     */
    public function delete(QuantityUnitInterface $quantityUnit)
    {
        $this->queryBuilder->delete('quantity_unit', ['quantity_unit_id' => $quantityUnit->getId()]);
        $this->queryBuilder->delete('quantity_unit_description', ['quantity_unit_id' => $quantityUnit->getId()]);
        
        return $this;
    }
}