AbstractAudit::getCommandOptions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Suitmedia\LighthouseAudit\Audit;
4
5
use Suitmedia\LighthouseAudit\Concerns\CanRetrieveInputValues;
6
use Suitmedia\LighthouseAudit\ProcessBuilder;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Process\Exception\ProcessFailedException;
10
11
abstract class AbstractAudit
12
{
13
    use CanRetrieveInputValues;
14
15
    /**
16
     * Process builder object.
17
     *
18
     * @var ProcessBuilder
19
     */
20
    protected $processBuilder;
21
22
    /**
23
     * Console output interface.
24
     *
25
     * @var OutputInterface
26
     */
27
    protected $output;
28
29
    /**
30
     * Current filename.
31
     *
32
     * @var string
33
     */
34
    protected $filename;
35
36
    /**
37
     * Current url.
38
     *
39
     * @var string
40
     */
41
    protected $url;
42
43
    /**
44
     * AbstractProcess constructor.
45
     *
46
     * @param ProcessBuilder  $processBuilder
47
     * @param InputInterface  $input
48
     * @param OutputInterface $output
49
     * @param string          $filename
50
     */
51
    public function __construct(ProcessBuilder $processBuilder, InputInterface $input, OutputInterface $output, string $filename)
52
    {
53
        $this->processBuilder = $processBuilder;
54
        $this->input = $input;
55
        $this->output = $output;
56
        $this->filename = $filename;
57
58
        $this->url = $this->getUrlPrefix().$this->filename;
59
    }
60
61
    /**
62
     * Generate command for the current process.
63
     *
64
     * @return array
65
     */
66
    protected function generateCommand() :array
67
    {
68
        $command = [
69
            'lighthouse-ci',
70
            $this->url,
71
        ];
72
        $command[] = '--chrome-flags="'.implode(' ', $this->getChromeFlags()).'"';
73
74
        return array_merge($command, $this->getCommandOptions());
75
    }
76
77
    /**
78
     * Get the command options.
79
     *
80
     * @return array
81
     */
82
    protected function getCommandOptions() :array
83
    {
84
        return [
85
            '--performance='.$this->getPerformanceScore(),
86
            '--best-practices='.$this->getBestPracticesScore(),
87
            '--accessibility='.$this->getAccessibilityScore(),
88
            '--seo='.$this->getSeoScore(),
89
            '--pwa='.$this->getPwaScore(),
90
            '--emulated-form-factor='.$this->getMode(),
91
            '--throttling-method=devtools',
92
        ];
93
    }
94
95
    /**
96
     * Get the default chrome flags.
97
     *
98
     * @return array
99
     */
100
    protected function getDefaultChromeFlags() :array
101
    {
102
        return [
103
            '--no-sandbox',
104
            '--headless',
105
            '--disable-gpu',
106
            '--incognito',
107
            '--disable-timeouts-for-profiling',
108
        ];
109
    }
110
111
    /**
112
     * Run the process.
113
     *
114
     * @throws \Exception
115
     *
116
     * @return bool
117
     */
118
    public function run() :bool
119
    {
120
        $status = '<info>PASS</info>';
121
122
        $process = $this->processBuilder->create($this->generateCommand());
123
        $process->run();
124
        $processStatus = $process->isSuccessful();
125
126
        if (!$processStatus) {
127
            $status = '<error>FAIL</error>';
128
        }
129
130
        $this->output->writeln(sprintf('[%s] Processed url: %s', $status, $this->url));
131
132
        if (!$processStatus) {
133
            throw new ProcessFailedException($process);
134
        }
135
136
        return $processStatus;
137
    }
138
}
139