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 
<?php
/* --------------------------------------------------------------
   CustomerAddressReader.inc.php 2016-06-21
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2016 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

MainFactory::load_class('CustomerAddressReaderInterface');

/**
 * Class CustomerAddressReader
 *
 * This class is used for reading customer address data from the database
 *
 * @category   System
 * @package    Customer
 * @subpackage Address
 * @implements CustomerAddressReaderInterface
 */
class CustomerAddressReader implements CustomerAddressReaderInterface
{
    /**
     * @var CI_DB_query_builder
     */
    protected $db;
    /**
     * @var AbstractCustomerFactory
     */
    protected $customerFactory;
    /**
     * @var CountryServiceInterface
     */
    protected $countryService;
    /**
     * @var StringHelperInterface
     */
    protected $stringHelper;


    /**
     * Constructor for the class CustomerAddressReader
     *
     * CrossCuttingLoader dependencies:
     * - StringHelper
     *
     * @param AbstractCustomerFactory $customerFactory
     * @param CountryServiceInterface  $countryService
     * @param CI_DB_query_builder      $dbQueryBuilder
     */
    public function __construct(AbstractCustomerFactory $customerFactory, CountryServiceInterface $countryService,
                                CI_DB_query_builder $dbQueryBuilder)
    {
        $this->customerFactory = $customerFactory;
        $this->countryService  = $countryService;
        $this->db              = $dbQueryBuilder;

        $this->stringHelper = StaticCrossCuttingLoader::getObject('StringHelper');
    }


    /**
     * @param IdType $id
     *
     * @return CustomerAddress
     * @throws InvalidArgumentException
     */
    public function getById(IdType $id)
    {
        $address = $this->findById($id);
        if($address === null)
        {
            throw new InvalidArgumentException('No Address found for the given ID.');
        }

        return $address;
    }


    /**
     * @param IdType $id
     * @return CustomerAddress
     */
    public function findById(IdType $id)
    {
        $addressDataResult = $this->db->get_where('address_book', array('address_book_id' => $id->asInt()));
        $addressDataArray = $addressDataResult->row_array();
        if($addressDataResult->num_rows() == 0)
        {
            return null;
        }

        return $this->_createCustomerAddressByArray($addressDataArray);
    }


    /**
     * This method will return an array of all customer's addresses
     *
     * @param CustomerInterface $customer
     *
     * @return array containing CustomerAddress objects
     */
    public function findAddressesByCustomer(CustomerInterface $customer)
    {
        $addressesArray = $this->db->get_where('address_book', array('customers_id' => $customer->getId()))
                                   ->result_array();

        foreach($addressesArray as &$address)
        {
            $address = $this->_createCustomerAddressByArray($address);
        }

        return $addressesArray;
    }


    /**
     * Get all system addresses.
     */
    public function getAllAddresses()
    {
        $addressesArray = $this->db->get('address_book')->result_array();

        foreach($addressesArray as &$address)
        {
            $address = $this->_createCustomerAddressByArray($address);
        }

        return $addressesArray;
    }


    /**
     * Filter existing addresses by keyword.
     *
     * This method is useful when creating a search mechanism for the registered addresses.
     *
     * @param string $p_keyword The keyword to be used for filtering the records.
     *
     * @return array Returns an array of CustomerAddress objects.
     */
    public function filterAddresses($p_keyword)
    {
        // CodeIgniter DB library will automatically escape the keyword.

        $this->db->like('entry_gender', $p_keyword);
        $this->db->or_like('entry_company', $p_keyword);
        $this->db->or_like('entry_firstname', $p_keyword);
        $this->db->or_like('entry_lastname', $p_keyword);
        $this->db->or_like('entry_street_address', $p_keyword);
        $this->db->or_like('entry_house_number', $p_keyword);
        $this->db->or_like('entry_additional_info', $p_keyword);
        $this->db->or_like('entry_suburb', $p_keyword);
        $this->db->or_like('entry_postcode', $p_keyword);
        $this->db->or_like('entry_city', $p_keyword);
        $this->db->or_like('entry_state', $p_keyword);

        $addressesArray = $this->db->get('address_book')->result_array();

        foreach($addressesArray as &$address)
        {
            $address = $this->_createCustomerAddressByArray($address);
        }

        return $addressesArray;
    }

    /**
     * @param array $addressDataArray
     *
     * @return CustomerAddress
     */
    protected function _createCustomerAddressByArray(array $addressDataArray)
    {
        $addressDataArray = $this->stringHelper->convertNullValuesToStringInArray($addressDataArray);

        $customerAddress = $this->customerFactory->createCustomerAddress();
        $customerAddress->setId(new IdType((int)$addressDataArray['address_book_id']));
        $customerAddress->setCustomerId(new IdType($addressDataArray['customers_id']));
        $customerAddress->setGender(MainFactory::create('CustomerGender', $addressDataArray['entry_gender']));
        $customerAddress->setCompany(MainFactory::create('CustomerCompany', $addressDataArray['entry_company']));
        $customerAddress->setB2BStatus(MainFactory::create('CustomerB2BStatus', (bool)(int)$addressDataArray['customer_b2b_status']));
        $customerAddress->setFirstname(MainFactory::create('CustomerFirstname', $addressDataArray['entry_firstname']));
        $customerAddress->setLastname(MainFactory::create('CustomerLastname', $addressDataArray['entry_lastname']));
        $customerAddress->setStreet(MainFactory::create('CustomerStreet', $addressDataArray['entry_street_address']));
        $customerAddress->setHouseNumber(MainFactory::create('CustomerHouseNumber', $addressDataArray['entry_house_number']));
        $customerAddress->setAdditionalAddressInfo(MainFactory::create('CustomerAdditionalAddressInfo', $addressDataArray['entry_additional_info']));
        $customerAddress->setSuburb(MainFactory::create('CustomerSuburb', $addressDataArray['entry_suburb']));
        $customerAddress->setPostcode(MainFactory::create('CustomerPostcode', $addressDataArray['entry_postcode']));
        $customerAddress->setCity(MainFactory::create('CustomerCity', $addressDataArray['entry_city']));
        $customerAddress->setAddressClass(MainFactory::create('AddressClass', $addressDataArray['address_class']));

        $country = $this->countryService->getCountryById(new IdType(
                                                                             $addressDataArray['entry_country_id']));
        $customerAddress->setCountry($country);

        $state = MainFactory::create('CustomerCountryZoneName', $addressDataArray['entry_state']);

        if($this->countryService->countryHasCountryZones($country) && (string)$state !== '')
        {
            $countryZone = $this->countryService->findCountryZoneByNameAndCountry($state, $country);
        }

        if(!isset($countryZone))
        {
            $countryZone = $this->customerFactory->createCustomerCountryZone(new IdType(0),
                                                                             $state,
                                                                             MainFactory::create('CustomerCountryZoneIsoCode', ''));
        }

        $customerAddress->setCountryZone($countryZone);

        return $customerAddress;
    }
}