Skip to content

Commit 35de3a5

Browse files
Merge pull request #68 from WeareJH/bugfix/#67-issue-when-running-jh-import-list-command
Handling a case when cron schedule is kept in configuration
2 parents f7caa61 + fcd18bb commit 35de3a5

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

src/Command/ListImportsCommand.php

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Jh\Import\Command;
44

5+
use Jh\Import\Config as ImportConfig;
56
use Jh\Import\Config\Data;
67
use Jh\Import\Locker\Locker;
78
use Magento\Cron\Model\Config;
9+
use Magento\Framework\App\Config\ScopeConfigInterface;
810
use Magento\Framework\Console\Cli;
911
use Symfony\Component\Console\Command\Command;
1012
use Symfony\Component\Console\Helper\Table;
@@ -31,12 +33,18 @@ class ListImportsCommand extends Command
3133
*/
3234
private $locker;
3335

34-
public function __construct(Data $importConfig, Config $cronConfig, Locker $locker)
36+
/**
37+
* @var ScopeConfigInterface
38+
*/
39+
private $scopeConfig;
40+
41+
public function __construct(Data $importConfig, Config $cronConfig, Locker $locker, ScopeConfigInterface $scopeConfig)
3542
{
43+
parent::__construct();
3644
$this->importConfig = $importConfig;
3745
$this->cronConfig = $cronConfig;
3846
$this->locker = $locker;
39-
parent::__construct();
47+
$this->scopeConfig = $scopeConfig;
4048
}
4149

4250
protected function configure()
@@ -60,19 +68,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
6068
->setHeaders(['Name', 'Type', 'Match Files', 'Incoming Directory', 'Cron Expr', 'Locked?'])
6169
->setRows(array_map(function ($import) use ($jobs) {
6270
$config = $this->importConfig->getImportConfigByName($import);
63-
64-
if ($config->hasCron() && isset($jobs[$config->getCronGroup()][$config->getCron()])) {
65-
$cron = $jobs[$config->getCronGroup()][$config->getCron()]['schedule'];
66-
} else {
67-
$cron = 'N/A';
68-
}
71+
$schedule = $this->resolveSchedule($jobs, $config);
6972

7073
return [
7174
$config->getImportName(),
7275
$config->getType(),
7376
$config->get('match_files'),
7477
$config->get('incoming_directory'),
75-
$cron,
78+
$schedule,
7679
$this->locker->locked($import) ? '<error>Yes</error>' : 'No'
7780
];
7881
}, $this->importConfig->getAllImportNames()))
@@ -82,4 +85,25 @@ protected function execute(InputInterface $input, OutputInterface $output)
8285

8386
return Cli::RETURN_SUCCESS;
8487
}
88+
89+
private function resolveSchedule(array $jobs, ImportConfig $importConfig): string
90+
{
91+
$unknownSchedule = 'N/A';
92+
93+
if (!$importConfig->hasCron() || !isset($jobs[$importConfig->getCronGroup()][$importConfig->getCron()])) {
94+
return $unknownSchedule;
95+
}
96+
97+
$cronConfiguration = $jobs[$importConfig->getCronGroup()][$importConfig->getCron()];
98+
99+
if (isset($cronConfiguration['schedule'])) {
100+
return $cronConfiguration['schedule'];
101+
}
102+
103+
if (isset($cronConfiguration['config_path'])) {
104+
return (string) $this->scopeConfig->getValue($cronConfiguration['config_path']);
105+
}
106+
107+
return $unknownSchedule;
108+
}
85109
}

0 commit comments

Comments
 (0)