1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TechDivision\Import\Cli\Command\ImportCreateOkFileCommand |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
18
|
|
|
* @link http://www.techdivision.com |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Cli\Command; |
22
|
|
|
|
23
|
|
|
use TechDivision\Import\Cli\Configuration; |
24
|
|
|
use TechDivision\Import\ConfigurationInterface; |
25
|
|
|
use Symfony\Component\Console\Input\InputOption; |
26
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
27
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The command implementation that creates a OK file from a directory with CSV files. |
31
|
|
|
* |
32
|
|
|
* @author Tim Wagner <[email protected]> |
33
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
34
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
35
|
|
|
* @link https://github.com/techdivision/import-cli-simple |
36
|
|
|
* @link http://www.techdivision.com |
37
|
|
|
*/ |
38
|
|
|
class ImportCreateOkFileCommand extends AbstractSimpleImportCommand |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Configures the current command. |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
* @see \Symfony\Component\Console\Command\Command::configure() |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
protected function configure() |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
|
50
|
|
|
// initialize the command with the required/optional options |
51
|
|
|
$this->setName('import:create:ok-file') |
52
|
|
|
->setDescription('Create\'s the OK file for the CSV files of the configured source directory') |
53
|
|
|
->addOption( |
54
|
|
|
InputOptionKeys::CONFIGURATION, |
55
|
|
|
null, |
56
|
|
|
InputOption::VALUE_REQUIRED, |
57
|
|
|
'Specify the pathname to the configuration file to use', |
58
|
|
|
sprintf('%s/techdivision-import.json', getcwd()) |
59
|
|
|
) |
60
|
|
|
->addOption( |
61
|
|
|
InputOptionKeys::LOG_LEVEL, |
62
|
|
|
null, |
63
|
|
|
InputOption::VALUE_REQUIRED, |
64
|
|
|
'The log level to use' |
65
|
|
|
) |
66
|
|
|
->addOption( |
67
|
|
|
InputOptionKeys::PID_FILENAME, |
68
|
|
|
null, |
69
|
|
|
InputOption::VALUE_REQUIRED, |
70
|
|
|
'The explicit PID filename to use', |
71
|
|
|
sprintf('%s/%s', sys_get_temp_dir(), Configuration::PID_FILENAME) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Finally executes the simple command. |
78
|
|
|
* |
79
|
|
|
* @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance |
80
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
81
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
82
|
|
|
* |
83
|
|
|
* @return void |
84
|
|
|
*/ |
85
|
|
|
protected function executeSimpleCommand( |
86
|
|
|
ConfigurationInterface $configuration, |
87
|
|
|
InputInterface $input, |
88
|
|
|
OutputInterface $output |
89
|
|
|
) { |
90
|
|
|
|
91
|
|
|
// load the source directory |
92
|
|
|
$sourceDir = $configuration->getSourceDir(); |
93
|
|
|
|
94
|
|
|
/** @var TechDivision\Import\Configuration\PluginConfigurationInterface $plugin */ |
95
|
|
|
foreach ($configuration->getPlugins() as $plugin) { |
96
|
|
|
/** @var TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */ |
97
|
|
|
foreach ($plugin->getSubjects() as $subject) { |
98
|
|
|
// query whether or not, an OK file is needed |
99
|
|
|
if ($subject->isOkFileNeeded()) { |
100
|
|
|
// load the prefix |
101
|
|
|
$prefix = $subject->getPrefix(); |
102
|
|
|
|
103
|
|
|
// prepare the OK file's content |
104
|
|
|
$okfileContent = ''; |
105
|
|
|
foreach (glob(sprintf('%s/%s*.csv', $sourceDir, $prefix)) as $filename) { |
106
|
|
|
$okfileContent .= basename($filename) . PHP_EOL; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// prepare the OK file's name |
110
|
|
|
$okFilename = sprintf('%s/%s.ok', $sourceDir, $prefix); |
111
|
|
|
|
112
|
|
|
// write the OK file |
113
|
|
|
if (file_put_contents($okFilename, $okfileContent)) { |
114
|
|
|
// write a message to the console |
115
|
|
|
$output->writeln(sprintf('<info>Successfully written OK file %s</info>', $okFilename)); |
116
|
|
|
|
|
|
|
|
117
|
|
|
} else { |
118
|
|
|
// write a message to the console |
119
|
|
|
$output->writeln(sprintf('<error>Can\'t write OK file %s</error>', $okFilename)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
// stop the process |
123
|
|
|
return; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.