Completed
Push — 3.0 ( 46111f...d5ff83 )
by Daniel
02:06
created

Link::createDelegate()   C

Complexity

Conditions 17
Paths 74

Size

Total Lines 80
Code Lines 44

Duplication

Lines 19
Ratio 23.75 %

Code Coverage

Tests 38
CRAP Score 20.9951
Metric Value
dl 19
loc 80
ccs 38
cts 50
cp 0.76
rs 5.033
cc 17
eloc 44
nc 74
nop 2
crap 20.9951

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
 * Composer Magento Installer
4
 */
5
6
namespace MagentoHackathon\Composer\Magento\Deploystrategy;
7
8
/**
9
 * Hardlink deploy strategy
10
 */
11
class Link extends DeploystrategyAbstract
12
{
13
    /**
14
     * Creates a hardlink with lots of error-checking
15
     *
16
     * @param string $source
17
     * @param string $dest
18
     * @return bool
19
     * @throws \ErrorException
20
     */
21 13
    public function createDelegate($source, $dest)
22
    {
23 13
        $sourcePath = $this->getSourceDir() . '/' . $this->removeTrailingSlash($source);
24 13
        $destPath = $this->getDestDir() . '/' . $this->removeTrailingSlash($dest);
25
26
27
        // Create all directories up to one below the target if they don't exist
28 13
        $destDir = dirname($destPath);
29 13
        if (!file_exists($destDir)) {
30 2
            mkdir($destDir, 0777, true);
31 2
        }
32
33
        // Handle source to dir link,
34
        // e.g. Namespace_Module.csv => app/locale/de_DE/
35 13
        if (file_exists($destPath) && is_dir($destPath)) {
36 6
            if (basename($sourcePath) === basename($destPath)) {
37
                // copy/link each child of $sourcePath into $destPath
38 1 View Code Duplication
                foreach (new \DirectoryIterator($sourcePath) as $item) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
39 1
                    $item = (string) $item;
40 1
                    if (!strcmp($item, '.') || !strcmp($item, '..')) {
41 1
                        continue;
42
                    }
43 1
                    $childSource = $source . '/' . $item;
44 1
                    $this->create($childSource, substr($destPath, strlen($this->getDestDir())+1));
45 1
                }
46 1
                return true;
47 View Code Duplication
            } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
48 6
                $destPath .= '/' . basename($source);
49 6
                return $this->create($source, substr($destPath, strlen($this->getDestDir())+1));
50
            }
51
        }
52
53
        // From now on $destPath can't be a directory, that case is already handled
54
55
        // If file exists and force is not specified, throw exception unless FORCE is set
56 13 View Code Duplication
        if (file_exists($destPath)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
57
            if ($this->isForced()) {
58
                unlink($destPath);
59
            } else {
60
                throw new \ErrorException("Target $dest already exists (set extra.magento-force to override)");
61
            }
62
        }
63
64
        // File to file
65 13
        if (!is_dir($sourcePath)) {
66 9
            if (is_dir($destPath)) {
67
                $destPath .= '/' . basename($sourcePath);
68
            }
69 9
            return link($sourcePath, $destPath);
70
        }
71
72
        // Copy dir to dir
73
        // First create destination folder if it doesn't exist
74 4
        if (file_exists($destPath)) {
75
            $destPath .= '/' . basename($sourcePath);
76
        }
77 4
        mkdir($destPath, 0777, true);
78
79 4
        $iterator = new \RecursiveIteratorIterator(
80 4
            new \RecursiveDirectoryIterator($sourcePath),
81
            \RecursiveIteratorIterator::SELF_FIRST
82 4
        );
83
84 4
        foreach ($iterator as $item) {
85 4
            $subDestPath = $destPath . '/' . $iterator->getSubPathName();
86 4
            if ($item->isDir()) {
87 4
                if (! file_exists($subDestPath)) {
88
                    mkdir($subDestPath, 0777, true);
89
                }
90 4
            } else {
91 4
                link($item, $subDestPath);
92 4
                $this->addDeployedFile($subDestPath);
93
            }
94 4
            if (!is_readable($subDestPath)) {
95
                throw new \ErrorException("Could not create $subDestPath");
96
            }
97 4
        }
98
99 4
        return true;
100
    }
101
}
102