You are here

class ModifierIndex in Persistent URL 8

Hierarchy

Expanded class hierarchy of ModifierIndex

4 files declare their use of ModifierIndex
ModifiersController.php in src/Controller/ModifiersController.php
ProvidersController.php in src/Controller/ProvidersController.php
RebuildIndex.php in src/Event/RebuildIndex.php
RequestSubscriber.php in src/Event/RequestSubscriber.php
1 string reference to 'ModifierIndex'
purl.services.yml in ./purl.services.yml
purl.services.yml
1 service uses ModifierIndex
purl.modifier_index in ./purl.services.yml
Drupal\purl\Plugin\ModifierIndex

File

src/Plugin/ModifierIndex.php, line 8

Namespace

Drupal\purl\Plugin
View source
class ModifierIndex {
  protected $connection;
  protected $providerManager;
  public function __construct(ProviderManager $providerManager, Connection $connection) {
    $this->providerManager = $providerManager;
    $this->connection = $connection;
  }
  public function findModifiers() {
    $result = $this->connection
      ->select('purl_modifiers', 'p')
      ->fields('p', array(
      'provider',
      'modifier',
      'value',
    ))
      ->execute();
    return array_map(function ($row) {
      return array_merge((array) $row, array(
        'value' => unserialize($row->value),
      ));
    }, $result
      ->fetchAll());
  }
  public function indexModifiers(ProviderInterface $provider, $method) {
    $id = $provider
      ->getId();
    $modifiers = $provider
      ->getModifiers();
    if (!is_array($modifiers)) {
      return;
    }
    $this
      ->deleteEntriesByProvider($provider
      ->getId());
    foreach ($modifiers as $modifier => $value) {
      $this->connection
        ->insert('purl_modifiers')
        ->fields(array(
        'modifier' => $modifier,
        'provider' => $id,
        'value' => serialize($value),
      ))
        ->execute();
    }
    $this->connection
      ->update('purl_providers_settings')
      ->condition('provider', $provider
      ->getId())
      ->fields(array(
      'rebuild' => 0,
    ))
      ->execute();
  }
  public function deleteEntriesByProvider($providerId) {
    $this->connection
      ->delete('purl_modifiers')
      ->condition('provider', $providerId)
      ->execute();
  }
  public function deleteAll() {
    $this->connection
      ->delete('purl_modifiers')
      ->execute();
  }
  public function rebuild($providerId = null, $immediately = false) {
    if ($immediately) {
      if (!$providerId) {
        throw new \BadMethodCallException('A provider must be specified during immediate rebuilds.');
      }
      $provider = $this->providerManager
        ->getProvider($providerId);
      $definition = $this->providerManager
        ->getDefinition($providerId);
      if (!$definition['method']) {
        return;
      }
      $this
        ->indexModifiers($providerId, $definition['method']);
    }
    else {
      $statement = $this->connection
        ->update('purl_providers_settings')
        ->fields(array(
        'rebuild' => 1,
      ));
      if ($providerId) {
        $statement
          ->condition('provider', $providerId);
      }
      $statement
        ->execute();
    }
  }
  public function performDueRebuilds() {
    $result = $this->connection
      ->select('purl_providers_settings', 'p')
      ->fields('p', array(
      'provider',
      'method',
    ))
      ->condition('p.rebuild', 1)
      ->execute();
    foreach ($result
      ->fetchAll() as $row) {
      $provider = $this->providerManager
        ->getProvider($row->provider);
      $this
        ->indexModifiers($provider, $row->method);
    }
  }

}

Members