SymlinkFilesystem   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 65
c 0
b 0
f 0
wmc 9
lcom 0
cbo 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A ensureSymlinkExists() 0 10 2
A removeSymlink() 0 14 3
A removeEmptyDirectory() 0 14 4
1
<?php
2
3
/*
4
 * This file is part of the "Composer Shared Package Plugin" package.
5
 *
6
 * https://github.com/Letudiant/composer-shared-package-plugin
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace LEtudiant\Composer\Util;
13
14
use Composer\Util\Filesystem;
15
16
/**
17
 * @author Sylvain Lorinet <[email protected]>
18
 */
19
class SymlinkFilesystem extends Filesystem
20
{
21
    /**
22
     * Create a symlink
23
     *
24
     * @param string $sourcePath
25
     * @param string $symlinkPath
26
     *
27
     * @return bool
28
     */
29
    public function ensureSymlinkExists($sourcePath, $symlinkPath)
30
    {
31
        if (!is_link($symlinkPath)) {
32
            $this->ensureDirectoryExists(dirname($symlinkPath));
33
34
            return symlink($sourcePath, $symlinkPath);
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * @param string $symlinkPath
42
     *
43
     * @return bool
44
     *
45
     * @throws \RuntimeException
46
     */
47
    public function removeSymlink($symlinkPath)
48
    {
49
        if (is_link($symlinkPath)) {
50
            if (!$this->unlink($symlinkPath)) {
51
                // @codeCoverageIgnoreStart
52
                throw new \RuntimeException('Unable to remove the symlink : ' . $symlinkPath);
53
                // @codeCoverageIgnoreEnd
54
            }
55
56
            return true;
57
        }
58
59
        return false;
60
    }
61
62
    /**
63
     * @param string $directoryPath
64
     *
65
     * @return bool
66
     *
67
     * @throws \RuntimeException
68
     */
69
    public function removeEmptyDirectory($directoryPath)
70
    {
71
        if (is_dir($directoryPath) && $this->isDirEmpty($directoryPath)) {
72
            if (!$this->removeDirectory($directoryPath)) {
73
                // @codeCoverageIgnoreStart
74
                throw new \RuntimeException('Unable to remove the directory : ' . $directoryPath);
75
                // @codeCoverageIgnoreEnd
76
            }
77
78
            return true;
79
        }
80
81
        return false;
82
    }
83
}
84