Completed
Push — master ( f3f979...855d01 )
by Tim
10s
created

ImportCreateOkFileCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 91
Duplicated Lines 29.67 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 3
dl 27
loc 91
ccs 0
cts 50
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B configure() 27 27 1
B executeSimpleCommand() 0 43 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
0 ignored issues
show
Coding Style introduced by
Blank line found at end of control structure
Loading history...
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