You are here

class OptOut in Mass Contact 8

The user opt out service.

Hierarchy

Expanded class hierarchy of OptOut

1 string reference to 'OptOut'
mass_contact.services.yml in ./mass_contact.services.yml
mass_contact.services.yml
1 service uses OptOut
mass_contact.opt_out in ./mass_contact.services.yml
Drupal\mass_contact\OptOut

File

src/OptOut.php, line 12

Namespace

Drupal\mass_contact
View source
class OptOut implements OptOutInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityManager;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * OptOut constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->configFactory = $config_factory;
    $this->entityManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getOptOutAccounts(array $categories = []) {

    // Get the latest configs.
    $config = $this->configFactory
      ->get('mass_contact.settings');
    if ($config
      ->get('optout_enabled') === MassContactInterface::OPT_OUT_DISABLED) {

      // Opt-out is completely disabled, return empty.
      return [];
    }
    $query = $this->entityManager
      ->getStorage('user')
      ->getQuery();
    $query
      ->condition('status', 1);
    if ($config
      ->get('optout_enabled') === MassContactInterface::OPT_OUT_GLOBAL) {

      // Any user with a value here has opted out.
      $query
        ->condition(MassContactInterface::OPT_OUT_FIELD_ID, 0, '<>');
    }
    else {
      $category_ids = array_map(function (MassContactCategoryInterface $category) {
        return $category
          ->id();
      }, $categories);
      $group = $query
        ->orConditionGroup()
        ->condition(MassContactInterface::OPT_OUT_FIELD_ID, $category_ids, 'IN')
        ->condition(MassContactInterface::OPT_OUT_FIELD_ID, '1');
      $query
        ->condition($group);
    }
    return $query
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OptOut::$configFactory protected property The config factory service.
OptOut::$entityManager protected property The entity type manager.
OptOut::getOptOutAccounts public function Finds a list of users that have opted out of emails. Overrides OptOutInterface::getOptOutAccounts
OptOut::__construct public function OptOut constructor.