-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing issue #10: the export engine
- Loading branch information
Showing
8 changed files
with
379 additions
and
2 deletions.
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
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
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,90 @@ | ||
<?php | ||
/* | ||
* This file is part of Cantiga Project. Copyright 2015 Tomasz Jedrzejewski. | ||
* | ||
* Cantiga Project is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Cantiga Project is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Foobar; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
namespace Cantiga\ExportBundle\Command; | ||
|
||
use Exception; | ||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
/** | ||
* @author Tomasz Jędrzejewski | ||
*/ | ||
class ExportCommand extends ContainerAwareCommand | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('cantiga:export-data') | ||
->setDescription('Exports the data to the external services via REST.') | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$exportEngine = $this->getContainer()->get('cantiga.export.repo.engine'); | ||
foreach ($exportEngine->findActiveExports() as $export) { | ||
$output->writeln('<info>Exporting data to \''.$export['name'].'\' endpoint.</info>'); | ||
try { | ||
$result = $exportEngine->exportData($export, function($text) use($output) { | ||
$output->writeln($text, OutputInterface::VERBOSITY_NORMAL); | ||
}); | ||
if ($this->send($export, $this->encrypt($export, $result), $output)) { | ||
$output->writeln('<info>Export completed</info>'); | ||
} else { | ||
$output->writeln('<info>Export failed</info>'); | ||
} | ||
} catch(Exception $exception) { | ||
$output->writeln('<error>Export failed: '.$exception->getMessage().'</error>'); | ||
} | ||
} | ||
} | ||
|
||
private function encrypt($export, $output) | ||
{ | ||
$key = base64_decode($export['encryptionKey']); | ||
|
||
if (strlen($key) != 32) { | ||
throw new \RuntimeException('The key must have the length of 32 bytes!'); | ||
} | ||
|
||
$ivSize = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); | ||
$iv = mcrypt_create_iv($ivSize, MCRYPT_RAND); | ||
return base64_encode($iv.mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, json_encode($output), MCRYPT_MODE_CBC, $iv)); | ||
} | ||
|
||
private function send($export, $encrypted, OutputInterface $output) | ||
{ | ||
$ch = curl_init(); | ||
curl_setopt($ch, CURLOPT_URL, $export['url']); | ||
curl_setopt($ch, CURLOPT_POST, 1); | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, $encrypted); | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); | ||
|
||
$result = curl_exec($ch); | ||
$errno = curl_errno($ch); | ||
if ($errno !== 0) { | ||
$output->writeln('<error>The server responded with error: '.$errno.' ('.curl_error($ch).')</error>'); | ||
curl_close($ch); | ||
return false; | ||
} | ||
curl_close($ch); | ||
return true; | ||
} | ||
} |
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 | ||
/* | ||
* This file is part of Cantiga Project. Copyright 2015 Tomasz Jedrzejewski. | ||
* | ||
* Cantiga Project is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Cantiga Project is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Foobar; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
namespace Cantiga\ExportBundle\Entity; | ||
|
||
/** | ||
* Represents a single pack of data that shall be exported. It consists of the | ||
* total list of all ID-s that shall be present in the external system, and the | ||
* full records of those entities that need to be updated. | ||
* | ||
* @author Tomasz Jędrzejewski | ||
*/ | ||
class ExportBlock | ||
{ | ||
private $ids = []; | ||
private $update = []; | ||
|
||
public function addId($id) | ||
{ | ||
$this->ids[] = $id; | ||
} | ||
|
||
public function addUpdate(array $update) | ||
{ | ||
$this->update[] = $update; | ||
} | ||
|
||
public function countIds() | ||
{ | ||
return sizeof($this->ids); | ||
} | ||
|
||
public function countUpdates() | ||
{ | ||
return sizeof($this->update); | ||
} | ||
|
||
public function getIds() | ||
{ | ||
return $this->ids; | ||
} | ||
|
||
public function output() | ||
{ | ||
return [ | ||
'ids' => $this->ids, | ||
'update' => $this->update | ||
]; | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
/* | ||
* This file is part of Cantiga Project. Copyright 2015 Tomasz Jedrzejewski. | ||
* | ||
* Cantiga Project is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Cantiga Project is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Foobar; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
namespace Cantiga\ExportBundle\Event; | ||
|
||
use Cantiga\ExportBundle\Entity\ExportBlock; | ||
use InvalidArgumentException; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
/** | ||
* Description of ExportEvent | ||
* | ||
* @author Tomasz Jędrzejewski | ||
*/ | ||
class ExportEvent extends Event | ||
{ | ||
private $blocks = array(); | ||
private $reporter = null; | ||
|
||
public function __construct($reporterCallback) | ||
{ | ||
$this->reporter = $reporterCallback; | ||
} | ||
|
||
public function addBlock($name, ExportBlock $block) | ||
{ | ||
if (isset($this->blocks[$name])) { | ||
throw new InvalidArgumentException('Cannot overwrite export block: '.$name); | ||
} | ||
$this->blocks[$name] = $block; | ||
$callback = $this->reporter; | ||
$callback('Exporting block '.$name.': IDs '.$block->countIds().'; Updates '.$block->countUpdates()); | ||
} | ||
|
||
public function getBlock($name) | ||
{ | ||
if (!isset($this->blocks[$name])) { | ||
throw new InvalidArgumentException('No such export block: '.$name); | ||
} | ||
return $this->blocks[$name]; | ||
} | ||
|
||
public function output() | ||
{ | ||
$output = []; | ||
foreach ($this->blocks as $name => $block) { | ||
$output[$name] = $block->output(); | ||
} | ||
return $output; | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
/* | ||
* This file is part of Cantiga Project. Copyright 2015 Tomasz Jedrzejewski. | ||
* | ||
* Cantiga Project is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Cantiga Project is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Foobar; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
*/ | ||
namespace Cantiga\ExportBundle; | ||
|
||
/** | ||
* @author Tomasz Jędrzejewski | ||
*/ | ||
class ExportEvents | ||
{ | ||
/** | ||
* Call for adding the blocks to export to the external system. | ||
*/ | ||
const EXPORT_ONGOING = 'export.ongoing'; | ||
} |
Oops, something went wrong.