Completed
Pull Request — master (#78)
by Tim
02:59
created

ArchivePlugin::process()   C

Complexity

Conditions 9
Paths 17

Size

Total Lines 80
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 80
ccs 0
cts 43
cp 0
rs 5.6205
cc 9
eloc 33
nc 17
nop 0
crap 90

How to fix   Long Method   

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\ArchivePlugin
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\RegistryKeys;
24
25
/**
26
 * Plugin that archives the artefacts.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
class ArchivePlugin extends AbstractPlugin
35
{
36
37
    /**
38
     * Process the plugin functionality.
39
     *
40
     * @return void
41
     * @throws \Exception Is thrown, if the plugin can not be processed
42
     */
43
    public function process()
44
    {
45
46
        // query whether or not, the import artefacts have to be archived
47
        if (!$this->getConfiguration()->haveArchiveArtefacts()) {
48
            $this->getSystemLogger()->info('Archiving functionality has not been activated');
49
            return;
50
        }
51
52
        // load the actual status
53
        $status = $this->getRegistryProcessor()->getAttribute($this->getSerial());
54
55
        // load the number of imported bunches from the status
56
        $bunches = $status[RegistryKeys::BUNCHES];
57
58
        // if no files have been imported, return immediately
59
        if ($bunches === 0) {
60
            $this->getSystemLogger()->info('Found no files to archive');
61
            return;
62
        }
63
64
        // clear the filecache
65
        clearstatcache();
66
67
        // query whether or not the configured source directory is available
68
        if (!is_dir($sourceDir = $status[RegistryKeys::SOURCE_DIRECTORY])) {
69
            throw new \Exception(sprintf('Configured source directory %s is not available!', $sourceDir));
70
        }
71
72
        // init file iterator on source directory
73
        $fileIterator = new \FilesystemIterator($sourceDir);
74
75
        // log the number of files that has to be archived
76
        $this->getSystemLogger()->info(sprintf('Found %d files to archive in directory %s', $bunches, $sourceDir));
77
78
        // try to load the archive directory
79
        $archiveDir = $this->getConfiguration()->getArchiveDir();
80
81
        // query whether or not the specified archive directory already exists
82
        if ($archiveDir === null) {
83
            // try to initialize a default archive directory by concatenating 'archive' to the target directory
84
            $archiveDir = sprintf('%s/archive', $this->getConfiguration()->getTargetDir());
85
        }
86
87
        // query whether or not the archive directory already exists
88
        if (!is_dir($archiveDir)) {
89
            // create the archive directory if possible
90
            if (mkdir($archiveDir, 0755, true)) {
91
                $this->getSystemLogger()->info(sprintf('Successfully created archived archive directory %s', $archiveDir));
92
            } else {
93
                // initialize the message that the archive directory can not be created
94
                $message = sprintf('Can\'t create archive directory %s', $archiveDir);
95
96
                // log a message if we're in debug mode, else throw an exception
97
                if ($this->getConfiguration()->isDebugMode()) {
98
                    $this->getSystemLogger()->error($message);
99
                } else {
100
                    throw new \Exception($message);
101
                }
102
            }
103
        }
104
105
        // create the ZIP archive
106
        $archive = new \ZipArchive();
107
        $archive->open($archiveFile = sprintf('%s/%s.zip', $archiveDir, $this->getSerial()), \ZipArchive::CREATE);
108
109
        // iterate through all files and add them to the ZIP archive
110
        foreach ($fileIterator as $filename) {
111
            $archive->addFile($filename);
112
        }
113
114
        // save the ZIP archive
115
        $archive->close();
116
117
        // finally remove the directory with the imported files
118
        $this->removeDir($sourceDir);
119
120
        // and and log a message that the import artefacts have been archived
121
        $this->getSystemLogger()->info(sprintf('Successfully archived imported files to %s!', $archiveFile));
122
    }
123
}
124