Skip to content

Commit

Permalink
Merge branch 'master' of github.com:spryker/spryker into feature/core…
Browse files Browse the repository at this point in the history
…-922-calculator-cleanup
  • Loading branch information
Auris committed Jun 14, 2017
2 parents 33b9ed5 + d1478de commit 408c48c
Show file tree
Hide file tree
Showing 24 changed files with 589 additions and 15 deletions.
34 changes: 34 additions & 0 deletions src/Spryker/Client/Customer/CustomerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ public function getCustomerById($idCustomer)
return $customerTransfer;
}

/**
* {@inheritdoc}
*
* @api
*
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return \Generated\Shared\Transfer\CustomerTransfer|null
*/
public function findCustomerById(CustomerTransfer $customerTransfer)
{
$customerTransfer = $this->getCustomerById($customerTransfer->getIdCustomer());

if ($customerTransfer && $customerTransfer->getIdCustomer()) {
return $customerTransfer;
}

return null;
}

/**
* @api
*
Expand Down Expand Up @@ -387,4 +407,18 @@ public function setDefaultBillingAddress(AddressTransfer $addressTransfer)
->setDefaultBillingAddress($addressTransfer);
}

/**
* @api
*
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return \Generated\Shared\Transfer\CustomerTransfer
*/
public function anonymizeCustomer(CustomerTransfer $customerTransfer)
{
return $this->getFactory()
->createZedCustomerStub()
->anonymizeCustomer($customerTransfer);
}

}
21 changes: 21 additions & 0 deletions src/Spryker/Client/Customer/CustomerClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ public function getAddresses(CustomerTransfer $customerTransfer);
*/
public function getCustomerById($idCustomer);

/**
* Specification:
* - Returns fresh Customer transfer or NULL, if it does not exist
*
* @api
*
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return \Generated\Shared\Transfer\CustomerTransfer|null
*/
public function findCustomerById(CustomerTransfer $customerTransfer);

/**
* @api
*
Expand Down Expand Up @@ -238,4 +250,13 @@ public function setDefaultShippingAddress(AddressTransfer $addressTransfer);
*/
public function setDefaultBillingAddress(AddressTransfer $addressTransfer);

/**
* @api
*
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return \Generated\Shared\Transfer\CustomerTransfer
*/
public function anonymizeCustomer(CustomerTransfer $customerTransfer);

}
10 changes: 10 additions & 0 deletions src/Spryker/Client/Customer/Zed/CustomerStub.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,14 @@ public function setDefaultShippingAddress(AddressTransfer $AddressTransfer)
return $this->zedStub->call('/customer/gateway/default-shipping-address', $AddressTransfer);
}

/**
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return \Spryker\Shared\Kernel\Transfer\TransferInterface
*/
public function anonymizeCustomer(CustomerTransfer $customerTransfer)
{
return $this->zedStub->call('/customer/gateway/anonymize-customer', $customerTransfer);
}

}
7 changes: 7 additions & 0 deletions src/Spryker/Client/Customer/Zed/CustomerStubInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,11 @@ public function setDefaultBillingAddress(AddressTransfer $AddressTransfer);
*/
public function setDefaultShippingAddress(AddressTransfer $AddressTransfer);

/**
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return \Generated\Shared\Transfer\CustomerTransfer
*/
public function anonymizeCustomer(CustomerTransfer $customerTransfer);

}
2 changes: 2 additions & 0 deletions src/Spryker/Shared/Customer/Code/Messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ interface Messages
const CUSTOMER_EMAIL_ALREADY_USED = 'customer.email.already.used';
const CUSTOMER_EMAIL_INVALID = 'customer.email.invalid';
const CUSTOMER_TOKEN_INVALID = 'customer.token.invalid';
const CUSTOMER_ANONYMIZATION_SUCCESS = 'customer.anonymization.success';
const CUSTOMER_ANONYMIZATION_FAILED = 'customer.anonymization.failed';

}
6 changes: 4 additions & 2 deletions src/Spryker/Shared/Customer/Transfer/customer.transfer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<property name="fkCountry" type="int" />
<property name="fkRegion" type="int" />
<property name="iso2Code" type="string" />
<property name="anonymizedAt" type="string" />
</transfer>

