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 
<?php

/* --------------------------------------------------------------
  CustomerGroupServiceFactory.inc.php 2017-09-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 CustomerGroupServiceFactory
 * *
 * @category   System
 * @package    CustomerGroup
 * @subpackage Factories
 */
class CustomerGroupServiceFactory
{
    /**
     * @var \CI_DB_query_builder
     */
    protected $queryBuilder;
    
    /**
     * @var \CustomerGroupAccessRepository
     */
    protected $accessRepository;
    
    /**
     * @var \CustomerGroupWriteServiceInterface
     */
    protected $writeService;
    
    /**
     * @var \CustomerGroupReadServiceInterface
     */
    protected $readerService;
    
    /**
     * @var \CustomerGroupFactory
     */
    protected $factory;
    
    
    /**
     * CustomerGroupServiceFactory constructor.
     *
     * @param \CI_DB_query_builder $queryBuilder for database connection.
     */
    public function __construct(CI_DB_query_builder $queryBuilder)
    {
        $this->queryBuilder = $queryBuilder;
    }
    
    
    /**
     * Creates a customer group write service.
     *
     * @return  \CustomerGroupWriteServiceInterface
     */
    public function createWriteService()
    {
        $this->_createFactory()->_createWriter();
        
        return $this->writeService;
    }
    
    
    /**
     * Creates a customer group read service.
     *
     * @return  \CustomerGroupReadServiceInterface
     */
    public function createReadService()
    {
        $this->_createRepository()->_createReader();
        
        return $this->readerService;
    }
    
    
    public function createFactory()
    {
        $this->_createFactory();
        
        return $this->factory;
    }
    
    
    /**
     * Creates a new instance of a CustomerGroupRepository object.
     * Consecutive calls provide the same object.
     *
     * @return \CustomerGroupServiceFactory
     */
    protected function _createRepository()
    {
        if(null === $this->accessRepository)
        {
            $languageProvider = MainFactory::create('LanguageProvider', $this->queryBuilder);
            $writer           = MainFactory::create('CustomerGroupWriter', $this->queryBuilder, $languageProvider);
            $deleter          = MainFactory::create('CustomerGroupDeleter', $this->queryBuilder);
            $repository       = MainFactory::create('CustomerGroupRepository', $writer, $deleter);
            $reader           = MainFactory::create('CustomerGroupReader', $this->queryBuilder);
            $factory          = MainFactory::create('CustomerGroupFactory', $repository);
            
            $this->accessRepository = MainFactory::create('CustomerGroupAccessRepository', $factory, $reader,
                                                          $languageProvider);
        }
        
        return $this;
    }
    
    
    /**
     * Creates a new instance of a CustomerGroupReadService object.
     * Consecutive calls provide the same object.
     *
     * @return \CustomerGroupServiceFactory
     */
    protected function _createReader()
    {
        if(null === $this->readerService)
        {
            $this->readerService = MainFactory::create('CustomerGroupReadService', $this->accessRepository);
        }
        
        return $this;
    }
    
    
    /**
     * Creates a new instance of a CustomerGroupFactory object.
     * Consecutive call provide the same Object.
     *
     * @return $this
     */
    protected function _createFactory()
    {
        if(null === $this->factory)
        {
            $languageProvider = MainFactory::create('LanguageProvider', $this->queryBuilder);
            $writer           = MainFactory::create('CustomerGroupWriter', $this->queryBuilder, $languageProvider);
            $deleter          = MainFactory::create('CustomerGroupDeleter', $this->queryBuilder);
            $repository       = MainFactory::create('CustomerGroupRepository', $writer, $deleter);
            $this->factory    = MainFactory::create('CustomerGroupFactory', $repository);
        }
        
        return $this;
    }
    
    
    /**
     * Creates a new instance of a CustomerGroupWritService object.
     * Consecutive calls provide the same object.
     *
     * @return \CustomerGroupServiceFactory
     */
    protected function _createWriter()
    {
        if(null === $this->writeService)
        {
            $this->writeService = MainFactory::create('CustomerGroupWriteService',$this->factory);
        }
        
        return $this;
    }
}