Plugin::activate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 2
eloc 9
c 2
b 1
f 1
nc 2
nop 2
dl 0
loc 17
rs 9.9666
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatchesProxy;
7
8
require_once 'src/Plugin.php';
9
10
/**
11
 * @SuppressWarnings(PHPMD.ShortVariable)
12
 */
13
class Plugin extends \Vaimo\ComposerPatches\Plugin
14
{
15
    public function activate(\Composer\Composer $composer, \Composer\IO\IOInterface $io)
16
    {
17
        $namespacePrefix = implode('\\', array_slice(explode('\\', get_parent_class($this)), 0, 2)) . '\\';
18
        $autoloadFile = $this->resolveAutoloadFilePath($composer);
19
20
        if (!$autoloadFile) {
21
            return;
22
        }
23
24
        include $autoloadFile;
25
26
        $composerCtxFactory = new \Vaimo\ComposerPatches\Factories\ComposerContextFactory($composer);
27
        $composerContext = $composerCtxFactory->create();
28
29
        $this->bootstrapFileTree($composerContext, $namespacePrefix);
30
31
        parent::activate($composer, $io);
32
    }
33
34
    public function resolveAutoloadFilePath(\Composer\Composer $composer)
35
    {
36
       /**
37
         * When running through the initial installation, make sure that installing the proxy
38
         * command (to get the patch commands) does not result in crashing the whole
39
         * installation process.
40
         */
41
        $autoloadFile = $this->composePath(
42
            $composer->getConfig()->get('vendor-dir'),
43
            'autoload.php'
44
        );
45
46
        if (!file_exists($autoloadFile)) {
47
            return '';
48
        }
49
50
        return $autoloadFile;
51
    }
52
53
    public function resetPackages(\Composer\Installer\PackageEvent $event)
54
    {
55
        $autoloadFile = $this->resolveAutoloadFilePath($event->getComposer());
56
57
        if (!$autoloadFile) {
58
            return;
59
        }
60
61
        return parent::resetPackages($event);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::resetPackages($event) targeting Vaimo\ComposerPatches\Plugin::resetPackages() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
    }
63
64
    public function postInstall(\Composer\Script\Event $event)
65
    {
66
        $autoloadFile = $this->resolveAutoloadFilePath($event->getComposer());
67
68
        if (!$autoloadFile) {
69
            return;
70
        }
71
72
        return parent::postInstall($event);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::postInstall($event) targeting Vaimo\ComposerPatches\Plugin::postInstall() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
73
    }
74
75
    private function bootstrapFileTree(\Vaimo\ComposerPatches\Composer\Context $composerContext, $namespacePrefix)
76
    {
77
        $composer = $composerContext->getLocalComposer();
78
79
        $composerConfig = $composer->getConfig();
80
81
        $vendorDir = $composerConfig->get(\Vaimo\ComposerPatches\Composer\ConfigKeys::VENDOR_DIR);
82
83
        $packageResolver = new \Vaimo\ComposerPatches\Composer\Plugin\PackageResolver(
84
            array($composer->getPackage())
85
        );
86
87
        $pluginPackage = $packageResolver->resolveForNamespace(
88
            $composerContext->getActivePackages(),
89
            $namespacePrefix
90
        );
91
92
        $this->createSymlink(
93
            realpath('.'),
94
            $this->composePath($vendorDir, $pluginPackage->getName()),
95
            true
96
        );
97
    }
98
99
    /**
100
     * @SuppressWarnings(PHPMD.BooleanArgumentFlag)
101
     */
102
    private function createSymlink($fromPath, $toPath, $graceful = false)
103
    {
104
        if (is_link($toPath)) {
105
            unlink($toPath);
106
        }
107
108
        if ($graceful && (file_exists($toPath) || !file_exists($fromPath))) {
109
            return;
110
        }
111
112
        symlink($fromPath, $toPath);
113
    }
114
115
    private function composePath()
116
    {
117
        $pathSegments = array_map(function ($item) {
118
            return rtrim($item, \DIRECTORY_SEPARATOR);
119
        }, func_get_args());
120
121
        return implode(
122
            DIRECTORY_SEPARATOR,
123
            array_filter($pathSegments)
124
        );
125
    }
126
127
    /**
128
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
129
     */
130
    public function deactivate(\Composer\Composer $composer, \Composer\IO\IOInterface $appIO)
131
    {
132
    }
133
134
    /**
135
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
136
     */
137
    public function uninstall(\Composer\Composer $composer, \Composer\IO\IOInterface $appIO)
138
    {
139
    }
140
}
141