Skip to content

Commit

Permalink
Merge pull request #1 from liamjtoohey/feature/PREZACC-13
Browse files Browse the repository at this point in the history
PREZACC-13 - Snow bundle for Akeneo 2
  • Loading branch information
nei authored May 23, 2018
2 parents 219eaae + 32f8ad8 commit 9595de6
Show file tree
Hide file tree
Showing 44 changed files with 3,086 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor/
/composer.lock
/.idea/
/.php-version
23 changes: 23 additions & 0 deletions DependencyInjection/SnowioCsvConnectorExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Snowio\Bundle\CsvConnectorBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;

class SnowioCsvConnectorExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('steps.yml');
$loader->load('readers.yml');
$loader->load('writers.yml');
$loader->load('jobs.yml');
$loader->load('form_parameters.yml');
$loader->load('job_defaults.yml');
$loader->load('job_constraints.yml');
}
}
65 changes: 65 additions & 0 deletions Handler/ArchiveHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Snowio\Bundle\CsvConnectorBundle\Handler;

use Akeneo\Component\Batch\Item\AbstractConfigurableStepElement;
use Akeneo\Component\Batch\Model\StepExecution;
use Akeneo\Component\Batch\Step\StepExecutionAwareInterface;

class ArchiveHandler extends AbstractConfigurableStepElement implements StepExecutionAwareInterface
{
const ZIP_FILE_NAME = 'export.zip';

/** @var string */
protected $exportDir;
/** @var StepExecution */
private $stepExecution;

public function execute()
{
$zip = new \ZipArchive();
$location = rtrim($this->exportDir, '/') . DIRECTORY_SEPARATOR . self::ZIP_FILE_NAME;
$opened = $zip->open($location, \ZipArchive::CREATE);
if ($opened !== true) {
$this->stepExecution->addFailureException(new \RuntimeException('Failed to open zip, reason code:' . $opened));
} else {
$success = $zip->addPattern(
'/(?:\w+\.csv|metadata.json)/',
$this->exportDir,
['add_path' => '/', 'remove_all_path' => true]
);
if (!$success) {
$this->stepExecution->addFailureException(new \RuntimeException('Failed to add files to zip.'));
}
$zip->close();
$this->stepExecution->addSummaryInfo('zip_location', $location);
}
}

public function getConfigurationFields()
{
return [
'exportDir' => [
'options' => [
'label' => 'snowio_connector.form.exportDir.label',
'help' => 'snowio_connector.form.exportDir.help'
]
]
];
}

public function setStepExecution(StepExecution $stepExecution)
{
$this->stepExecution = $stepExecution;
}

public function getExportDir()
{
return $this->exportDir;
}

public function setExportDir($exportDir)
{
$this->exportDir = $exportDir;
}
}
55 changes: 55 additions & 0 deletions Handler/MetadataHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Snowio\Bundle\CsvConnectorBundle\Handler;

use Akeneo\Component\Batch\Item\AbstractConfigurableStepElement;
use Akeneo\Component\Batch\Model\StepExecution;
use Akeneo\Component\Batch\Step\StepExecutionAwareInterface;
use Snowio\Bundle\CsvConnectorBundle\SnowioCsvConnectorBundle;

class MetadataHandler extends AbstractConfigurableStepElement implements StepExecutionAwareInterface
{
/** @var StepExecution */
private $stepExecution;
/** @var array */
private $configs;

public function execute()
{
$location = rtrim($this->configs['exportDir'], '/') . DIRECTORY_SEPARATOR . 'metadata.json';
file_put_contents(
$location,
json_encode([
'bundleVersion' => SnowioCsvConnectorBundle::VERSION,
'jobCode' => $this->stepExecution->getJobExecution()->getJobInstance()->getAlias(),
'date' => gmdate('Y-m-d_H:i:s'),
'channel' => $this->configs['channel'],
'delimiter' => $this->configs['delimiter'],
'enclosure' => $this->configs['enclosure'],
'withHeader' => $this->configs['withHeader'],
'decimalSeparator' => $this->configs['decimalSeparator'],
'dateFormat' => $this->configs['dateFormat'],
])
);

$this->stepExecution->addSummaryInfo('metadata_location', $location);
}

public function getConfigurationFields()
{
return [];
}

public function setConfiguration(array $config)
{
parent::setConfiguration($config);
$this->configs = $config;

}

public function setStepExecution(StepExecution $stepExecution)
{
$this->stepExecution = $stepExecution;
}

}
200 changes: 200 additions & 0 deletions Handler/PostHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

