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 
<?php
/* --------------------------------------------------------------
   CustomerGroupAccessRepository.inc.php 2018-01-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 CustomerGroupAccessRepository
 *
 * @category   System
 * @package    CustomerGroup
 * @subpackage Repositories
 */
class CustomerGroupAccessRepository implements CustomerGroupAccessRepositoryInterface
{
    
    /**
     * @var \CustomerGroupFactory
     */
    private $factory;
    
    
    /**
     * @var \CustomerGroupReaderInterface
     */
    private $reader;
    
    
    /**
     * @var \LanguageProvider
     */
    private $languageProvider;
    
    
    /**
     * CustomerGroupAccessRepository constructor.
     *
     * @param \CustomerGroupFactory         $factory
     * @param \CustomerGroupReaderInterface $reader
     * @param \LanguageProvider             $languageProvider To convert language id's to language codes.
     */
    public function __construct(CustomerGroupFactory $factory,
                                CustomerGroupReaderInterface $reader,
                                LanguageProvider $languageProvider)
    {
        $this->factory          = $factory;
        $this->reader           = $reader;
        $this->languageProvider = $languageProvider;
    }
    
    
    /**
     * Returns all customer group as collection.
     *
     * @return \CustomerGroupCollection Customer group collection.
     * @throws \UnexpectedValueException
     * @throws \InvalidArgumentException
     */
    public function getAll()
    {
        $rawData    = $this->reader->getAll();
        $collection = $this->factory->createCollection();
        
        foreach($rawData as $dataSet)
        {
            $customerGroup = $this->factory->createEntity();
            
            $customerGroup->setId(new IntType($dataSet['id']))
                          ->setMembers(new IntType($dataSet['members']))
                          ->setDefault(new BoolType($dataSet['default']))
                          ->setSettings($this->_createSettings($dataSet['settings']))
                          ->setConfigurations($this->_createConfigurations($dataSet['configurations']));
            
            foreach($dataSet['names'] as $languageId => $customerGroupName)
            {
                $languageCode = $this->languageProvider->getCodeById(new IdType($languageId));
                $customerGroup->setName(new StringType($customerGroupName), $languageCode);
            }
            
            $collection->addItem($customerGroup);
        }
        
        return $collection;
    }
    
    
    /**
     * Returns customer group entity by given id.
     *
     * @param \IntType $id IdType of entity to be returned.
     *
     * @return \CustomerGroup Customer group collection.
     */
    public function getById(IntType $id)
    {
        $dataSet = $this->reader->getById($id);
        
        $customerGroup = $this->factory->createEntity();
        
        $customerGroup->setId($id)
                      ->setMembers(new IntType($dataSet['members']))
                      ->setDefault(new BoolType($dataSet['default']))
                      ->setSettings($this->_createSettings($dataSet['settings']))
                      ->setConfigurations($this->_createConfigurations($dataSet['configurations']));
        
        foreach($dataSet['names'] as $languageId => $customerGroupName)
        {
            $languageCode = $this->languageProvider->getCodeById(new IdType($languageId));
            $customerGroup->setName(new StringType($customerGroupName), $languageCode);
        }
        
        return $customerGroup;
    }
    
    
    /**
     * creates customer group entity.
     *
     * @return \CustomerGroup New customer group entity.
     */
    public function create()
    {
        return $this->factory->createEntity();
    }
    
    
    /**
     * Creates with given array an CustomerGroupSetting
     *
     * @param array $dataSet
     *
     * @return \CustomerGroupSettings
     */
    protected function _createSettings(array $dataSet)
    {
        $true  = new BoolType(true);
        $false = new BoolType(false);
        
        $public             = $dataSet['public'] ? $true : $false;
        $otDiscount         = $dataSet['otDiscount'] ? $true : $false;
        $graduatedPrices    = $dataSet['graduatedPrices'] ? $true : $false;
        $showPrice          = $dataSet['showPrice'] ? $true : $false;
        $showPriceTax       = $dataSet['showPriceTax'] ? $true : $false;
        $addTaxOt           = $dataSet['addTaxOt'] ? $true : $false;
        $discountAttributes = $dataSet['discountAttributes'] ? $true : $false;
        $fsk18              = $dataSet['fsk18'] ? $true : $false;
        $fsk18Display       = $dataSet['fsk18Display'] ? $true : $false;
        $writeReviews       = $dataSet['writeReviews'] ? $true : $false;
        $readReviews        = $dataSet['readReviews'] ? $true : $false;
        
        return $this->factory->createSettings($public, $otDiscount, $graduatedPrices, $showPrice, $showPriceTax,
                                              $addTaxOt, $discountAttributes, $fsk18, $fsk18Display, $writeReviews,
                                              $readReviews);
    }
    
    
    /**
     * Creates with given array an CustomerGroupConfiguration
     *
     * @param array $dataSet
     *
     * @return \CustomerGroupConfigurations
     */
    protected function _createConfigurations(array $dataSet)
    {
        return $this->factory->createConfigurations(new DecimalType($dataSet['discount']),
                                                    new DecimalType($dataSet['otDiscount']),
                                                    $dataSet['minOrder'] ? new DecimalType($dataSet['minOrder']) : null,
                                                    $dataSet['maxOrder'] ? new DecimalType($dataSet['maxOrder']) : null,
                                                    $dataSet['unallowedPaymentModules'],
                                                    $dataSet['unallowedShippingModules']);
    }
}