GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#4)
by Iacovos
09:36
created

MonitorCommand::setClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Softius\JenkinsJobMonitor;
4
5
use GuzzleHttp\ClientInterface;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Process\Process;
12
13
/**
14
 * Class MonitorCommand
15
 * @package Softius\JenkinsJobMonitor
16
 */
17
class MonitorCommand extends Command
18
{
19
20
    /**
21
     * Configures the monitor command.
22
     */
23
    protected function configure()
24
    {
25
        $this
26
            ->setName('monitor')
27
            ->setDescription('Monitors the non-interactive execution of processes')
28
            ->addArgument('url', InputArgument::REQUIRED, 'Jenkins home URL')
29
            ->addArgument('job', InputArgument::REQUIRED, 'Job name as configured in Jenkins')
30
            ->addArgument('monitor', InputArgument::OPTIONAL, 'Command to be monitored')
31
            ->addOption('display', null, InputOption::VALUE_OPTIONAL, '')
32
            ->addOption('description', null, InputOption::VALUE_OPTIONAL, '');
33
    }
34
35
    /**
36
     * Executes the monitor command.
37
     *
38
     * @param InputInterface  $input  An InputInterface instance
39
     * @param OutputInterface $output An OutputInterface instance
40
     *
41
     * @return null|int null or 0 if everything went fine, or an error code
42
     */
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        try {
46
            $command = $this->getMonitorArgument($input);
47
48
            $monitor = new JobMonitor(
49
                $input->getArgument('job'),
50
                $input->getOption('display'),
51
                $input->getOption('description')
52
            );
53
54
            $monitor->start();
55
            $process = new Process($command);
56
            $process->run();
57
            $monitor->stop($process->getExitCode(), $process->getOutput());
58
59
            $this->getClient()->send($monitor->getRequest(), ['base_uri' => $input->getArgument('url')]);
60
        } catch (\Exception $e) {
61
            $output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
62
            return 1;
63
        }
64
65
        return 0;
66
    }
67
68
    /**
69
     * Returns the command to monitor.
70
     *
71
     * If monitor argument is not provided, STDIN is being used instead.
72
     *
73
     * @param InputInterface $input
74
     * @throws \RuntimeException
75
     * @return mixed|string
76
     */
77 View Code Duplication
    private function getMonitorArgument(InputInterface $input)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
    {
79
        $monitor = $input->getArgument('monitor');
80
        if (!$monitor && 0 === ftell(STDIN)) {
81
            $monitor = '';
82
            while (!feof(STDIN)) {
83
                $monitor .= fread(STDIN, 1024);
84
            }
85
        }
86
87
        if (!$monitor) {
88
            throw new \RuntimeException('Please provide a command or pipe content to STDIN.');
89
        }
90
91
        return $monitor;
92
    }
93
94
    /**
95
     * @param GuzzleHttp\ClientInterface $client
96
     */
97
    public function setClient(ClientInterface $client)
98
    {
99
        $this->client = $client;
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
100
    }
101
102
    /**
103
     * @return Client
104
     */
105
    public function getClient()
106
    {
107
        return $this->client;
108
    }
109
}