<?php
namespace Drupal\bb2b_baselinker\Plugin\QueueWorker;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\Queue\RequeueException;
use Drupal\Tests\Core\Logger\LoggerChannelFactoryTest;
use GuzzleHttp\Client;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines 'bb2b_baselinker_addproduct' queue worker.
*
* @QueueWorker(
* id = "bb2b_baselinker_addproduct",
* title = @Translation("AddProduct"),
* cron = {"time" = 60}
* )
*/
class AddProductQueue extends QueueWorkerBase {
/**
* The http client.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* The logger.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
public function __construct(
array $configuration, $plugin_id,
$plugin_definition,
Client $httpClient,
LoggerChannelFactoryInterface $logger_factory
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->httpClient = $httpClient;
$this->logger = $logger_factory->get('bb2b_baselinker');
}
public static function create (
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition
) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('http_client'),
$container->get('logger.factory')
);
}
/**
* {@inheritdoc}
*/
public function processItem($data) {
// @todo Process data here.
$params = [
'storage_id' => '4721',
'product_id' => $data['product']->id(),
'ean' => $data['product']->get('ean')->value,
];
try {
$response = $this->httpClient->request('post', $data['connected_domain'], [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
'X-BLToken' => $data['token'],
],
'form_params' => [
'method' => 'addProduct',
'parameters' => $params,
]
]);
}
catch (RequeueException $e) {
$this->logger->debug('BaseLinker request failed with the message: @message', [
'@message' => $e->getMessage(),
]);
}
$response = json_decode($response->getStatusCode());
return $response;
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter