SymlinkMethod   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 11
c 3
b 0
f 0
dl 0
loc 38
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createLink() 0 5 2
A exposeDirectory() 0 11 1
A __construct() 0 3 2
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