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 
<?php
/* --------------------------------------------------------------
   CustomerWriter.inc.php 2016-03-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('CustomerWriterInterface');

/**
 * Class CustomerWriter
 *
 * This class is used for writing customer data to the database
 *
 * @category   System
 * @package    Customer
 * @implements CustomerWriterInterface
 */
class CustomerWriter implements CustomerWriterInterface
{
    /**
     * Query builder.
     * @var CI_DB_query_builder
     */
    protected $db;
    
    
    /**
     * Constructor of the class CustomerWriter.
     *
     * @param CI_DB_query_builder $dbQueryBuilder Query builder.
     */
    public function __construct(CI_DB_query_builder $dbQueryBuilder)
    {
        $this->db = $dbQueryBuilder;
    }


    /**
     * Writes customer data.
     *
     * If customer does not exists it will perform an _insert(), otherwise it will perform an _update().
     *
     * @param CustomerInterface $customer Customer.
     */
    public function write(CustomerInterface $customer)
    {
        if($customer->getId() == null)
        {
            $this->_insert($customer);
        }
        else
        {
            $this->_update($customer);
        }
    }
    

    /**
     * Helper method to insert customer data into the database.
     *
     * @param CustomerInterface $customer Customer.
     *                                    
     * @throws InvalidArgumentException If CIDB_query_builder::insert_id does not return an integer.
     */
    protected function _insert(CustomerInterface $customer)
    {
        // Insert customer record.
        $customerDataArray = array(
            'account_type'                 => (int)$customer->isGuest(),
            'customers_status'             => (int)$customer->getStatusId(),
            'customers_cid'                => (string)$customer->getCustomerNumber(),
            'customers_gender'             => (string)$customer->getGender(),
            'customers_firstname'          => (string)$customer->getFirstname(),
            'customers_lastname'           => (string)$customer->getLastname(),
            'customers_email_address'      => (string)$customer->getEmail(),
            'customers_password'           => (string)$customer->getPassword(),
            'customers_vat_id'             => (string)$customer->getVatNumber(),
            'customers_vat_id_status'      => (string)$customer->getVatNumberStatus(),
            'customers_dob'                => (string)$customer->getDateOfBirth()->format('Y-m-d'),
            'customers_telephone'          => (string)$customer->getTelephoneNumber(),
            'customers_fax'                => (string)$customer->getFaxNumber(),
            'customers_default_address_id' => (int)(string)$customer->getDefaultAddress()->getId(),
            'customers_date_added'         => date('Y-m-d H:i:s')
        );
        $this->db->insert('customers', $customerDataArray);
        $customer->setId(new IdType($this->db->insert_id()));
        
        // Insert customer info record. 
        $customerInfoDataArray = array(
            'customers_info_id'                         => (string)$customer->getId(),
            'customers_info_date_of_last_logon'         => '1000-01-01 00:00:00',
            'customers_info_number_of_logons'           => '0',
            'customers_info_date_account_created'       => date('Y-m-d H:i:s')
        );
        $this->db->insert('customers_info', $customerInfoDataArray);
    }
    

    /**
     * Helper method to update customer data in the database.
     *
     * @param CustomerInterface $customer Customer.
     *
     * TODO Use wrapper function getDefaultAddressId() instead of getDefaultAddress()->getId()
     */
    protected function _update(CustomerInterface $customer)
    {
        $customerId = $customer->getId();
        
        // Update customer record. 
        $customerDataArray = array(
            'account_type'                 => (int)$customer->isGuest(),
            'customers_status'             => (int)$customer->getStatusId(),
            'customers_cid'                => (string)$customer->getCustomerNumber(),
            'customers_gender'             => (string)$customer->getGender(),
            'customers_firstname'          => (string)$customer->getFirstname(),
            'customers_lastname'           => (string)$customer->getLastname(),
            'customers_email_address'      => (string)$customer->getEmail(),
            'customers_password'           => (string)$customer->getPassword(),
            'customers_vat_id'             => (string)$customer->getVatNumber(),
            'customers_vat_id_status'      => (string)$customer->getVatNumberStatus(),
            'customers_dob'                => (string)$customer->getDateOfBirth()->format('Y-m-d'),
            'customers_telephone'          => (string)$customer->getTelephoneNumber(),
            'customers_fax'                => (string)$customer->getFaxNumber(),
            'customers_default_address_id' => (string)$customer->getDefaultAddress()->getId()
        );

        $this->db->update('customers', $customerDataArray, array('customers_id' => $customerId));
        
        // Update customer info record.
        $customerInfoDataArray = array(
            'customers_info_date_account_last_modified' => 'now()'
        );
        $this->db->update('customers_info', $customerInfoDataArray, array('customers_info_id' => $customerId));
    }
}