3 Commits

Author SHA1 Message Date
jeremycr
806e5bb90f Fixed next execution for scheduled dataflows not increasing (#36) 2019-11-22 14:15:29 +01:00
jbcr
d18494212d fix example (#33)
* fix example
2019-11-19 08:18:56 +01:00
Olivier PORTIER
fbc4a20b57 Mise à jour de la commande d'installation code-rhapsodie/dataflow-bundle (#31) 2019-11-18 16:47:30 +01:00
3 changed files with 39 additions and 35 deletions

View File

@@ -40,7 +40,7 @@ As the following schema shows, you can define more than one dataflow:
To install this bundle, run this command :
```shell script
$ composer require code-rhapsodie/dataflow
$ composer require code-rhapsodie/dataflow-bundle
```
#### Suggest
@@ -167,11 +167,11 @@ class MyFirstDataflowType extends AbstractDataflowType
protected function buildDataflow(DataflowBuilder $builder, array $options): void
{
$this->myReader->setFilename($options['fileName']);
$this->myWriter->setDestinationFilePath($options['to-file']);
$builder->setReader($this->myReader)
->addStep(function($data) use ($options) {
$builder
->setReader($this->myReader->read($options['from-file']))
->addStep(function ($data) use ($options) {
// TODO : Write your code here...
return $data;
})
@@ -181,11 +181,8 @@ class MyFirstDataflowType extends AbstractDataflowType
protected function configureOptions(OptionsResolver $optionsResolver): void
{
$optionsResolver->setDefaults([
'my_option' => 'my_default_value',
'fileName' => null,
]);
$optionsResolver->setRequired('fileName');
$optionsResolver->setDefaults(['to-file' => '/tmp/dataflow.csv', 'from-file' => null]);
$optionsResolver->setRequired('from-file');
}
public function getLabel(): string
@@ -229,11 +226,8 @@ class MyFirstDataflowType extends AbstractDataflowType
// ...
protected function configureOptions(OptionsResolver $optionsResolver): void
{
$optionsResolver->setDefaults([
'my_option' => 'my_default_value',
'fileName' => null,
]);
$optionsResolver->setRequired('fileName');
$optionsResolver->setDefaults(['to-file' => '/tmp/dataflow.csv', 'from-file' => null]);
$optionsResolver->setRequired('from-file');
}
}
@@ -281,27 +275,18 @@ namespace CodeRhapsodie\DataflowExemple\Reader;
class FileReader
{
private $filename;
/**
* Set the filename option needed by the Reader.
*/
public function setFilename(string $filename) {
$this->filename = $filename;
}
public function __invoke(): iterable
public function read(string $filename): iterable
{
if (!$this->filename) {
if (!$filename) {
throw new \Exception("The file name is not defined. Define it with 'setFilename' method");
}
if (!$fh = fopen($this->filename, 'r')) {
throw new \Exception("Unable to open file '".$this->filename."' for read.");
if (!$fh = fopen($filename, 'r')) {
throw new \Exception("Unable to open file '".$filename."' for read.");
}
while (false === ($read = fread($fh, 1024))) {
yield explode("|", $read);
while (false !== ($read = fgets($fh))) {
yield explode('|', trim($read));
}
}
}
@@ -327,14 +312,16 @@ A *Step* can be any callable, taking the element as its argument, and returning
A few examples:
```php
$builder->addStep(function($item) {
<?php
//[...]
$builder->addStep(function ($item) {
// Titles are changed to all caps before export
$item['title'] = strtoupper($item['title']);
return $item;
});
$builder->addStep(function($item) {
$builder->addStep(function ($item) {
// Private items are not exported
if ($item['private']) {
return false;
@@ -342,6 +329,7 @@ $builder->addStep(function($item) {
return $item;
});
//[...]
```
### Writers
@@ -369,11 +357,20 @@ class FileWriter implements WriterInterface
{
private $fh;
/** @var string */
private $path;
public function setDestinationFilePath(string $path) {
$this->path = $path;
}
public function prepare()
{
if (!$this->fh = fopen('/path/to/file', 'w')) {
throw new \Exception("Unable to open in write mode the output file.");
if (null === $this->path) {
throw new \Exception('Define the destination file name before use');
}
if (!$this->fh = fopen($this->path, 'w')) {
throw new \Exception('Unable to open in write mode the output file.');
}
}

View File

@@ -83,6 +83,12 @@ class ScheduledDataflowManagerTest extends TestCase
)
;
$this->scheduledDataflowRepository
->expects($this->once())
->method('save')
->with($scheduled2)
;
$this->connection
->expects($this->once())
->method('commit')

View File

@@ -67,6 +67,7 @@ class ScheduledDataflowManager implements ScheduledDataflowManagerInterface
}
$scheduled->setNext($next);
$this->scheduledDataflowRepository->save($scheduled);
}
/**