Completed
Push — master ( 9cebd2...cdc5f6 )
by
unknown
10:00 queued 09:59
created

DebugCreatePlugin::process()   B

Complexity

Conditions 10
Paths 26

Size

Total Lines 87
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 39
nc 26
nop 0
dl 0
loc 87
ccs 0
cts 33
cp 0
crap 110
rs 7.6666
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * TechDivision\Import\Cli\Plugins\DebugCreatePlugin
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2020 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-cli
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Cli\Plugins;
16
17
use TechDivision\Import\Utils\RegistryKeys;
18
use TechDivision\Import\ApplicationInterface;
19
use TechDivision\Import\Utils\DebugUtilInterface;
20
use Symfony\Component\Console\Question\ChoiceQuestion;
21
use TechDivision\Import\Utils\InputOptionKeysInterface;
22
23
/**
24
 * Plugin that creates and sends a debug report via email.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2020 TechDivision GmbH <[email protected]>
28
 * @license   https://opensource.org/licenses/MIT
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
class DebugCreatePlugin extends AbstractConsolePlugin
33
{
34
35
    /**
36
     * The debug util instance.
37
     *
38
     * @var \TechDivision\Import\Utils\DebugUtilInterface
39
     */
40
    private $debugUtil;
41
42
    /**
43
     * The constructor to initialize the plugin with.
44
     *
45
     * @param \TechDivision\Import\ApplicationInterface     $application The application instance
46
     * @param \TechDivision\Import\Utils\DebugUtilInterface $debugUtil   The debug util instance
47
     */
48
    public function __construct(ApplicationInterface $application, DebugUtilInterface $debugUtil)
49
    {
50
51
        // set the debug utility
52
        $this->debugUtil = $debugUtil;
53
54
        // pass the application to the parent constructor
55
        parent::__construct($application);
56
    }
57
58
59
    /**
60
     * Return's the debug util instance.
61
     *
62
     * @return \TechDivision\Import\Utils\DebugUtilInterface The debug util instance
63
     */
64
    private function getDebugUtil() : DebugUtilInterface
65
    {
66
        return $this->debugUtil;
67
    }
68
69
    /**
70
     * Process the plugin functionality.
71
     *
72
     * @return void
73
     * @throws \InvalidArgumentException Is thrown if either the directory nor a artefact for the given serial is available
74
     */
75
    public function process()
