SalesforceMappingStorage.php in Salesforce Suite 8.3
File
modules/salesforce_mapping/src/SalesforceMappingStorage.php
View source
<?php
namespace Drupal\salesforce_mapping;
use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\Entity\ConfigEntityStorage;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityInterface;
class SalesforceMappingStorage extends ConfigEntityStorage {
protected $configFactory;
protected $uuidService;
protected $languageManager;
protected $entityManager;
public function __construct($entity_type_id, ConfigFactoryInterface $config_factory, UuidInterface $uuid_service, LanguageManagerInterface $language_manager, EntityManagerInterface $entity_manager) {
$entity_type = $entity_manager
->getDefinition($entity_type_id);
parent::__construct($entity_type, $config_factory, $uuid_service, $language_manager);
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type
->id(), $container
->get('config.factory'), $container
->get('uuid'), $container
->get('language_manager'), $container
->get('entity.manager'));
}
public function loadByDrupal($entity_type_id) {
return $this
->loadByProperties([
"drupal_entity_type" => $entity_type_id,
]);
}
public function loadByEntity(EntityInterface $entity) {
return $this
->loadByProperties([
'drupal_entity_type' => $entity
->getEntityTypeId(),
'drupal_bundle' => $entity
->bundle(),
]);
}
public function loadPushMappings($entity_type_id = NULL) {
$properties = empty($entity_type_id) ? [] : [
"drupal_entity_type" => $entity_type_id,
];
return $this
->loadPushMappingsByProperties($properties);
}
public function loadCronPushMappings() {
if ($this->configFactory
->get('salesforce.settings')
->get('standalone')) {
return [];
}
$properties["push_standalone"] = FALSE;
return $this
->loadPushMappingsByProperties($properties);
}
public function loadCronPullMappings() {
if ($this->configFactory
->get('salesforce.settings')
->get('standalone')) {
return [];
}
return $this
->loadPullMappingsByProperties([
"pull_standalone" => FALSE,
]);
}
public function loadPushMappingsByProperties(array $properties) {
$mappings = $this
->loadByProperties($properties);
foreach ($mappings as $key => $mapping) {
if (!$mapping
->doesPush()) {
continue;
}
$push_mappings[$key] = $mapping;
}
if (empty($push_mappings)) {
return [];
}
return $push_mappings;
}
public function loadPullMappingsByProperties(array $properties) {
$mappings = $this
->loadByProperties($properties);
foreach ($mappings as $key => $mapping) {
if (!$mapping
->doesPull()) {
continue;
}
$push_mappings[$key] = $mapping;
}
if (empty($push_mappings)) {
return [];
}
return $push_mappings;
}
public function loadPullMappings($entity_type_id = NULL) {
$pull_mappings = [];
$properties = empty($entity_type_id) ? [] : [
"drupal_entity_type" => $entity_type_id,
];
$mappings = $this
->loadByProperties($properties);
foreach ($mappings as $key => $mapping) {
if (!$mapping
->doesPull()) {
continue;
}
$pull_mappings[$key] = $mapping;
}
if (empty($pull_mappings)) {
return [];
}
return $pull_mappings;
}
public function loadByProperties(array $values = []) {
$entity_query = $this
->getQuery();
$this
->buildPropertyQuery($entity_query, $values);
$entity_query
->sort('weight');
$result = $entity_query
->execute();
return $result ? $this
->loadMultiple($result) : [];
}
public function getMappedSobjectTypes() {
$object_types = [];
$mappings = $this
->loadByProperties();
foreach ($mappings as $mapping) {
$type = $mapping
->getSalesforceObjectType();
$object_types[$type] = $type;
}
return $object_types;
}
}