Passed
Push — master ( e32a81...12baf4 )
by Antonio Carlos
02:26
created

Composer::check()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 13
ccs 4
cts 7
cp 0.5714
crap 2.3149
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace PragmaRX\Health\Checkers;
4
5
use PragmaRX\Health\Support\Result;
6
use Symfony\Component\Process\Process as SymfonyProcess;
7
8
class Composer extends Base
9
{
10
    /**
11
     * Check resource.
12
     *
13
     * @return Result
14
     */
15 1
    public function check()
16
    {
17 1
        $outdated = $this->executeCommand();
18
19 1
        if ($outdated->count() > $this->target->resource->shouldCountAtMost) {
20
            return $this->makeResult(
21
                false,
22
                sprintf($this->target->getErrorMessage(), $outdated->count())
23
            );
24
        }
25
26 1
        return $this->makeHealthyResult();
27
    }
28
29
    /**
30
     * Convert output to array.
31
     *
32
     * @param string $output
33
     * @return \Illuminate\Support\Collection|mixed
34
     */
35 1
    protected function outputToCollection(string $output)
36
    {
37 1
        if ($this->target->resource->jsonResult) {
38 1
            return collect(json_decode($output, true) ?? collect([]));
39
        }
40
41
        return $output;
42
    }
43
44
    /**
45
     * Get the ping binary.
46
     */
47 1
    protected function makeCommand()
48
    {
49 1
        return sprintf(
50 1
            '%s %s',
51 1
            $this->target->resource->binary,
52 1
            $this->target->resource->command
53
        );
54
    }
55
56
    /**
57
     * Execute the Composer command.
58
     *
59
     * @return \Illuminate\Support\Collection|mixed
60
     */
61 1
    protected function executeCommand()
62
    {
63 1
        $process = new SymfonyProcess(
64 1
            $this->makeCommand(),
65 1
            $this->target->workingDir
0 ignored issues
show
Bug introduced by
The property workingDir does not seem to exist in PragmaRX\Health\Support\Target.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
66
        );
67
68 1
        $process->run();
69
70 1
        $output = $this->outputToCollection($process->getOutput());
71
72 1
        if ($output->count() == 0) {
73 1
            return $output;
74
        }
75
76
        if ($rootItem = $this->target->resource->rootItem) {
77
            return collect($output[$rootItem]);
78
        }
79
80
        return $output;
81
    }
82
}
83