Skip to content

Instantly share code, notes, and snippets.

@cyberlex404
Last active October 13, 2017 08:07
Show Gist options
  • Select an option

  • Save cyberlex404/96d9d1fd7013388987fbd4d0dbff0f70 to your computer and use it in GitHub Desktop.

Select an option

Save cyberlex404/96d9d1fd7013388987fbd4d0dbff0f70 to your computer and use it in GitHub Desktop.
Queue Worker Plugin
<?php
/**
* Created by PhpStorm.
* User: Lex
* Date: 03.10.2017
* Time: 0:26
*/
namespace Drupal\housing_sync\Plugin\QueueWorker;
/**
* Class GallerySyncProcessor
*
* @QueueWorker(
* id = "gallery_sync_processor",
* title = "Gallery sync processor",
* cron = {"time" = 50}
* )
*
* @package Drupal\housing_sync\Plugin\QueueWorker
*/
class GallerySyncProcessor extends GallerySyncWorker {
}
<?php
namespace Drupal\housing_sync\Plugin\QueueWorker;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Logger\LoggerChannel;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\file\Entity\File;
use GuzzleHttp\Client;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GallerySyncWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {
/**
* Drupal\Core\Entity\EntityTypeManager definition.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;
/**
* GuzzleHttp\Client definition.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* Drupal\Core\Logger\LoggerChannel definition.
*
* @var \Drupal\Core\Logger\LoggerChannel
*/
protected $logger;
/**
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $fileStorage;
public function __construct(
EntityTypeManager $entity_type_manager,
Client $http_client,
LoggerChannel $logger
) {
$this->entityTypeManager = $entity_type_manager;
$this->httpClient = $http_client;
$this->logger = $logger;
$this->fileStorage = $this->entityTypeManager->getStorage('file');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$container->get('entity_type.manager'),
$container->get('http_client'),
$container->get('logger.channel.housings')
);
}
public function processItem($data) {
// TODO: Implement processItem() method.
try{
/**
* @var $gallery \Drupal\housing_gallery\Entity\HousingGallery
*
*/
$gallery = $this->entityTypeManager->getStorage('housing_gallery')->load($data->target);
$dataArray = (array)$data;
$params = implode("::" ,$dataArray);
//$this->logger->debug('Execute task:' . $data->uri . ' ' .$data->filename . ' ' .$data->target . ' ' . $data->uuid);
\Drupal::logger('test')->debug($params);
if (!$file = $this->fileIsset($data->filename, $data->uuid)) {
$target_uri = $this->fileLoad($data->filename, $data->uri);
/**
* @var $file \Drupal\file\FileInterface
*/
$file = $this->fileStorage->create([
'uri' => $target_uri,
'uuid' => $data->uuid,
'filename' => $data->filename,
'status' => TRUE,
]);
$file->setOwner($gallery->getOwner());
$file->save();
$gallery->photos[] = $file->id();
$gallery->save();
}
}catch (\Exception $e) {
drupal_set_message($e->getMessage());
}
}
/**
* @param $filename
* @param $uri
* @return string
*/
protected function fileLoad($filename, $uri) {
$source = 'https://braslavskie.by/sites/default/files' . substr($uri, 8);
$target = 'sites/default/files/housing/gallery/' . $filename;
if(!file_exists($target)) {
$resource = fopen($target, 'w');
$this->httpClient->request('GET', $source, ['sink' => $resource]);
\Drupal::logger('sync gallery')->debug('Load file: ' . $target);
}else {
$message = new FormattableMarkup('File by name @name exists. URI: @uri', [
'@name' => $filename,
'@uri' => $uri,
]);
$this->logger->error($message);
}
$target_uri = 'public://housing/gallery/' . $filename;
return $target_uri;
}
/**
* @param $filename
* @param $uuid
*
* @return bool|\Drupal\file\FileInterface
*/
protected function fileIsset($filename, $uuid) {
$load = $this->fileStorage->loadByProperties([
'uuid' => $uuid,
'filename' => $filename,
]);
if (empty($load)) {
return FALSE;
}else{
return reset($load);
}
}
}
public function queueCallback(array &$form, FormStateInterface $form_state) {
$buildData = $form_state->getBuildInfo();
$queue = $this->queue->get('gallery_sync_processor');
if ((isset($buildData['gallery_data'])) && (isset($buildData['target_gallery']))) {
$data = $buildData['gallery_data'];
$photos = $data['photos'];
/**
* @var $targetGallery HousingGallery
*/
$targetGallery = $buildData['target_gallery'];
foreach ($photos as $photo) {
$task = new \stdClass();
$task->uuid = $photo['uuid'];
$task->filename = $photo['filename'];
$task->uri = $photo['uri'];
$task->target = $targetGallery->id();
$queue->createItem($task);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment