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

/**
 * Class AgreementAccessRepository
 *
 * @category   System
 * @package    Agreement
 * @subpackage Repositories
 */
class AgreementAccessRepository implements AgreementAccessRepositoryInterface
{
    /**
     * @var \AgreementFactory
     */
    protected $factory;
    
    /**
     * @var \AgreementReaderInterface
     */
    protected $reader;
    
    
    /**
     * ReviewAccessRepository constructor.
     *
     * @param \AgreementFactory         $factory
     * @param \AgreementReaderInterface $reader
     */
    public function __construct(AgreementFactory $factory, AgreementReaderInterface $reader)
    {
        $this->factory = $factory;
        $this->reader  = $reader;
    }
    
    
    /**
     * Returns all agreement entities as an array.
     *
     * @return AgreementCollection
     */
    public function getAll()
    {
        $collection = $this->factory->createCollection();
        
        $rawData = $this->reader->getAll();
        foreach($rawData as $data)
        {
            $agreement = $this->factory->create();
            $agreement->setId(new IdType($data['id']))
                      ->setDateAdded(new DateTime($data['dateAdded']))
                      ->setLastModifiedDate(new DateTime($data['lastModified']))
                      ->setIpAddress(new StringType($data['ipAddress']))
                      ->setText(new NonEmptyStringType($data['text']))
                      ->setLegalTextVersion(new StringType($data['legalTextVersion']))
                      ->setContentGroup(new IdType($data['contentGroup']))
                      ->setCustomer($this->_createCustomer($data['customer']))
                      ->setLanguageId(new IdType($data['languageId']));
            
            $collection->addItem($agreement);
        }
        
        return $collection;
    }
    
    
    /**
     * Returns an agreement entity by the provided id.
     *
     * @param \IdType $id
     *
     * @return Agreement
     */
    public function getById(IdType $id)
    {
        $rawData = $this->reader->getById($id);
        
        $agreement = $this->factory->create();
        
        $agreement->setId(new IdType($rawData['id']))
                  ->setDateAdded(new DateTime($rawData['dateAdded']))
                  ->setLastModifiedDate(new DateTime($rawData['lastModified']))
                  ->setIpAddress(new StringType($rawData['ipAddress']))
                  ->setText(new NonEmptyStringType($rawData['text']))
                  ->setLegalTextVersion(new StringType($rawData['legalTextVersion']))
                  ->setContentGroup(new IdType($rawData['contentGroup']))
                  ->setCustomer($this->_createCustomer($rawData['customer']))
                  ->setLanguageId(new IdType($rawData['languageId']));
        
        return $agreement;
    }
    
    
    /**
     * Returns the agreements by the provided customer email address.
     *
     * @param \StringType $customerEmail
     *
     * @return AgreementCollection
     */
    public function getAgreementsByCustomerEmail(StringType $customerEmail)
    {
        $collection = $this->factory->createCollection();
        
        $rawData = $this->reader->getAgreementsByCustomerEmail($customerEmail);
        foreach($rawData as $data)
        {
            $agreement = $this->factory->create();
            $agreement->setId(new IdType($data['id']))
                      ->setDateAdded(new DateTime($data['dateAdded']))
                      ->setLastModifiedDate(new DateTime($data['lastModified']))
                      ->setIpAddress(new StringType($data['ipAddress']))
                      ->setText(new NonEmptyStringType($data['text']))
                      ->setLegalTextVersion(new StringType($data['legalTextVersion']))
                      ->setContentGroup(new IdType($data['contentGroup']))
                      ->setCustomer($this->_createCustomer($data['customer']))
                      ->setLanguageId(new IdType($data['languageId']));
            
            $collection->addItem($agreement);
        }
        
        return $collection;
    }
    
    
    /**
     * Creates a AgreementCustomer by the provided array.
     *
     * @param array $dataSet
     *
     * @return \AgreementCustomer
     */
    private function _createCustomer(array $dataSet)
    {
        return $this->factory->createCustomer(new StringType($dataSet['customerName']),
                                              new AgreementCustomerEmail($dataSet['customerEmail']));
    }
}