76
    {
77
        if ($this->getConfiguration()->isConfigOutput()) {
0 ignored issues
show
Bug introduced by
The method isConfigOutput() does not exist on TechDivision\Import\Conf...\ConfigurationInterface. ( Ignorable by Annotation )

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

77
        if ($this->getConfiguration()->/** @scrutinizer ignore-call */ isConfigOutput()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
78
            $configurationFiles = $this->getConfigurationFiles();
79
            $this->getSystemLogger()->info(
80
                print_r($configurationFiles, true)
0 ignored issues
show
Bug introduced by
It seems like print_r($configurationFiles, true) can also be of type true; however, parameter $message of Psr\Log\LoggerInterface::info() does only seem to accept string, 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
                /** @scrutinizer ignore-type */ print_r($configurationFiles, true)
Loading history...
81
            );
82
            return;
83
        }
84
85
        // load the actual status
86
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
87
88
        // query whether or not the configured source directory is available
89
        if (isset($status[RegistryKeys::SOURCE_DIRECTORY])) {
90
            $sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY];
91
        } else {
92
            throw new \Exception('Source directory is not available!');
93
        }
94
95
        // try to load the archive directory
96
        $archiveDir = $this->getConfiguration()->getArchiveDir();
97
98
        // try to initialize a default archive directory by concatenating 'archive' to the target directory
99
        if ($archiveDir === null) {
0 ignored issues
show
introduced by
The condition $archiveDir === null is always false.
Loading history...
100
            $archiveDir = sprintf('var/import_history');
101
        }
102
103
        // retrieve the question helper
104
        $questionHelper = $this->getHelper('question');
105
106
        // initialize the array for the available serials
107
        $availableSerials = array();
108
109
        // load the directories from the actual working directory (broken imports, etc.)
110
        foreach (glob(sprintf('%s/*', $sourceDir), GLOB_ONLYDIR) as $possibleImportDir) {
111
            $availableSerials[basename($possibleImportDir)] = filemtime($possibleImportDir);
112
        }
113
114
        // load the ZIP artefacts from the archive directory
115
        foreach (glob(sprintf('%s/*.zip', $archiveDir)) as $possibleArtefact) {
116
            $availableSerials[ basename($possibleArtefact, '.zip')] = filemtime($possibleArtefact);
117
        }
118
119
        // sort the available serials by modification time
120
        uasort($availableSerials, function ($a, $b) {
121
            // return zero, if the passed values are equal
122
            if ($a == $b) {
123
                return 0;
124
            }
125
            // otherwise return -1 or 1
126
            return ($a > $b) ? -1 : 1;
127
        });
128
129
        // finally create the array with the available serials to render on the console
130
        $availableSerials = array_slice(array_keys($availableSerials), 0, $this->getInput()->getOption(InputOptionKeysInterface::RENDER_DEBUG_SERIALS));
131
132
        // this is, when the import:debug send command has been invoked
133
        // WITHOUT the --serial=<UUID> parameter or an invalid serial
134
        if (!in_array($serial = $this->getSerial(), $availableSerials, true)) {
135
            // create the question instance to choose the serial
136
            $chooseSerialQuestion = new ChoiceQuestion('Please select the serial to create the debug artefact for', $availableSerials);
137
            $chooseSerialQuestion->setErrorMessage('Selected serial "%s" is invalid.');
138
139
            // abort the operation if the user does not confirm with 'y' or enter
140
            if (!$serial = $questionHelper->ask($this->getInput(), $this->getOutput(), $chooseSerialQuestion)) {
0 ignored issues
show
Bug introduced by
The method ask() does not exist on Symfony\Component\Console\Helper\Helper. It seems like you code against a sub-type of Symfony\Component\Console\Helper\Helper such as Symfony\Component\Console\Helper\QuestionHelper. ( Ignorable by Annotation )

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

140
            if (!$serial = $questionHelper->/** @scrutinizer ignore-call */ ask($this->getInput(), $this->getOutput(), $chooseSerialQuestion)) {
Loading history...
141
                $this->getOutput()->writeln('<info>Aborting operation - debug report has NOT been sent.</info>');
142
                return;
143
            }
144
        }
145
146
        // update the registry with the (probably new) serial and the source directory
147
        $this->getRegistryProcessor()->mergeAttributesRecursive(
148
            RegistryKeys::STATUS,
149
            array(
150
                RegistryKeys::DEBUG_SERIAL     => $serial,
151
                RegistryKeys::SOURCE_DIRECTORY => sprintf('%s/%s', $sourceDir, $serial)
152
            )
153
        );
154
155
        // prepare the debug dump
156
        $this->getDebugUtil()->extractArchive($serial);
157
        $this->getDebugUtil()->prepareDump($serial);
158
        $dumpFilename = $this->getDebugUtil()->createDump($serial);
159
160
        // write a success message to the console
161
        $this->getOutput()->writeln(sprintf('<info>Successfully created debug dump "%s"</info>', $dumpFilename));
162
    }
163
164
    /**
165
     * @return array
166
     */
167
    public function getConfigurationFiles()
168
    {
169
        return $this->getConfiguration()->getConfigurationFiles();
0 ignored issues
show
Bug introduced by
The method getConfigurationFiles() does not exist on TechDivision\Import\Conf...\ConfigurationInterface. ( Ignorable by Annotation )

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

169
        return $this->getConfiguration()->/** @scrutinizer ignore-call */ getConfigurationFiles();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
170
    }
171
}
172