AddressBook.php in Commerce Core 8.2
File
modules/order/src/AddressBook.php
View source
<?php
namespace Drupal\commerce_order;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\profile\Entity\ProfileInterface;
use Drupal\user\UserInterface;
class AddressBook implements AddressBookInterface {
protected $entityTypeBundleInfo;
protected $profileStorage;
protected $profileTypeStorage;
public function __construct(EntityTypeBundleInfo $entity_type_bundle_info, EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->profileStorage = $entity_type_manager
->getStorage('profile');
$this->profileTypeStorage = $entity_type_manager
->getStorage('profile_type');
}
public function allowsMultiple($profile_type_id) {
$bundle_info = $this->entityTypeBundleInfo
->getBundleInfo('profile');
return !empty($bundle_info[$profile_type_id]['multiple']);
}
public function hasUi() {
$profile_types = $this
->loadTypes();
if (empty($profile_types)) {
return FALSE;
}
elseif (count($profile_types) === 1) {
$profile_type = reset($profile_types);
if (!$profile_type
->allowsMultiple()) {
return FALSE;
}
}
return TRUE;
}
public function loadTypes() {
$profile_types = $this->profileTypeStorage
->loadByProperties([
'third_party_settings.commerce_order.customer_profile_type' => TRUE,
]);
return $profile_types;
}
public function loadAll(UserInterface $customer, $profile_type_id, array $available_countries = []) {
if ($customer
->isAnonymous()) {
return [];
}
$profiles = $this->profileStorage
->loadMultipleByUser($customer, $profile_type_id, TRUE);
foreach ($profiles as $profile_id => $profile) {
if (!$this
->isAvailable($profile, $available_countries)) {
unset($profiles[$profile_id]);
}
}
return $profiles;
}
public function load(UserInterface $customer, $profile_type_id, array $available_countries = []) {
if ($customer
->isAnonymous()) {
return NULL;
}
$profile = $this->profileStorage
->loadByUser($customer, $profile_type_id);
if ($profile && !$this
->isAvailable($profile, $available_countries)) {
$profile = NULL;
}
return $profile;
}
public function needsCopy(ProfileInterface $profile) {
return (bool) $profile
->getData('copy_to_address_book', FALSE);
}
public function copy(ProfileInterface $profile, UserInterface $customer) {
if ($customer
->isAnonymous()) {
return;
}
$address_book_profile = NULL;
$address_book_profile_id = $profile
->getData('address_book_profile_id');
if ($address_book_profile_id) {
$address_book_profile = $this->profileStorage
->load($address_book_profile_id);
}
if (!$address_book_profile && !$this
->allowsMultiple($profile
->bundle())) {
$address_book_profile = $this
->load($customer, $profile
->bundle());
}
if ($address_book_profile) {
$address_book_profile
->populateFromProfile($profile);
$address_book_profile
->save();
}
else {
$address_book_profile = $profile
->createDuplicate();
$address_book_profile
->setOwnerId($customer
->id());
$address_book_profile
->unsetData('copy_to_address_book');
$address_book_profile
->save();
}
$profile
->unsetData('copy_to_address_book');
$profile
->setData('address_book_profile_id', $address_book_profile
->id());
$profile
->save();
}
protected function isAvailable(ProfileInterface $profile, array $available_countries) {
if (empty($available_countries)) {
return TRUE;
}
$address = $profile
->get('address')
->first();
$country_code = $address ? $address
->getCountryCode() : 'ZZ';
return in_array($country_code, $available_countries);
}
}