Skip to content

Commit

Permalink
[add] Console command to trigger DR
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikel committed Jan 8, 2019
1 parent 62c9b70 commit 7423f10
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 1 deletion.
84 changes: 84 additions & 0 deletions Console/TriggerReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Copyright © 2017 SeQura Engineering. All rights reserved.
*/

namespace Sequra\Core\Console;

use \Sequra\Core\Model\ConfigFactory;
use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputArgument;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;

class TriggerReport extends Command
{
/** Command name */
const NAME = 'sequra:triggerreport';

/**
* Names of input arguments or options
*/
const INPUT_KEY_SHOPCODES = 'shopcodes';

/**
* @var \Sequra\Core\Model\Config
*/
protected $_config;

/**
* @var \Sequra\Core\Model\ReporterFactory
*/
protected $_reporter;


/**
* Constructor
*
* @param ConfigFactory $configFactory
* @param \Sequra\Core\Model\ReporterFactory $reporterFactory
*/
public function __construct(
ConfigFactory $configFactory,
\Sequra\Core\Model\ReporterFactory $reporterFactory
) {
$this->_config = $configFactory->create();
$this->_reporter = $reporterFactory->create();
parent::__construct();
}

protected function configure()
{
$this->addArgument(
self::INPUT_KEY_SHOPCODES,
InputArgument::IS_ARRAY | InputArgument::OPTIONAL,
'Shop code'
);
$this->setName(self::NAME)
->setDescription('Send Delivery Report to SeQura');

parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$codeKeys = $input->getArgument(self::INPUT_KEY_SHOPCODES);
$output->write('Trigger Delivery Report for ');
if(count($codeKeys)<1){
$codeKeys[0] = false;
$output->writeln('all shops');
} else {
$output->writeln(implode(',',$codeKeys));
}
foreach ($codeKeys as $codeKey){
if ($results = $this->_reporter->sendOrderWithShipment($codeKey)) {
$output->writeln('Ok, report Sent!');
foreach($results as $key => $value){
$output->writeln($key . ' => ' . $value . ' orders sent');
}
return;
}
$output->writeln('Ko, report was not sent!');
}
}
}
12 changes: 11 additions & 1 deletion Model/Reporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,22 @@ public function __construct(
/*
* @return: int orders sent
*/
public function sendOrderWithShipment()
public function sendOrderWithShipment($codeKey = false)
{
$ret = array();
$stores = $this->_storeManager->getStores($withDefault = false);
$builder = $this->_builder;
/* @todo: Understand why without the echo executing from console gives
[Magento\Framework\Exception\SessionException]
Area code not set: Area code must be set before starting a session.
*/
echo " ";
//

foreach ($stores as $store) {
if($codeKey && $store->getCode()!==$codeKey){
continue;
}
$client = new \Sequra\PhpClient\Client(
$this->_config->getCoreValue('user_name'),
$this->_config->getCoreValue('user_secret'),
Expand Down
9 changes: 9 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@
</virtualType>

<preference for="Sequra\Core\Api\SubmissionInterface" type="Sequra\Core\Model\Service\SubmissionService"/>

<!-- Commandline option to triggerrerport -->
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="triggerReport" xsi:type="object">Sequra\Core\Console\TriggerReport</item>
</argument>
</arguments>
</type>
</config>

0 comments on commit 7423f10

Please sign in to comment.