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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 
<?php
/* --------------------------------------------------------------
   OrderServiceFactory.php 2015-12-22
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2015 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

MainFactory::load_class('AbstractOrderServiceFactory');

/**
 * Class OrderServiceFactory
 *
 * @category   System
 * @package    Order
 * @subpackage Factories
 */
class OrderServiceFactory extends AbstractOrderServiceFactory
{
    /**
     * Query builder.
     * @var CI_DB_query_builder
     */
    protected $db;
    
    /**
     * @var \PaymentTitleProvider
     */
    protected $paymentTitleProvider;
    
    /**
     * @var \ShippingTitleProvider
     */
    protected $shippingTitleProvider;
    
    /**
     * @var \OrderServiceSettings
     */
    protected $orderServiceSettings;
    
    
    /**
     * OrderServiceFactory constructor.
     *
     * @param CI_DB_query_builder $db Query builder.
     */
    public function __construct(CI_DB_query_builder $db)
    {
        $this->db = $db;
    }
    
    
    /**
     * Creates and returns an order write service object.
     *
     * @return OrderWriteService New order write service object.
     */
    public function createOrderWriteService()
    {
        $orderRepository                     = $this->_createOrderRepository();
        $orderItemRepository                 = $this->_createOrderItemRepository();
        $orderItemAttributeRepositoryFactory = $this->_createOrderItemAttributeRepositoryFactory();
        $orderTotalRepository                = $this->_createOrderTotalRepository();
        $orderStatusHistoryReader            = $this->_createOrderStatusHistoryStorage();
        $orderServiceSettings                = $this->_createOrderServiceSettings();
        
        return MainFactory::create(OrderWriteService::class, $orderRepository, $orderItemRepository,
                                   $orderItemAttributeRepositoryFactory, $orderTotalRepository,
                                   $orderStatusHistoryReader, $orderServiceSettings);
    }
    
    
    /**
     * Creates and returns an order read service object.
     *
     * @return OrderReadService New order read service object.
     */
    public function createOrderReadService()
    {
        $orderRepository     = $this->_createOrderRepository();
        $orderItemRepository = $this->_createOrderItemRepository();
        $orderListGenerator  = $this->_createOrderListGenerator();
        
        return MainFactory::create(OrderReadService::class, $orderRepository, $orderItemRepository,
                                   $orderListGenerator);
    }
    
    
    /**
     * Creates and returns an order object service.
     *
     * @return OrderObjectService New order object service.
     */
    public function createOrderObjectService()
    {
        $orderItemFactory          = MainFactory::create(OrderItemFactory::class);
        $orderItemAttributeFactory = MainFactory::create(OrderItemAttributeFactory::class);
        $orderItemPropertyFactory  = MainFactory::create(OrderItemPropertyFactory::class);
        $orderTotalFactory         = MainFactory::create(OrderTotalFactory::class);
        
        return MainFactory::create(OrderObjectService::class, $orderItemFactory, $orderItemAttributeFactory,
                                   $orderItemPropertyFactory, $orderTotalFactory);
    }
    
    
    /**
     * Creates and returns an order repository.
     *
     * @return OrderRepository New order repository.
     */
    protected function _createOrderRepository()
    {
        $orderFactory             = MainFactory::create(OrderFactory::class);
        $countryService           = StaticGXCoreLoader::getService('Country');
        $orderRepositoryReader    = MainFactory::create(OrderRepositoryReader::class, $this->db, $orderFactory,
                                                        $countryService, $this->_createPaymentTitleProvider());
        $orderRepositoryWriter    = MainFactory::create(OrderRepositoryWriter::class, $this->db);
        $orderRepositoryDeleter   = MainFactory::create(OrderRepositoryDeleter::class, $this->db);
        $orderItemRepository      = $this->_createOrderItemRepository();
        $orderTotalRepository     = $this->_createOrderTotalRepository();
        $orderStatusHistoryReader = $this->_createOrderStatusHistoryStorage();
        $addonValueService        = MainFactory::create(AddonValueService::class,
                                                        MainFactory::create(AddonValueStorageFactory::class,
                                                                            $this->db));
        
        return MainFactory::create(OrderRepository::class, $orderFactory, $orderRepositoryWriter,
                                   $orderRepositoryReader, $orderRepositoryDeleter, $orderItemRepository,
                                   $orderTotalRepository, $orderStatusHistoryReader, $addonValueService);
    }
    
    
    /**
     * Creates and returns an order item repository.
     *
     * @return OrderItemRepository New order item repository.
     */
    protected function _createOrderItemRepository()
    {
        $orderItemFactory                    = MainFactory::create(OrderItemFactory::class);
        $orderItemRepositoryReader           = MainFactory::create(OrderItemRepositoryReader::class, $this->db,
                                                                   $orderItemFactory);
        $orderItemRepositoryWriter           = MainFactory::create(OrderItemRepositoryWriter::class, $this->db);
        $orderItemRepositoryDeleter          = MainFactory::create(OrderItemRepositoryDeleter::class, $this->db);
        $orderItemAttributeRepositoryFactory = $this->_createOrderItemAttributeRepositoryFactory();
        $addonValueService                   = MainFactory::create(AddonValueService::class,
                                                                   MainFactory::create(AddonValueStorageFactory::class,
                                                                                       $this->db));
        
        return MainFactory::create('OrderItemRepository', $orderItemAttributeRepositoryFactory,
                                   $orderItemRepositoryReader, $orderItemRepositoryWriter, $orderItemRepositoryDeleter,
                                   $addonValueService);
    }
    
    
    /**
     * Creates and returns and order item attribute repository factory.
     *
     * @return OrderItemAttributeRepositoryFactory New order item attribute repository factory.
     */
    protected function _createOrderItemAttributeRepositoryFactory()
    {
        return MainFactory::create(OrderItemAttributeRepositoryFactory::class, $this->db);
    }
    
    
    /**
     * Creates and returns an order total repository.
     *
     * @return OrderTotalRepository New order total repository.
     */
    protected function _createOrderTotalRepository()
    {
        $orderTotalFactory           = MainFactory::create(OrderTotalFactory::class);
        $orderTotalRepositoryReader  = MainFactory::create(OrderTotalRepositoryReader::class, $this->db,
                                                           $orderTotalFactory);
        $orderTotalRepositoryWriter  = MainFactory::create(OrderTotalRepositoryWriter::class, $this->db);
        $orderTotalRepositoryDeleter = MainFactory::create(OrderTotalRepositoryDeleter::class, $this->db);
        
        return MainFactory::create(OrderTotalRepository::class, $orderTotalRepositoryReader,
                                   $orderTotalRepositoryWriter, $orderTotalRepositoryDeleter);
    }
    
    
    /**
     * Creates and returns a order status history storage.
     *
     * @return OrderStatusHistoryStorage New order status history storage.
     */
    protected function _createOrderStatusHistoryStorage()
    {
        return MainFactory::create(OrderStatusHistoryStorage::class, $this->db);
    }
    
    
    /**
     * Creates and returns an order list generator.
     *
     * @return OrderListGenerator New order list generator.
     */
    protected function _createOrderListGenerator()
    {
        return MainFactory::create(OrderListGenerator::class, $this->db, $this->_createPaymentTitleProvider(),
                                   $this->_createShippingTitleProvider());
    }
    
    
    /**
     * Creates a order service settings object
     *
     * @return OrderServiceSettings New order service settings object
     */
    protected function _createOrderServiceSettings()
    {
        if(null === $this->orderServiceSettings)
        {
            $configProvider = new OrderConfigurationProvider($this->db);
            
            $this->orderServiceSettings = new OrderServiceSettings($this->_int($configProvider->defaultOrderStatusId()),
                                                                   $this->_int($configProvider->defaultCustomerStatusId()),
                                                                   $this->_int($configProvider->defaultGuestStatusId()));
        }
        
        return $this->orderServiceSettings;
    }
    
    
    /**
     * Creates, in memory caches and returns the payment title provider.
     *
     * @return \PaymentTitleProvider
     */
    protected function _createPaymentTitleProvider()
    {
        if(null === $this->paymentTitleProvider)
        {
            $this->paymentTitleProvider = MainFactory::create(PaymentTitleProvider::class);
        }
        
        return $this->paymentTitleProvider;
    }
    
    
    /**
     * Creates, in memory caches and returns the shipping title provider.
     *
     * @return \ShippingTitleProvider
     */
    protected function _createShippingTitleProvider()
    {
        if(null === $this->shippingTitleProvider)
        {
            $this->shippingTitleProvider = MainFactory::create(ShippingTitleProvider::class);
        }
        
        return $this->shippingTitleProvider;
    }
    
    
    /**
     * Returns a new integer type.
     *
     * @param string $int Representing value,
     *
     * @return \IntType
     */
    protected function _int($int)
    {
        return new \IntType($int);
    }
}