PackagistSecurityCheckerCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * MIT License
5
 *
6
 * Copyright (c) 2016 Bernardo Secades
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in all
16
 * copies or substantial portions of the Software.
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
namespace BernardoSecades\Packagist\SecurityChecker\Command;
27
28
use BernardoSecades\Packagist\SecurityChecker\Formatter\JsonFormatter;
29
use BernardoSecades\Packagist\SecurityChecker\Formatter\TextFormatter;
30
use BernardoSecades\Packagist\SecurityChecker\PackagistSecurityChecker;
31
use BernardoSecades\Packagist\SecurityChecker\ValueObject\FilterCheck;
32
use Symfony\Component\Console\Command\Command;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Input\InputArgument;
35
use Symfony\Component\Console\Input\InputOption;
36
use Symfony\Component\Console\Output\OutputInterface;
37
38
/**
39
 * @author bernardosecades <[email protected]>
40
 */
41
class PackagistSecurityCheckerCommand extends Command
42
{
43
    const TEXT_FORMAT = 'text';
44
45
    const JSON_FORMAT = 'json';
46
47
    /** @var  PackagistSecurityChecker */
48
    protected $checker;
49
50
    /**
51
     * @param PackagistSecurityChecker $packagistSecurityChecker
52
     */
53 3
    public function __construct(PackagistSecurityChecker $packagistSecurityChecker)
54
    {
55 3
        $this->checker = $packagistSecurityChecker;
56 3
        parent::__construct();
57 3
    }
58
59
    /**
60
     * Configure
61
     */
62 3
    protected function configure()
63
    {
64 3
        $this
65 3
            ->setName('packagist:security-check')
66 3
            ->setAliases(['security-check', 'sc'])
67 3
            ->setDescription('Analize you dependencies in composer.lock and check if there are bugs in packagist')
68 3
            ->setHelp('This command allows find bugs in your packagist dependencies.')
69 3
            ->addArgument('composer-lock-file', InputArgument::REQUIRED, 'path your composer.lock file')
70 3
            ->addOption('packagist-url', null, InputOption::VALUE_OPTIONAL, 'url of your company`s packagist')
71 3
            ->addOption('format', null, InputOption::VALUE_REQUIRED, 'Formats enabled: json, text', 'text')
72 3
            ->addOption('only-bugs', null, InputOption::VALUE_NONE);
73 3
    }
74
75
    /**
76
     * @param InputInterface  $input
77
     * @param OutputInterface $output
78
     * @return int
79
     */
80 3
    protected function execute(InputInterface $input, OutputInterface $output)
81
    {
82 3
        if (function_exists('xdebug_disable')) {
83 3
            xdebug_disable();
84 3
        }
85
86 3
        $packagistUrl = $input->getOption('packagist-url');
87 3
        if (null !== $packagistUrl) {
88
            $this->checker->setPackagistUrl($packagistUrl);
89
        }
90
91 3
        $composerLockFile = $input->getArgument('composer-lock-file');
92
93 3
        $filter = null;
94 3
        if ($input->getOption('only-bugs')) {
95
            $filter = FilterCheck::BUG;
96
        }
97
98 3
        $packages = $this->checker->check($composerLockFile, $filter);
99
100 3
        switch ($input->getOption('format')) {
101 3
            case self::TEXT_FORMAT:
102 2
                $formatter = new TextFormatter();
103 2
                break;
104 1
            case self::JSON_FORMAT:
105 1
                $formatter = new JsonFormatter();
106 1
                break;
107
            default:
108
                $formatter = new TextFormatter();
109
                break;
110 3
        }
111
112 3
        $formatter->displayReport($output, $input, $packages);
113
114 3
        if ($this->checker->hasBugs()) {
115 1
            return 1;
116
        }
117
118 2
        return 0;
119
    }
120
}
121