Completed
Pull Request — master (#6)
by Thorsten
05:11
created

PhpExtensionsAreInstalled::message()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace BeyondCode\SelfDiagnosis\Checks;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Collection;
7
8
class PhpExtensionsAreInstalled implements Check
9
{
10
11
    const EXT = 'ext-';
12
13
    /** @var Filesystem */
14
    private $filesystem;
15
16
    public function __construct(Filesystem $filesystem)
17
    {
18
        $this->filesystem = $filesystem;
19
    }
20
21
    /** @var Collection */
22
    private $extensions;
23
24
    /**
25
     * The name of the check.
26
     *
27
     * @return string
28
     */
29
    public function name(): string
30
    {
31
        return 'The required PHP extensions are installed';
32
    }
33
34
    /**
35
     * The error message to display in case the check does not pass.
36
     *
37
     * @return string
38
     */
39
    public function message(): string
40
    {
41
        return 'The following extensions are missing: ' . PHP_EOL . $this->extensions->implode(PHP_EOL);
42
    }
43
44
    /**
45
     * Perform the actual verification of this check.
46
     *
47
     * @return bool
48
     */
49
    public function check(): bool
50
    {
51
        $this->extensions = Collection::make([
52
            'openssl',
53
            'PDO',
54
            'mbstring',
55
            'tokenizer',
56
            'xml',
57
            'ctype',
58
            'json'
59
        ]);
60
        $this->extensions = $this->extensions->merge($this->getExtensionsRequiredInComposerFile());
61
        $this->extensions = $this->extensions->reject(function ($ext) {
62
            return extension_loaded($ext);
63
        });
64
65
        return $this->extensions->isEmpty();
66
    }
67
68
    /**
69
     * @return array
70
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
71
     */
72
    public function getExtensionsRequiredInComposerFile()
73
    {
74
        $composer = json_decode($this->filesystem->get(base_path('composer.json')), true);
75
        $filtered = array_where(array_keys(array_get($composer, 'require')), function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
            return starts_with($value, self::EXT);
77
        });
78
        $extensions = [];
79
        foreach ($filtered as $extension) {
80
            $extensions[] = str_replace_first(self::EXT, '', $extension);
81
        }
82
        return $extensions;
83
    }
84
85
}
86