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

/* --------------------------------------------------------------
   OrderStatusRepository.inc.php 2017-03-30
   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 OrderStatusRepository
 *
 * @category   System
 * @package    OrderStatus
 * @subpackage Repositories
 */
class OrderStatusRepository implements OrderStatusRepositoryInterface
{
    /**
     * @var OrderStatusFactoryInterface
     */
    protected $factory;
    
    /**
     * @var OrderStatusReaderInterface
     */
    protected $reader;
    
    /**
     * @var OrderStatusWriterInterface
     */
    protected $writer;
    
    /**
     * @var \OrderStatusDeleterInterface
     */
    protected $deleter;
    
    /**
     * @var \LanguageProvider
     */
    protected $languageProvider;
    
    
    /**
     * OrderStatusRepository constructor.
     *
     * @param \OrderStatusFactoryInterface $factory          Factory instance to create order status entities.
     * @param \OrderStatusReaderInterface  $reader           Reader instance to fetch data from the storage.
     * @param \OrderStatusWriterInterface  $writer           Writer instance to add or update data in the storage.
     * @param \OrderStatusDeleterInterface $deleter          Deleter instance to remove data from the storage.
     * @param \LanguageProvider            $languageProvider Provider instance to get language codes from language ids.
     */
    public function __construct(OrderStatusFactoryInterface $factory,
                                OrderStatusReaderInterface $reader,
                                OrderStatusWriterInterface $writer,
                                OrderStatusDeleterInterface $deleter,
                                LanguageProvider $languageProvider)
    {
        $this->factory          = $factory;
        $this->reader           = $reader;
        $this->writer           = $writer;
        $this->deleter          = $deleter;
        $this->languageProvider = $languageProvider;
    }
    
    
    /**
     * Returns a collection with all order status.
     *
     * @return OrderStatusCollection Collection of all order status resources.
     */
    public function findAll()
    {
        $orderStatusData = $this->reader->getAllOrderStatus();
        $orderStatuses   = [];
        
        foreach($orderStatusData as $data)
        {
            $orderStatus = $this->factory->createOrderStatus(new IntType($data['id']));
            foreach($data['names'] as $languageId => $name)
            {
                $languageCode = $this->languageProvider->getCodeById(new IdType($languageId));
                $orderStatus->setName($languageCode, new StringType($name));
            }
            
            $orderStatus->setColor(new StringType($data['color']));
            $orderStatuses[] = $orderStatus;
        }
        
        return $this->factory->createOrderStatusCollection($orderStatuses);
    }
    
    
    /**
     * Returns an order status entity by the given order status id. If no data was found, null is returned.
     *
     * @param \IntType $orderStatusId Id of expected OrderStatus entity.
     *
     * @return OrderStatus|null Whether the found order status entity or null.
     */
    public function find(IntType $orderStatusId)
    {
        $data = $this->reader->getOrderStatusById($orderStatusId);
        if(count($data) === 0)
        {
            return null;
        }
        
        $orderStatus = $this->factory->createOrderStatus($orderStatusId);
        foreach($data['names'] as $languageId => $name)
        {
            $orderStatus->setName($this->languageProvider->getCodeById(new IdType($languageId)), new StringType($name));
        }
        $orderStatus->setColor(new StringType($data['color']));
        
        return $orderStatus;
    }
    
    
    /**
     * Returns an order status entity by the given order status id. If no data was found, an exception is thrown.
     *
     * @param \IntType $orderStatusId Id of expected OrderStatus entity.
     *
     * @throws OrderStatusNotFoundException If expected order status entity was not found by the given id.
     * @return OrderStatus Expected order status entity.
     */
    public function get(IntType $orderStatusId)
    {
        $orderStatus = $this->find($orderStatusId);
        
        if(null === $orderStatus)
        {
            throw new OrderStatusNotFoundException('Order status with id "' . $orderStatusId->asInt() . '" not found.');
        }
        
        return $orderStatus;
    }
    
    
    /**
     * Creates a new order status.
     *
     * @param \OrderStatus $orderStatus Order status entity with new data.
     *
     * @return int Id of new order status entity.
     */
    public function create(OrderStatus $orderStatus)
    {
        return $this->writer->createOrderStatus($orderStatus);
    }
    
    
    /**
     * Updates an order status.
     *
     * @param \OrderStatus $orderStatus Order status entity with updated data.
     *
     * @return $this|OrderStatusRepositoryInterface Same instance for chained method calls.
     */
    public function update(OrderStatus $orderStatus)
    {
        $this->writer->updateOrderStatus($orderStatus);
        
        return $this;
    }
    
    
    /**
     * Removes an order status.
     *
     * @param \IntType $orderStatusId Id of order status entity to be removed.
     *
     * @return $this|OrderStatusRepositoryInterface Same instance for chained method calls.
     */
    public function remove(IntType $orderStatusId)
    {
        $this->deleter->removeOrderStatus($orderStatusId);
        
        return $this;
    }
}