BootstrapStrategy::shouldAllow()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 4
eloc 10
c 3
b 0
f 1
nc 5
nop 0
dl 0
loc 22
rs 9.9332
1
<?php
2
/**
3
 * Copyright © Vaimo Group. All rights reserved.
4
 * See LICENSE_VAIMO.txt for license details.
5
 */
6
namespace Vaimo\ComposerPatches\Strategies;
7
8
class BootstrapStrategy
9
{
10
    /**
11
     * @var \Vaimo\ComposerPatches\Composer\Context
12
     */
13
    private $composerContext;
14
15
    /**
16
     * @param \Vaimo\ComposerPatches\Composer\Context $composerContext
17
     */
18
    public function __construct(
19
        \Vaimo\ComposerPatches\Composer\Context $composerContext
20
    ) {
21
        $this->composerContext = $composerContext;
22
    }
23
24
    public function shouldAllow()
25
    {
26
        if (getenv('COMPOSER_PATCHER_ALLOW_GLOBAL_USAGE')) {
27
            return true;
28
        }
29
30
        if (!$this->isPluginAvailable()) {
31
            return false;
32
        }
33
34
        $lockUpdateArgument = 'lock';
35
36
        try {
37
            $input = new \Symfony\Component\Console\Input\ArgvInput();
38
39
            return !$input->hasParameterOption(sprintf('--%s', $lockUpdateArgument));
40
        } catch (\Exception $e) {
41
            // There are situations where composer is accessed from non-CLI entry-points,
42
            // which will cause $argv not to be available, resulting a crash.
43
        }
44
45
        return false;
46
    }
47
48
    private function isPluginAvailable()
49
    {
50
        $composer = $this->composerContext->getLocalComposer();
51
52
        $packageResolver = new \Vaimo\ComposerPatches\Composer\Plugin\PackageResolver(
53
            array($composer->getPackage())
54
        );
55
56
        $repository = $composer->getRepositoryManager()->getLocalRepository();
57
58
        try {
59
            $packageResolver->resolveForNamespace($repository->getCanonicalPackages(), __NAMESPACE__);
60
        } catch (\Vaimo\ComposerPatches\Exceptions\PackageResolverException $exception) {
61
            return false;
62
        }
63
64
        return true;
65
    }
66
}
67