SymlinkMethod::exposeDirectory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 3
b 0
f 0
nc 1
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
namespace SilverStripe\VendorPlugin\Methods;
4
5
use Composer\Util\Filesystem;
6
use RuntimeException;
7
8
/**
9
 * Expose the vendor module resources via a symlink
10
 */
11
class SymlinkMethod implements ExposeMethod
12
{
13
    const NAME = 'symlink';
14
15
    /**
16
     * @var Filesystem
17
     */
18
    protected $filesystem = null;
19
20
    public function __construct(Filesystem $filesystem = null)
21
    {
22
        $this->filesystem = $filesystem ?: new Filesystem();
23
    }
24
25
    public function exposeDirectory($source, $target)
26
    {
27
        // Remove destination directory to ensure it is clean
28
        $this->filesystem->removeDirectory($target);
29
30
        // Ensure parent dir exist
31
        $parent = dirname($target);
32
        $this->filesystem->ensureDirectoryExists($parent);
33
34
        // Ensure symlink exists
35
        $this->createLink($source, $target);
36
    }
37
38
    /**
39
     * Create symlink
40
     *
41
     * @param string $source File source
42
     * @param string $target Place to put symlink
43
     */
44
    protected function createLink($source, $target)
45
    {
46
        $success = $this->filesystem->relativeSymlink($source, $target);
47
        if (!$success) {
48
            throw new RuntimeException("Could not create symlink at $target");
49
        }
50
    }
51
}
52