Files::copyDirectory()   B
last analyzed

Complexity

Conditions 11
Paths 48

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 11
eloc 22
c 3
b 0
f 0
nc 48
nop 3
dl 0
loc 43
rs 7.3166

How to fix   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
 * Created by PhpStorm.
4
 * User: joshgulledge
5
 * Date: 3/13/18
6
 * Time: 1:11 PM
7
 */
8
9
namespace LCI\Blend\Helpers;
10
11
use FilesystemIterator;
12
use RecursiveDirectoryIterator;
13
use RecursiveIteratorIterator;
14
use Symfony\Component\Console\Helper\ProgressBar;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
trait Files
18
{
19
    /** @var string  */
20
    protected $mode = '0700';
21
22
    /**
23
     * @return string
24
     */
25
    public function getMode(): string
26
    {
27
        return $this->mode;
28
    }
29
30
    /**
31
     * @param string $mode
32
     * @return $this
33
     */
34
    public function setMode(string $mode)
35
    {
36
        $this->mode = $mode;
37
        return $this;
38
    }
39
40
    /**
41
     * @param string $source ~ full path of source
42
     * @param string $destination ~ full path of destination
43
     * @param int $file_count
44
     */
45
    public function copyDirectory($source, $destination, $file_count = 4000)
46
    {
47
        $destination = rtrim($destination, '\/\\');
48
        if (!is_dir($destination)) {
49
            $this->makeDirectory($destination);
50
        }
51
52
        /** @var \RecursiveDirectoryIterator $directoryIterator */
53
        $directoryIterator = new RecursiveDirectoryIterator($source, FilesystemIterator::SKIP_DOTS);
54
55
        if (isset($this->output) && $this->output instanceof OutputInterface) {
56
            $this->output->writeln('Now copying extracted files to '.$destination.' File count: '.$file_count);
57
            $progress = new ProgressBar($this->output, $file_count);
58
            $progress->start();
59
60
            $progress->setRedrawFrequency(10);
61
        }
62
63
        /** @var RecursiveIteratorIterator $recursiveIteratorIterator */
64
        $recursiveIteratorIterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST);
65
66
        /** @var \DirectoryIterator $item */
67
        foreach ($recursiveIteratorIterator as $item) {
68
            if ($item->isDir()) {
69
                if (is_dir($destination.DIRECTORY_SEPARATOR.$recursiveIteratorIterator->getSubPathName())) {
70
                    continue;
71
                }
72
                $this->makeDirectory($destination.DIRECTORY_SEPARATOR.$recursiveIteratorIterator->getSubPathName());
73
74
            } else {
75
                copy($item, $destination.DIRECTORY_SEPARATOR.$recursiveIteratorIterator->getSubPathName());
76
            }
77
78
            if (isset($progress) && $progress instanceof ProgressBar) {
79
                $progress->advance();
80
            }
81
82
        }
83
84
        if (isset($progress) && $progress instanceof ProgressBar) {
85
            // ensures that the progress bar is at 100%
86
            $progress->finish();
87
            $this->output->writeln('');
88
        }
89
90
    }
91
92
    /**
93
     * @param string $directory
94
     * @return bool
95
     */
96
    public function deleteDirectory($directory)
97
    {
98
        $directory = rtrim($directory, '\/\\');
99
        if (!empty($directory) && file_exists($directory)) {
100
101
            /** @var \RecursiveDirectoryIterator $directoryIterator */
102
            $directoryIterator = new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS);
103
104
            /** @var RecursiveIteratorIterator $recursiveIteratorIterator */
105
            $recursiveIteratorIterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::CHILD_FIRST);
106
107
            /** @var  $file */
108
            foreach ($recursiveIteratorIterator as $file) {
109
                $file->isDir() ? rmdir($file) : unlink($file);
110
            }
111
112
            return rmdir($directory);
113
        }
114
115
        return false;
116
    }
117
118
    /**
119
     * @param $directory
120
     * @return bool
121
     */
122
    protected function makeDirectory($directory)
123
    {
124
        $created = file_exists($directory);
125
        if (!$created && $created = mkdir($directory)) {
126
            chmod($directory, $this->mode);
127
        }
128
129
        return $created;
130
    }
131
132
}