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

BootstrapStrategy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 20
dl 0
loc 53
rs 10
c 2
b 0
f 1
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isPluginAvailable() 0 17 2
A shouldAllow() 0 18 3
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