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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 
<?php
/* --------------------------------------------------------------
   CustomerServiceFactory.inc.php 2018-06-15
   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('AbstractCustomerServiceFactory');

/**
 * Class CustomerServiceFactory
 *
 * Factory class for all needed customer data.
 *
 * @category System
 * @package  Customer
 * @extends  AbstractCustomerServiceFactory
 */
class CustomerServiceFactory extends AbstractCustomerServiceFactory
{
    /**
     * Query builder.
     * @var CI_DB_query_builder
     */
    protected $ciDatabaseQueryBuilder;
    
    /**
     * @var \CustomerConfigurationProvider
     */
    protected $configurationProvider;
    
    
    /**
     * CustomerServiceFactory constructor.
     *
     * @param CI_DB_query_builder $ciDatabaseQueryBuilder Query builder.
     */
    public function __construct(CI_DB_query_builder $ciDatabaseQueryBuilder)
    {
        $this->ciDatabaseQueryBuilder = $ciDatabaseQueryBuilder;
    }
    
    
    /**
     * Returns the country service.
     *
     * @return CountryService Country service.
     */
    public function getCountryService()
    {
        $customerCountryRepo     = $this->_getCustomerCountryRepository();
        $customerCountryZoneRepo = $this->_getCustomerCountryZoneRepository();
        
        $countryService = MainFactory::create('CountryService', $customerCountryRepo, $customerCountryZoneRepo);
        
        return $countryService;
    }
    
    
    /**
     * Returns the customer service.
     *
     * @return CustomerService Customer service.
     */
    public function getCustomerService()
    {
        $customerReadService  = $this->createCustomerReadService();
        $customerWriteService = $this->createCustomerWriteService();
        
        $customerService = MainFactory::create('CustomerService', $customerReadService, $customerWriteService);
        
        return $customerService;
    }
    
    
    /**
     * Creates a customer read service object.
     *
     * @return CustomerReadService Customer read service.
     */
    public function createCustomerReadService()
    {
        $customerRepository = $this->_getCustomerRepository();
        
        $customerReadService = MainFactory::create('CustomerReadService', $customerRepository);
        
        return $customerReadService;
    }
    
    
    /**
     * Creates a customer service object.
     *
     * @return CustomerService Customer service.
     */
    public function createCustomerWriteService()
    {
        $addressBookService      = $this->getAddressBookService();
        $customerRepository      = $this->_getCustomerRepository();
        $customerServiceSettings = $this->_getCustomerServiceSettings();
        $vatValidator            = MainFactory::create('VatNumberValidator');
        
        $customerWriteService = MainFactory::create('CustomerWriteService', $addressBookService, $customerRepository,
                                                    $customerServiceSettings, $vatValidator);
        
        return $customerWriteService;
    }
    
    
    /**
     * Returns the address book service.
     *
     * @return AddressBookService Address book service.
     */
    public function getAddressBookService()
    {
        $addressRepository  = $this->_getCustomerAddressRepository();
        $addressBookService = MainFactory::create('AddressBookService', $addressRepository);
        
        return $addressBookService;
    }
    
    
    /**
     * Returns the customer registration input validator service.
     *
     * @return CustomerRegistrationInputValidatorService Customer registration input validator service.
     */
    public function getCustomerRegistrationInputValidatorService()
    {
        return $this->_getCustomerInputValidatorServiceByValidatorName('CustomerRegistrationInputValidatorService');
    }
    
    
    /**
     * Returns the customer account input validator.
     *
     * @return CustomerAccountInputValidator Customer account input validator.
     */
    public function getCustomerAccountInputValidator()
    {
        return $this->_getCustomerInputValidatorServiceByValidatorName('CustomerAccountInputValidator');
    }
    
    
    /**
     * Returns the database query builder.
     *
     * @return CI_DB_query_builder Query builder.
     */
    public function getDatabaseQueryBuilder()
    {
        return $this->ciDatabaseQueryBuilder;
    }
    
    
    /**
     * Returns the customer factory.
     *
     * @return CustomerFactory Customer factory.
     *
     * TODO Inject CustomerFactory
     */
    protected function _getCustomerFactory()
    {
        $customerFactory = MainFactory::create('CustomerFactory');
        
        return $customerFactory;
    }
    
    
    /**
     * Creates a customer repository object.
     *
     * @return CustomerRepository Customer repository.
     */
    protected function _getCustomerRepository()
    {
        $customerWriter    = $this->_getCustomerWriter();
        $customerReader    = $this->_getCustomerReader();
        $customerDeleter   = $this->_getCustomerDeleter();
        $addressRepository = $this->_getCustomerAddressRepository();
        $customerFactory   = $this->_getCustomerFactory();
        $addonValueService = $this->_getAddonValueService();
        
        $repository = MainFactory::create('CustomerRepository', $customerWriter, $customerReader, $customerDeleter,
                                          $addressRepository, $customerFactory, $addonValueService);
        
        return $repository;
    }
    
    
    /**
     * Returns the customer input validator.
     *
     * @return CustomerAddressInputValidator Customer input validator.
     */
    public function getCustomerAddressInputValidatorService()
    {
        return $this->_getCustomerInputValidatorServiceByValidatorName('CustomerAddressInputValidator');
    }
    
    
    /**
     * Creates a customer country repository object.
     *
     * @return CustomerCountryRepository Customer country repository.
     */
    protected function _getCustomerCountryRepository()
    {
        $reader = $this->_getCustomerCountryReader();
        $repo   = MainFactory::create('CustomerCountryRepository', $reader);
        
        return $repo;
    }
    
    
    /**
     * Creates a customer country zone repository object.
     *
     * @return CustomerCountryZoneRepository Customer country zone repository.
     */
    protected function _getCustomerCountryZoneRepository()
    {
        $reader          = $this->_getCustomerCountryZoneReader();
        $customerFactory = $this->_getCustomerFactory();
        $repo            = MainFactory::create('CustomerCountryZoneRepository', $reader, $customerFactory);
        
        return $repo;
    }
    
    
    /**
     * Creates a customer address repository object.
     *
     * @return CustomerAddressRepository Customer address repository.
     */
    protected function _getCustomerAddressRepository()
    {
        $writer     = $this->_getCustomerAddressWriter();
        $reader     = $this->_getCustomerAddressReader();
        $deleter    = $this->_getCustomerAddressDeleter();
        $factory    = $this->_getCustomerFactory();
        $repository = MainFactory::create('CustomerAddressRepository', $writer, $deleter, $reader, $factory);
        
        return $repository;
    }
    
    
    /**
     * Returns customer input validator service by validator name.
     *
     * @param string $inputValidatorName Name of input validator service.
     *
     * @return object Found customer input validator service.
     */
    protected function _getCustomerInputValidatorServiceByValidatorName($inputValidatorName)
    {
        $customerService = $this->getCustomerService();
        $countryService  = $this->getCountryService();
        
        $settings = $this->_getInputValidatorSettings();
        
        $countryRepo        = $this->_getCustomerCountryRepository();
        $countryZoneRepo    = $this->_getCustomerCountryZoneRepository();
        $vatNumberValidator = MainFactory::create('VatNumberValidator');
        
        return MainFactory::create($inputValidatorName, $customerService, $countryService, $settings, $countryRepo,
                                   $countryZoneRepo, $vatNumberValidator);
    }
    
    
    /**
     * Creates the input validator settings.
     *
     * @return CustomerInputValidatorSettings
     */
    protected function _getInputValidatorSettings()
    {
        $configurationProvider = $this->_getConfigurationProvider();
        
        $configurationSettings     = $this->_getValidatorConfigSettings($configurationProvider);
        $displaySettings           = $this->_getValidatorDisplaySettings($configurationProvider);
        $customerMinLengthSettings = $this->_getValidatorMinLengthSettings($configurationProvider);
        $maxLengthSettings         = $this->_getValidatorMaxLengthSettings();
        $errorMessages             = $this->_getValidatorErrorMessageSettings($configurationProvider);
        
        return MainFactory::create('CustomerInputValidatorSettings', $configurationSettings, $displaySettings,
                                   $customerMinLengthSettings, $maxLengthSettings, $errorMessages);
    }
    
    
    /**
     * Creates the configuration settings of the input validator.
     *
     * @param \CustomerConfigurationProvider $configProvider Component that fetches data from configuration tables.
     *
     * @return \CustomerConfigurationSettings
     */
    protected function _getValidatorConfigSettings(CustomerConfigurationProvider $configProvider)
    {
        $optionalNames          = $configProvider->configuration($this->_string('optionalNames'));
        $acceptPrivacy          = $configProvider->configuration($this->_string('acceptPrivacy'));
        $splitStreetInformation = $configProvider->configuration($this->_string('splitStreetInformation'));
        $genderMandatory        = $configProvider->configuration($this->_string('genderMandatory'));
        
        return MainFactory::create('CustomerConfigurationSettings', $this->_bool($optionalNames),
                                   $this->_bool($acceptPrivacy), $this->_bool($splitStreetInformation), $this->_bool($genderMandatory));
    }
    
    
    /**
     * Creates the display settings of the input validator.
     *
     * @param \CustomerConfigurationProvider $configProvider Component that fetches data from configuration tables.
     *
     * @return \CustomerDisplaySettings
     */
    protected function _getValidatorDisplaySettings(CustomerConfigurationProvider $configProvider)
    {
        $gender      = $this->_bool($configProvider->display($this->_string('gender')));
        $dob         = $this->_bool($configProvider->display($this->_string('dob')));
        $company     = $this->_bool($configProvider->display($this->_string('company')));
        $countryZone = $this->_bool($configProvider->display($this->_string('countryZone')));
        $telephone   = $this->_bool($configProvider->display($this->_string('telephone')));
        $fax         = $this->_bool($configProvider->display($this->_string('fax')));
        $suburb      = $this->_bool($configProvider->display($this->_string('suburb')));
        
        return MainFactory::create('CustomerDisplaySettings', $gender, $dob, $company, $countryZone, $telephone, $fax,
                                   $suburb);
    }
    
    
    /**
     * Creates the min length settings of the input validator.
     *
     * @param \CustomerConfigurationProvider $configProvider Component that fetches data from configuration tables.
     *
     * @return \CustomerMinLengthSettings
     */
    protected function _getValidatorMinLengthSettings(CustomerConfigurationProvider $configProvider)
    {
        $firstName       = $this->_int($configProvider->minLength($this->_string('firstName')));
        $lastName        = $this->_int($configProvider->minLength($this->_string('lastName')));
        $dob             = $this->_int($configProvider->minLength($this->_string('dob')));
        $email           = $this->_int($configProvider->minLength($this->_string('email')));
        $street          = $this->_int($configProvider->minLength($this->_string('street')));
        $houseNumber     = $this->_int($configProvider->minLength($this->_string('houseNumber')));
        $company         = $this->_int($configProvider->minLength($this->_string('company')));
        $postCode        = $this->_int($configProvider->minLength($this->_string('postcode')));
        $city            = $this->_int($configProvider->minLength($this->_string('city')));
        $countryZone     = $this->_int($configProvider->minLength($this->_string('countryZone')));
        $telephoneNumber = $this->_int($configProvider->minLength($this->_string('telephone')));
        $password        = $this->_int($configProvider->minLength($this->_string('password')));
        
        return MainFactory::create('CustomerMinLengthSettings', $firstName, $lastName, $dob, $email, $street,
                                   $houseNumber, $company, $postCode, $city, $countryZone, $telephoneNumber, $password);
    }
    
    
    /**
     * Creates the max length settings of the input validator.
     * The max length represent the database field max length.
     *
     * @return \CustomerMaxLengthSettings
     */
    protected function _getValidatorMaxLengthSettings()
    {
        // database fields max length
        $firstName   = $this->_int(64);
        $lastName    = $this->_int(64);
        $company     = $this->_int(255);
        $vatNumber   = $this->_int(20);
        $street      = $this->_int(64);
        $houseNumber = $this->_int(10);
        $postcode    = $this->_int(10);
        $city        = $this->_int(32);
        $countryZone = $this->_int(32);
        $suburb      = $this->_int(32);
        $telephone   = $this->_int(32);
        
        return MainFactory::create('CustomerMaxLengthSettings', $firstName, $lastName, $company, $vatNumber, $street,
                                   $houseNumber, $postcode, $city, $countryZone, $suburb, $telephone);
    }
    
    
    /**
     * Creates the customer validator error messages.
     *
     * @param \CustomerConfigurationProvider $configProvider Component that fetches data from configuration tables.
     *
     * @return \CustomerErrorMessageSettings
     */
    protected function _getValidatorErrorMessageSettings(CustomerConfigurationProvider $configProvider)
    {
        $gender               = $this->_string($configProvider->errorMessage($this->_string('gender')));
        $firstName            = $this->_string($configProvider->errorMessage($this->_string('firstName')));
        $lastName             = $this->_string($configProvider->errorMessage($this->_string('lastName')));
        $dob                  = $this->_string($configProvider->errorMessage($this->_string('dob')));
        $company              = $this->_string($configProvider->errorMessage($this->_string('company')));
        $vatNumber            = $this->_string($configProvider->errorMessage($this->_string('vatNumber')));
        $email                = $this->_string($configProvider->errorMessage($this->_string('email')));
        $emailAddressCheck    = $this->_string($configProvider->errorMessage($this->_string('emailAddressCheck')));
        $emailConfirmation    = $this->_string($configProvider->errorMessage($this->_string('emailConfirmation')));
        $emailExist           = $this->_string($configProvider->errorMessage($this->_string('emailExists')));
        $street               = $this->_string($configProvider->errorMessage($this->_string('street')));
        $houseNumber          = $this->_string($configProvider->errorMessage($this->_string('houseNumber')));
        $postcode             = $this->_string($configProvider->errorMessage($this->_string('postcode')));
        $city                 = $this->_string($configProvider->errorMessage($this->_string('city')));
        $country              = $this->_string($configProvider->errorMessage($this->_string('country')));
        $countryZone          = $this->_string($configProvider->errorMessage($this->_string('countryZone')));
        $countryZoneSelection = $this->_string($configProvider->errorMessage($this->_string('countryZoneSelection')));
        $telephone            = $this->_string($configProvider->errorMessage($this->_string('telephone')));
        $password             = $this->_string($configProvider->errorMessage($this->_string('password')));
        $passwordMismatch     = $this->_string($configProvider->errorMessage($this->_string('passwordMismatch')));
        $privacy              = $this->_string($configProvider->errorMessage($this->_string('privacy')));
        $invalidInput         = $this->_string($configProvider->errorMessage($this->_string('invalidInput')));
        
        return MainFactory::create('CustomerErrorMessageSettings', $gender, $firstName, $lastName, $dob, $company,
                                   $vatNumber, $email, $emailAddressCheck, $emailConfirmation, $emailExist, $street,
                                   $houseNumber, $postcode, $city, $country, $countryZone, $countryZoneSelection,
                                   $telephone, $password, $passwordMismatch, $privacy, $invalidInput);
    }
    
    
    /**
     * Returns a new int type.
     *
     * @param int $int Value of representing type object.
     *
     * @return \IntType
     */
    protected function _int($int)
    {
        return new \IntType($int);
    }
    
    
    /**
     * Returns a new string type.
     *
     * @param string $str Value of representing type object.
     *
     * @return \StringType
     */
    protected function _string($str)
    {
        return new \StringType($str);
    }
    
    
    /**
     * Returns a new bool type.
     *
     * @param bool $bool Value of representing type object.
     *
     * @return \BoolType
     */
    protected function _bool($bool)
    {
        return new \BoolType($bool);
    }
    
    
    /**
     * Creates a customer address deleter object.
     *
     * @return CustomerAddressDeleter Customer address deleter.
     */
    protected function _getCustomerAddressDeleter()
    {
        $deleter = MainFactory::create('CustomerAddressDeleter', $this->getDatabaseQueryBuilder());
        
        return $deleter;
    }
    
    
    /**
     * Creates a customer address reader object.
     *
     * @return CustomerAddressReader Customer address reader.
     */
    protected function _getCustomerAddressReader()
    {
        $customerFactory = $this->_getCustomerFactory();
        $countryService  = $this->getCountryService();
        
        $reader = MainFactory::create('CustomerAddressReader', $customerFactory, $countryService,
                                      $this->getDatabaseQueryBuilder());
        
        return $reader;
    }
    
    
    /**
     * Creates a customer country zone reader object.
     *
     * @return CustomerCountryZoneReader Customer country zone reader.
     */
    protected function _getCustomerCountryZoneReader()
    {
        $customerFactory = $this->_getCustomerFactory();
        $reader          = MainFactory::create('CustomerCountryZoneReader', $customerFactory,
                                               $this->getDatabaseQueryBuilder());
        
        return $reader;
    }
    
    
    /**
     * Creates a customer country reader object
     *
     * @return CustomerCountryReader Customer country reader.
     */
    protected function _getCustomerCountryReader()
    {
        $customerFactory = $this->_getCustomerFactory();
        $reader          = MainFactory::create('CustomerCountryReader', $customerFactory,
                                               $this->getDatabaseQueryBuilder());
        
        return $reader;
    }
    
    
    /**
     * Creates a customer writer object.
     *
     * @return CustomerWriter Customer writer.
     */
    protected function _getCustomerWriter()
    {
        $customerWriter = MainFactory::create('CustomerWriter', $this->getDatabaseQueryBuilder());
        
        return $customerWriter;
    }
    
    
    /**
     * Creates a customer reader object.
     *
     * @return CustomerReader Customer reader.
     */
    protected function _getCustomerReader()
    {
        $customerFactory           = $this->_getCustomerFactory();
        $customerAddressRepository = $this->_getCustomerAddressRepository();
        $dbQueryBuilder            = $this->getDatabaseQueryBuilder();
        
        $customerReader = MainFactory::create('CustomerReader', $customerFactory, $customerAddressRepository,
                                              $dbQueryBuilder);
        
        return $customerReader;
    }
    
    
    /**
     * Creates a customer deleter object.
     *
     * @return CustomerDeleter Customer deleter.
     */
    protected function _getCustomerDeleter()
    {
        $customerDeleter = MainFactory::create('CustomerDeleter', $this->getDatabaseQueryBuilder());
        
        return $customerDeleter;
    }
    
    
    /**
     * Creates a customer service settings object.
     *
     * @return CustomerServiceSettings Customer service settings.
     */
    protected function _getCustomerServiceSettings()
    {
        $configProvider = $this->_getConfigurationProvider();
        
        $defaultCustomerStatusId = $configProvider->defaultStatusId($this->_string('customer'));
        $defaultGuestStatusId    = $configProvider->defaultStatusId($this->_string('guest'));
        $moveOnlyIfNoGuest       = $configProvider->configuration($this->_string('moveOnlyIfNoGuest'));
        
        return MainFactory::create('CustomerServiceSettings', new IdType($defaultCustomerStatusId),
                                   new IdType($defaultGuestStatusId), $this->_bool($moveOnlyIfNoGuest));
    }
    
    
    /**
     * Creates a customer address writer object.
     *
     * @return CustomerAddressWriter Customer address writer.
     */
    protected function _getCustomerAddressWriter()
    {
        $writer = MainFactory::create('CustomerAddressWriter', $this->getDatabaseQueryBuilder());
        
        return $writer;
    }
    
    
    protected function _getAddonValueService()
    {
        $addonValueStorageFactory = MainFactory::create('AddonValueStorageFactory', $this->getDatabaseQueryBuilder());
        $addonValueService        = MainFactory::create('AddonValueService', $addonValueStorageFactory);
        
        return $addonValueService;
    }
    
    
    protected function _getConfigurationProvider()
    {
        if(null === $this->configurationProvider)
        {
            $this->configurationProvider = new CustomerConfigurationProvider($this->getDatabaseQueryBuilder(),
                                                                             MainFactory::create('LanguageTextManager'));
        }
        
        return $this->configurationProvider;
    }
}