Completed
Push — master ( 1235b0...90fadd )
by Tim
15s
created

ImportCreateOkFileCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
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\Utils\CommandNames;
24
use TechDivision\Import\ConfigurationInterface;
25
use Symfony\Component\Console\Input\InputInterface;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
/**
29
 * The  command implementation that creates a OK file from a directory with CSV files.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2016 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/techdivision/import-cli-simple
35
 * @link      http://www.techdivision.com
36
 */
37
class ImportCreateOkFileCommand extends AbstractSimpleImportCommand
38
{
39
40
    /**
41
     * Configures the current command.
42
     *
43
     * @return void
44
     * @see \Symfony\Component\Console\Command\Command::configure()
45
     */
46
    protected function configure()
47
    {
48
49
        // initialize the command with the required/optional options
50
        $this->setName(CommandNames::IMPORT_CREATE_OK_FILE)
51
             ->setDescription('Create\'s the OK file for the CSV files of the configured source directory');
52
53
        // invoke the parent method
54
        parent::configure();
55
    }
56
57
58
    /**
59
     * Finally executes the simple command.
60
     *
61
     * @param \TechDivision\Import\ConfigurationInterface       $configuration The configuration instance
62
     * @param \Symfony\Component\Console\Input\InputInterface   $input         An InputInterface instance
63
     * @param \Symfony\Component\Console\Output\OutputInterface $output        An OutputInterface instance
64
     *
65
     * @return void
66
     */
67
    protected function executeSimpleCommand(
68
        ConfigurationInterface $configuration,
69
        InputInterface $input,
70
        OutputInterface $output
71
    ) {
72
73
        // load the source directory
74
        $sourceDir = $configuration->getSourceDir();
75
76
        /** @var TechDivision\Import\Configuration\PluginConfigurationInterface $plugin */
77
        foreach ($configuration->getPlugins() as $plugin) {
78
            /** @var TechDivision\Import\Configuration\SubjectConfigurationInterface $subject */
79
            foreach ($plugin->getSubjects() as $subject) {
80
                // query whether or not, an OK file is needed
81
                if ($subject->isOkFileNeeded()) {
82
                    // load the prefix
83
                    $prefix = $subject->getPrefix();
84
85
                    // load the CSVfiles from the source directory
86
                    $csvFiles = glob(sprintf('%s/%s*.csv', $sourceDir, $prefix));
87
88
                    // query whether or not any CSV files are available
89
                    if (sizeof($csvFiles) > 0) {
90
                        // prepare the OK file's content
91
                        $okfileContent = '';
92
                        foreach ($csvFiles as $filename) {
93
                            $okfileContent .= basename($filename) . PHP_EOL;
94
                        }
95
96
                        // prepare the OK file's name
97
                        $okFilename = sprintf('%s/%s.ok', $sourceDir, $prefix);
98
99
                        // write the OK file
100
                        if (file_put_contents($okFilename, $okfileContent)) {
101
                            // write a message to the console
102
                            $output->writeln(sprintf('<info>Successfully written OK file %s</info>', $okFilename));
103
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
104
                        } else {
105
                            // write a message to the console
106
                            $output->writeln(sprintf('<error>Can\'t write OK file %s</error>', $okFilename));
107
                        }
108
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
109
                    } else {
110
                        // write a message to the console
111
                        $output->writeln(sprintf('<error>Can\'t find any CSV files in source directory %s</error>', $sourceDir));
112
                    }
113
114
                    // stop the process
115
                    return;
116
                }
117
            }
118
        }
119
    }
120
}
121