<transfer name="Addresses">
Expand Down Expand Up @@ -62,8 +63,9 @@
<property name="registered" type="string" />
<property name="message" type="string" />
<property name="sendPasswordToken" type="bool" />
<property name="isGuest" type="bool"/>
<property name="locale" type="Locale"/>
<property name="isGuest" type="bool" />
<property name="locale" type="Locale" />
<property name="anonymizedAt" type="string" />
</transfer>

<transfer name="CustomerError">
Expand Down
200 changes: 200 additions & 0 deletions src/Spryker/Zed/Customer/Business/Anonymizer/CustomerAnonymizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\Customer\Business\Anonymizer;

use DateTime;
use Generated\Shared\Transfer\AddressesTransfer;
use Generated\Shared\Transfer\AddressTransfer;
use Generated\Shared\Transfer\CustomerTransfer;
use Spryker\Zed\Customer\Business\Customer\AddressInterface;
use Spryker\Zed\Customer\Business\Customer\CustomerInterface;
use Spryker\Zed\Customer\Persistence\CustomerQueryContainerInterface;
use Spryker\Zed\PropelOrm\Business\Transaction\DatabaseTransactionHandlerTrait;

class CustomerAnonymizer implements CustomerAnonymizerInterface
{

use DatabaseTransactionHandlerTrait;

/**
* @var \Spryker\Zed\Customer\Persistence\CustomerQueryContainerInterface
*/
protected $queryContainer;

/**
* @var \Spryker\Zed\Customer\Business\Customer\CustomerInterface
*/
protected $customerModel;

/**
* @var \Spryker\Zed\Customer\Business\Customer\AddressInterface
*/
protected $addressModel;

/**
* @var \Spryker\Zed\Customer\Dependency\Plugin\CustomerAnonymizerPluginInterface[]
*/
protected $plugins;

/**
* @param \Spryker\Zed\Customer\Persistence\CustomerQueryContainerInterface $customerQueryContainer
* @param \Spryker\Zed\Customer\Business\Customer\CustomerInterface $customerModel
* @param \Spryker\Zed\Customer\Business\Customer\AddressInterface $addressModel
* @param array $customerAnonymizerPlugins
*/
public function __construct(
CustomerQueryContainerInterface
$customerQueryContainer,
CustomerInterface $customerModel,
AddressInterface $addressModel,
array $customerAnonymizerPlugins
) {
$this->queryContainer = $customerQueryContainer;
$this->customerModel = $customerModel;
$this->addressModel = $addressModel;
$this->plugins = $customerAnonymizerPlugins;
}

/**
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return void
*/
public function process(CustomerTransfer $customerTransfer)
{
$customerTransfer->requireIdCustomer();
$customerTransfer = $this->getCustomer($customerTransfer);

$this->handleDatabaseTransaction(function () use ($customerTransfer) {
$this->processTransaction($customerTransfer);
});
}

/**
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return void
*/
protected function processTransaction(CustomerTransfer $customerTransfer)
{
foreach ($this->plugins as $plugin) {
$plugin->process($customerTransfer);
}

$addressesTransfer = $customerTransfer->getAddresses();
$addressesTransfer = $this->anonymizeCustomerAddresses($addressesTransfer);
$customerTransfer->setAddresses($addressesTransfer);

$customerTransfer = $this->anonymizeCustomer($customerTransfer);

$this->updateCustomerAddresses($customerTransfer->getAddresses());
$this->updateCustomer($customerTransfer);
}

/**
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return \Generated\Shared\Transfer\CustomerTransfer
*/
protected function getCustomer(CustomerTransfer $customerTransfer)
{
return $this->customerModel->get($customerTransfer);
}

/**
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return \Generated\Shared\Transfer\CustomerTransfer
*/
protected function anonymizeCustomer(CustomerTransfer $customerTransfer)
{
$customerTransfer
->setAnonymizedAt((new DateTime())->format("Y-m-d H:i:s.u"))
->setFirstName(null)
->setLastName(null)
->setSalutation(null)
->setGender(null)
->setDateOfBirth(null)
->setEmail($this->generateRandomEmail());

return $customerTransfer;
}

/**
* @return string
*/
protected function generateRandomEmail()
{
do {
$randomEmail = md5(mt_rand());
} while ($this->queryContainer->queryCustomerByEmail($randomEmail)->exists());

return $randomEmail;
}

/**
* @param \Generated\Shared\Transfer\AddressesTransfer $addressesTransfer
*
* @return \Generated\Shared\Transfer\AddressesTransfer
*/
protected function anonymizeCustomerAddresses(AddressesTransfer $addressesTransfer)
{
foreach ($addressesTransfer->getAddresses() as $addressTransfer) {
$this->anonymizeCustomerAddress($addressTransfer);
}

return $addressesTransfer;
}

/**
* @param \Generated\Shared\Transfer\AddressTransfer $addressTransfer
*
* @return \Generated\Shared\Transfer\AddressTransfer
*/
protected function anonymizeCustomerAddress(AddressTransfer $addressTransfer)
{
$addressTransfer
->setAnonymizedAt((new DateTime())->format("Y-m-d H:i:s.u"))
->setIsDeleted(true)
->setFirstName('')
->setLastName('')
->setSalutation(null)
->setAddress1(null)
->setAddress2(null)
->setAddress3(null)
->setCompany(null)
->setCity(null)
->setZipCode(null)
->setPhone(null);

return $addressTransfer;
}

/**
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return void
*/
protected function updateCustomer(CustomerTransfer $customerTransfer)
{
$this->customerModel->update($customerTransfer);
}

/**
* @param \Generated\Shared\Transfer\AddressesTransfer $addressesTransfer
*
* @return void
*/
protected function updateCustomerAddresses(AddressesTransfer $addressesTransfer)
{
foreach ($addressesTransfer->getAddresses() as $addressTransfer) {
$this->addressModel->updateAddress($addressTransfer);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/**
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
*/

namespace Spryker\Zed\Customer\Business\Anonymizer;

use Generated\Shared\Transfer\CustomerTransfer;

interface CustomerAnonymizerInterface
{

/**
* @param \Generated\Shared\Transfer\CustomerTransfer $customerTransfer
*
* @return void
*/
public function process(CustomerTransfer $customerTransfer);

}
22 changes: 22 additions & 0 deletions src/Spryker/Zed/Customer/Business/CustomerBusinessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Spryker\Zed\Customer\Business;

use Spryker\Zed\Customer\Business\Anonymizer\CustomerAnonymizer;
use Spryker\Zed\Customer\Business\Customer\Address;
use Spryker\Zed\Customer\Business\Customer\Customer;
use Spryker\Zed\Customer\Business\Model\CustomerOrderSaver;
Expand Down Expand Up @@ -108,6 +109,27 @@ public function createPreConditionChecker()
return new PreConditionChecker($this->createCustomer());
}

/**
* @return \Spryker\Zed\Customer\Business\Anonymizer\CustomerAnonymizer
*/
public function createCustomerAnonymizer()
{
return new CustomerAnonymizer(
$this->getQueryContainer(),
$this->createCustomer(),
$this->createAddress(),
$this->getCustomerAnonymizerPlugins()
);
}

/**
* @return \Spryker\Zed\Customer\Dependency\Plugin\CustomerAnonymizerPluginInterface[]
*/
public function getCustomerAnonymizerPlugins()
{
return $this->getProvidedDependency(CustomerDependencyProvider::PLUGINS_CUSTOMER_ANONYMIZER);
}

/**
* @return \Spryker\Zed\Locale\Persistence\LocaleQueryContainerInterface
*/
Expand Down
Loading

0 comments on commit 408c48c

Please sign in to comment.