Script::vendor2web()   B
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 7.6666
c 0
b 0
f 0
cc 10
nc 8
nop 1

How to fix   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
namespace BWC\Share\Composer;
4
5
use Composer\Script\Event;
6
use Symfony\Component\Filesystem\Filesystem;
7
use Symfony\Component\Finder\Finder;
8
9
class Script
10
{
11
    static function vendor2web(Event $event) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
        $options = self::getOptions($event);
13
        if (isset($options['vendor2web']) && is_array($options['vendor2web'])) {
14
            print "Installing vendor assets to web\n";
15
            $installType = $options['symfony-assets-install']; // hard|symlink|relative
16
            $webDir = $options['web-dir-full'];
17
            $vendorDir = $options['vendor-dir-full'];
18
            if ($installType != 'hard' && $installType != 'symlink' && $installType != 'relative') {
19
                print "ERROR: Unknown install type $installType \n";
20
                return;
21
            }
22
            if (!is_dir($webDir)) {
23
                print "ERROR: No web dir $webDir \n";
24
                return;
25
            }
26
            if (!is_dir(($vendorDir))) {
27
                print "ERROR: No vendor dir $vendorDir \n";
28
                return;
29
            }
30
            if (!is_dir($webDir.DIRECTORY_SEPARATOR.'vendor')) {
31
                mkdir($webDir.DIRECTORY_SEPARATOR.'vendor');
32
            }
33
            self::_deleteOldWebVendors($webDir);
34
            print "Installing new vendors to web:\n";
35
            foreach ($options['vendor2web'] as $vendor=>$destination) {
36
                self::_installVendorToWeb($vendor, $destination, $webDir, $vendorDir, $installType);
37
            }
38
        }
39
    }
40
41
    static private function _deleteOldWebVendors($webDir) {
42
        $finder = new Finder();
43
        $finder->directories()->in($webDir.DIRECTORY_SEPARATOR.'vendor')->depth(1);
44
        if ($finder->count()) {
45
            print "Deleting old vendors in web:\n";
46
            foreach ($finder as $dir) {
47
                /** @var $name \SplFileInfo */
48
                $name = $dir->getRelativePathname();
49
                $fn = $webDir.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.$name;
50
                print "    $name\n";
51
                @unlink($fn);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
52
                @rmdir($fn);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
53
            }
54
        }
55
    }
56
57
    static private function _installVendorToWeb($vendor, $destination, $webDir, $vendorDir, $installType) {
0 ignored issues
show
Unused Code introduced by
The parameter $installType is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
        print "    $vendor => $destination\n";
59
        $source = $vendorDir.DIRECTORY_SEPARATOR.$vendor;
60
        $dir = $webDir.DIRECTORY_SEPARATOR.'vendor';
61
        if (!is_dir($dir)) {
62
            mkdir($dir);
63
        }
64
        $target = $dir.DIRECTORY_SEPARATOR.$destination;
65
        if (!file_exists($source)) {
66
            print "WARNING: Source $vendor does not exist\n";
67
            return;
68
        }
69
        if (file_exists($target)) {
70
            @unlink($target);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
71
            @rmdir($target);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
72
        }
73
        // TODO target might contain some dir... should be created if not exist
74
        symlink($source, $target);
75
    }
76
77
    static protected function getOptions(Event $event) {
78
        $options = array_merge(
79
            array(
80
                'symfony-app-dir' => 'app',
81
                'symfony-web-dir' => 'web'
82
            ),
83
            $event->getComposer()->getPackage()->getExtra()
84
        );
85
        $options['symfony-assets-install'] = getenv('SYMFONY_ASSETS_INSTALL') ?: @$options['symfony-assets-install'];
86
        if (!$options['symfony-assets-install']) $options['symfony-assets-install'] = 'symlink';
87
        $options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout');
88
        $options['vendor-dir'] = $event->getComposer()->getConfig()->get('vendor-dir');
89
        $composerJsonFile = realpath(\Composer\Factory::getComposerFile());
90
        $rootDir = dirname($composerJsonFile);
91
        $options['root-dir'] = $rootDir;
92
        $options['app-dir-full'] = $rootDir.DIRECTORY_SEPARATOR.$options['symfony-app-dir'];
93
        $options['web-dir-full'] = $rootDir.DIRECTORY_SEPARATOR.$options['symfony-web-dir'];
94
95
        if ('/' == substr($options['vendor-dir'], 0, 1) ||
96
            ':' == substr($options['vendor-dir'], 1, 1)
97
        ) {
98
            $options['vendor-dir-full'] = $options['vendor-dir'];
99
        } else {
100
            $options['vendor-dir-full'] = $rootDir.DIRECTORY_SEPARATOR.$options['vendor-dir'];
101
        }
102
103
        return $options;
104
    }
105
106
}
107