Passed
Push — master ( 42c4d1...80535d )
by Allan
02:39 queued 12s
created

BootstrapStrategy::shouldAllow()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 18
rs 10
c 1
b 0
f 1
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 (!$this->isPluginAvailable()) {
27
            return false;
28
        }
29
30
        $lockUpdateArgument = 'lock';
31
        
32
        try {
33
            $input = new \Symfony\Component\Console\Input\ArgvInput();
34
35
            return !$input->hasParameterOption(sprintf('--%s', $lockUpdateArgument));
36
        } catch (\Exception $e) {
37
            // There are situations where composer is accessed from non-CLI entry-points,
38
            // which will cause $argv not to be available, resulting a crash.
39
        }
40
41
        return false;
42
    }
43
    
44
    private function isPluginAvailable()
45
    {
46
        $composer = $this->composerContext->getLocalComposer();
47
48
        $packageResolver = new \Vaimo\ComposerPatches\Composer\Plugin\PackageResolver(
49
            array($composer->getPackage())
50
        );
51
52
        $repository = $composer->getRepositoryManager()->getLocalRepository();
53
54
        try {
55
            $packageResolver->resolveForNamespace($repository->getCanonicalPackages(), __NAMESPACE__);
56
        } catch (\Vaimo\ComposerPatches\Exceptions\PackageResolverException $exception) {
57
            return false;
58
        }
59
        
60
        return true;
61
    }
62
}
63