Completed
Push — 8.x ( 88dc80...fdde97 )
by Tim
09:26
created

ImportCreateOkFileCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 8
ccs 0
cts 3
cp 0
crap 2
rs 10
c 1
b 0
f 0
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
use TechDivision\Import\Cli\Utils\DependencyInjectionKeys;
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
    protected function configure()
48
    {
49
50
        // initialize the command with the required/optional options
51
        $this->setName(CommandNames::IMPORT_CREATE_OK_FILE)
52
             ->setDescription('Create\'s the OK file for the CSV files of the configured source directory');
53
54
        // invoke the parent method
55
        parent::configure();
56
    }
57
58
    /**
59
     * Executes the current command.
60
     *
61
     * This method is not abstract because you can use this class
62
     * as a concrete class. In this case, instead of defining the
63
     * execute() method, you set the code to execute by passing
64
     * a Closure to the setCode() method.
65
     *
66
     * @param \Symfony\Component\Console\Input\InputInterface   $input  An InputInterface instance
67
     * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance
68
     *
69
     * @return null|int null or 0 if everything went fine, or an error code
70
     * @throws \LogicException When this abstract method is not implemented
71
     * @see \Symfony\Component\Console\Command\Command::execute()
72
     */
73
    protected function execute(InputInterface $input, OutputInterface $output)
74
    {
75
76
        // initialize the configuration instance
77
        $configuration = $this->getContainer()->get(DependencyInjectionKeys::CONFIGURATION_SIMPLE);
78
79
        // finally execute the simple command
80
        return $this->executeSimpleCommand($configuration, $input, $output);
0 ignored issues
show
Bug introduced by
It seems like $configuration can also be of type null; however, parameter $configuration of TechDivision\Import\Cli\...:executeSimpleCommand() does only seem to accept TechDivision\Import\ConfigurationInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

80
        return $this->executeSimpleCommand(/** @scrutinizer ignore-type */ $configuration, $input, $output);
Loading history...
Bug Best Practice introduced by
The expression return $this->executeSim...ation, $input, $output) returns the type void which is incompatible with the documented return type integer|null.
Loading history...
Bug introduced by
Are you sure the usage of $this->executeSimpleComm...ation, $input, $output) targeting TechDivision\Import\Cli\...:executeSimpleCommand() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
81
    }
82
83
84
    /**
85
     * Finally executes the simple command.
86
     *
87
     * @param \TechDivision\Import\ConfigurationInterface       $configuration The configuration instance
88
     * @param \Symfony\Component\Console\Input\InputInterface   $input         An InputInterface instance
89
     * @param \Symfony\Component\Console\Output\OutputInterface $output        An OutputInterface instance
90
     *
91
     * @return void
92
     */
93
    protected function executeSimpleCommand(
94
        ConfigurationInterface $configuration,
95
        InputInterface $input,
96
        OutputInterface $output
97
    ) {
98
99
        // load the source directory, ALWAYS remove the directory separator, if appended
100
        $sourceDir = rtrim($configuration->getSourceDir(), DIRECTORY_SEPARATOR);
101
102
        // load the array with the unique prefixes
103
        $prefixes = $configuration->getPrefixes();
104
105
        // sort the prefixes
106
        usort($prefixes, function ($a, $b) {
107
            return strcmp($a, $b);
108
        });
109
110
        // initialize the counter for the CSV files
111
        $csvFilesFound = 0;
112
113
        // iterate over the prefixes and create the .ok files
114
        foreach ($prefixes as $prefix) {
115
            // load the CSVfiles from the source directory
116
            $csvFiles = glob(sprintf('%s/%s_*.csv', $sourceDir, $prefix));
117
            // raise the counter for the CSV files we've found
118
            $csvFilesFound += sizeof($csvFiles);
0 ignored issues
show
Bug introduced by
It seems like $csvFiles can also be of type false; however, parameter $var of sizeof() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
            $csvFilesFound += sizeof(/** @scrutinizer ignore-type */ $csvFiles);
Loading history...
119
            // query whether or not any CSV files are available
120
            if (sizeof($csvFiles) > 0) {
121
                // prepare the OK file's content
122
                $okfileContent = '';
123
                foreach ($csvFiles as $filename) {
124
                    $okfileContent .= basename($filename) . PHP_EOL;
125
                }
126
127
                // prepare the OK file's name
128
                $okFilename = sprintf('%s/%s.ok', $sourceDir, $prefix);
129
130
                // write the OK file
131
                if (file_put_contents($okFilename, $okfileContent)) {
132
                    // write a message to the console
133
                    $output->writeln(sprintf('<info>Successfully written OK file %s</info>', $okFilename));
134
                } else {
135
                    // write a message to the console
136
                    $output->writeln(sprintf('<error>Can\'t write OK file %s</error>', $okFilename));
137
                }
138
            }
139
        }
140
141
        // query whether or not we've found any CSV files
142
        if ($csvFilesFound === 0) {
0 ignored issues
show
introduced by
The condition $csvFilesFound === 0 is always true.
Loading history...
143
            // write a message to the console, if we can't find any CSV files
144
            $output->writeln(sprintf('<error>Can\'t find any CSV files in source directory %s</error>', $sourceDir));
145
            // return 1 to signal an error
146
            return 1;
0 ignored issues
show
Bug Best Practice introduced by
The expression return 1 returns the type integer which is incompatible with the documented return type void.
Loading history...
147
        }
148
149
        // return 0 to signal success
150
        return 0;
151
    }
152
}
153