-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from liamjtoohey/feature/PREZACC-13
PREZACC-13 - Snow bundle for Akeneo 2
- Loading branch information
Showing
44 changed files
with
3,086 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/vendor/ | ||
/composer.lock | ||
/.idea/ | ||
/.php-version |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
], | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.