namespace Snowio\Bundle\CsvConnectorBundle\Handler;

use Akeneo\Component\Batch\Model\StepExecution;
use Akeneo\Component\Batch\Step\StepExecutionAwareInterface;
use Akeneo\Component\Batch\Item\AbstractConfigurableStepElement;
use GuzzleHttp\ClientInterface;

class PostHandler extends AbstractConfigurableStepElement implements StepExecutionAwareInterface
{
/** @var StepExecution */
protected $stepExecution;
/** @var string */
protected $url;
/** @var string */
protected $exportDir;
/** @var string */
protected $applicationId;
/** @var string */
protected $secretKey;
/** @var \GuzzleHttp\Client */
protected $client;

/**
* PostHandler constructor.
* @param \GuzzleHttp\ClientInterface $client
* @author Cristian Quiroz <[email protected]>
*/
public function __construct(ClientInterface $client)
{
$this->client = $client;
}

/**
* @throws \Exception
* @author Cristian Quiroz <[email protected]>
*/
public function execute()
{
$url = $this->getUrl() . $this->getApplicationId();
$this->stepExecution->addSummaryInfo('url', $url);

$resource = fopen(rtrim($this->exportDir, '/') . DIRECTORY_SEPARATOR . ArchiveHandler::ZIP_FILE_NAME, 'r');

if (false === $resource) {
$this->stepExecution->addFailureException(new \RuntimeException('Failed to open file to send to snow.io'));
return;
}

$response = $this->client->request(
'POST',
$url,
[
'body' => $resource,
'headers' => [
'Content-Type' => 'application/zip',
'Authorization' => $this->getSecretKey(),
],
]
);

$this->stepExecution->addSummaryInfo('response_code', $response->getStatusCode());
$this->stepExecution->addSummaryInfo('response_body', $response->getBody());

if ($response->getStatusCode() !== 204) {
// Unexpected response, handle
$this->stepExecution->addFailureException(new \Exception('Failed to POST csv data: ' . $response->getBody()));
}

}

/**
* @param \Akeneo\Component\Batch\Model\StepExecution $stepExecution
* @author Cristian Quiroz <[email protected]>
*/
public function setStepExecution(StepExecution $stepExecution)
{
$this->stepExecution = $stepExecution;
}

/**
* @param string $exportDir
* @return $this
* @author Cristian Quiroz <[email protected]>
*/
public function setExportDir($exportDir)
{
$this->exportDir = $exportDir;

return $this;
}

/**
* @return string
* @author Cristian Quiroz <[email protected]>
*/
public function getExportDir()
{
return $this->exportDir;
}

/**
* @return string
*/
public function getApplicationId()
{
return $this->applicationId;
}

/**
* @param string $applicationId
* @return $this
*/
public function setApplicationId($applicationId)
{
$this->applicationId = $applicationId;

return $this;
}

/**
* @return string
*/
public function getSecretKey()
{
return $this->secretKey;
}

/**
* @param string $secretKey
* @return $this
*/
public function setSecretKey($secretKey)
{
$this->secretKey = $secretKey;

return $this;
}

/**
* @param $url
* @return $this
* @author Cristian Quiroz <[email protected]>
*/
public function setUrl($url)
{
$this->url = $url;

return $this;
}

/**
* @return string
* @author Cristian Quiroz <[email protected]>
*/
public function getUrl()
{
return rtrim($this->url, '/') . '/';
}

/**
* Here, we define the form fields to use
* @return array
* @author Cristian Quiroz <[email protected]>
*/
public function getConfigurationFields()
{
return [
'exportDir' => [
'options' => [
'label' => 'snowio_connector.form.exportDir.label',
'help' => 'snowio_connector.form.exportDir.help'
]
],
'url' => [
'type' => 'text',
'required' => true,
'options' => [
'label' => 'snowio_connector.form.url.label',
'help' => 'snowio_connector.form.url.help'
],
],
'applicationId' => [
'type' => 'text',
'required' => true,
'options' => [
'label' => 'snowio_connector.form.applicationId.label',
],
],
'secretKey' => [
'type' => 'password',
'required' => true,
'options' => [
'label' => 'snowio_connector.form.secretKey.label',
],
],
];
}
}
Loading

0 comments on commit 9595de6

Please sign in to comment.