Completed
Pull Request — master (#51)
by Tim
09:44
created

MissingOptionValuesPlugin::process()   D

Complexity

Conditions 10
Paths 24

Size

Total Lines 96
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 96
c 0
b 0
f 0
ccs 0
cts 59
cp 0
rs 4.9494
cc 10
eloc 45
nc 24
nop 0
crap 110

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\Plugins\MissingOptionValuesPlugin
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Plugins;
22
23
use TechDivision\Import\Utils\ColumnKeys;
24
use TechDivision\Import\Utils\RegistryKeys;
25
use TechDivision\Import\Utils\ExporterTrait;
26
use TechDivision\Import\Utils\SwiftMailerKeys;
27
28
/**
29
 * Plugin that exports the missing option values to a CSV file.
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
35
 * @link      http://www.techdivision.com
36
 */
37
class MissingOptionValuesPlugin extends AbstractPlugin
38
{
39
40
    /**
41
     * The exporter trait implementation.
42
     *
43
     * @var \TechDivision\Import\Utils\ExporterTrait
44
     */
45
    use ExporterTrait;
46
47
    /**
48
     * Process the plugin functionality.
49
     *
50
     * @return void
51
     * @throws \Exception Is thrown, if the plugin can not be processed
52
     */
53
    public function process()
54
    {
55
56
        // query whether or not, the debug mode has been enabled
57
        if (!$this->getConfiguration()->isDebugMode()) {
58
            $this->getSystemLogger()->info('Debug mode is not enabled, missing option values will not be exported');
59
            return;
60
        }
61
62
        // clear the filecache
63
        clearstatcache();
64
65
        // load the actual status
66
        $status = $this->getRegistryProcessor()->getAttribute($this->getSerial());
67
68
        // query whether or not the configured source directory is available
69
        if (!is_dir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) {
70
            throw new \Exception(sprintf('Configured source directory %s is not available!', $sourceDir));
71
        }
72
73
        // load the array with the missing option values from the status
74
        $missingOptions = $status[RegistryKeys::MISSING_OPTION_VALUES];
75
76
        // if missing option values are found, return immediately
77
        if (sizeof($missingOptions) === 0) {
78
            $this->getSystemLogger()->info('Found no missing option values');
79
            return;
80
        }
81
82
        // intialize array for the missing option values with the CSV headers
83
        $toBeCreated = array(
84
            array(
85
                ColumnKeys::STORE_VIEW_CODE,
86
                ColumnKeys::ATTRIBUTE_CODE,
87
                ColumnKeys::VALUE,
88
                ColumnKeys::SORT_ORDER
89
            )
90
        );
91
92
        // append the missing option values to the array
93
        foreach ($missingOptions as $attributeCode => $options) {
94
            foreach (array_keys($options) as $value) {
95
                $toBeCreated[] = array(null, $attributeCode, $value, null);
96
            }
97
        }
98
99
        // prepare the filename and export the missing options as CSV file
100
        $filename = sprintf('%s/missing-option-values.csv', $sourceDir);
101
        $this->getExporterInstance()->export($filename, $toBeCreated);
102
103
        // query whether or not a swift mailer has been registered
104
        if ($swiftMailer = $this->getSwiftMailer()) {
105
            // the swift mailer configuration
106
            $swiftMailerConfiguration = $this->getPluginConfiguration()->getSwiftMailer();
0 ignored issues
show
Bug introduced by
The method getSwiftMailer() does not seem to exist on object<TechDivision\Impo...ConfigurationInterface>.

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...
107
108
            // create the message with the CSV with the missing option values
109
            $message = $swiftMailer->createMessage()
110
                                   ->setSubject($swiftMailerConfiguration->getParam(SwiftMailerKeys::SUBJECT))
111
                                   ->setFrom($swiftMailerConfiguration->getParam(SwiftMailerKeys::FROM))
112
                                   ->setTo($to = $swiftMailerConfiguration->getParam(SwiftMailerKeys::TO))
113
                                   ->setBody('The attached CSV file contains the missing attribute option values')
114
                                   ->attach(\Swift_Attachment::fromPath($filename));
115
116
            // initialize the array with the failed recipients
117
            $failedRecipients = array();
118
            $recipientsAccepted = 0;
0 ignored issues
show
Unused Code introduced by
$recipientsAccepted is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
119
120
            // send the mail
121
            $recipientsAccepted = $swiftMailer->send($message, $failedRecipients);
122
123
            // query whether or not all recipients have been accepted
124
            if (sizeof($failedRecipients) > 0) {
125
                $this->getSystemLogger()->error(sprintf('Can\'t send mail to %s', implode(', ', $failedRecipients)));
126
            }
127
128
            // if at least one recipient has been accepted
129
            if ($recipientsAccepted > 0) {
130
                // cast 'to' into an array if not already
131
                is_array($to) ? : $to = (array) $to;
132
                // remove the NOT accepted recipients
133
                $acceptedRecipients = array_diff($to, $failedRecipients);
134
135
                // log a message with the successfull receivers
136
                $this->getSystemLogger()->info(
137
                    sprintf(
138
                        'Mail successfully sent to %d recipient(s) (%s)',
139
                        $recipientsAccepted,
140
                        implode(', ', $acceptedRecipients)
141
                    )
142
                );
143
            }
144
        }
145
146
        // and and log a message that the missing option values has been exported
147
        $this->getSystemLogger()->info(sprintf('Successfully exported missing option values to file %s!', $filename));
148
    }
149
}
150