CopyFilter::checkRequirements()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace JK\Sam\Filter\Copy;
4
5
use Exception;
6
use JK\Sam\Filter\Filter;
7
use SplFileInfo;
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class CopyFilter extends Filter
11
{
12
    /**
13
     * Copy the sources files to the destinations.
14
     *
15
     * @param SplFileInfo[] $sources
16
     * @param string[] $destinations
17
     *
18
     * @return SplFileInfo[]
19
     *
20
     * @throws Exception
21
     */
22
    public function run(array $sources, array $destinations)
23
    {
24
        $fileSystem = new Filesystem();
25
        
26
        // must have the same sources and destinations number
27
        if (count($sources) !== count($destinations) && count($destinations) !== 1) {
28
            throw new Exception('Sources and destinations count mismatch');
29
        }
30
        
31
        // if only one destination is specified for multiple sources, it should be a directory. We should create it if
32
        // it is not exists
33
        if (count($destinations) === 1 && !is_dir($destinations[0]) && count($sources) > 1) {
34
            $fileSystem->mkdir($destinations[0]);
35
        }
36
        $copiedFiles = [];
37
        
38
        // copy generated files to its destination
39
        foreach ($sources as $index => $source) {
40
            
41
            if (array_key_exists($index, $destinations)) {
42
                $destination = $destinations[$index];
43
            } else {
44
                $destination = $destinations[0];
45
            }
46
            
47
            if (is_dir($destination)) {
48
                $destination = $this->removeLastSlash($destination);
49
                
50
                if ($source instanceof \Symfony\Component\Finder\SplFileInfo && $source->getRelativePath()) {
51
                    $destination .= '/'.$source->getRelativePath();
52
                }
53
                $destination .= '/'.$source->getFilename();
54
            }
55
            $this->addNotification('copying "'.$source.'" to "'.$destination.'"');
56
            
57
            $fileSystem->copy($source->getRealPath(), $destination);
58
            $copiedFiles[] = $destination;
59
        }
60
        
61
        return $copiedFiles;
62
    }
63
    
64
    /**
65
     * No specific requirements for the copy filter.
66
     */
67
    public function checkRequirements()
68
    {
69
    }
70
    
71
    /**
72
     * Return the file extensions supported by this filter. All the files can be process by the copy filter.
73
     *
74
     * @return string[]
75
     */
76
    public function getSupportedExtensions()
77
    {
78
        return [
79
            '*'
80
        ];
81
    }
82
    
83
    /**
84
     * Remove the last slash of the string.
85
     *
86
     * @param string $string
87
     *
88
     * @return string
89
     */
90 View Code Duplication
    private function removeLastSlash($string)
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...
91
    {
92
        if ('/' === substr($string, strlen($string) - 1)) {
93
            $string = substr($string, 0, strlen($string) - 1);
94
        }
95
        
96
        return $string;
97
    }
98
}
99