1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Suitmedia\LighthouseAudit\Audit; |
4
|
|
|
|
5
|
|
|
use Suitmedia\LighthouseAudit\Audit\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 ['--no-sandbox', '--headless', '--disable-gpu']; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Run the process. |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
* @throws \Exception |
110
|
|
|
*/ |
111
|
|
|
public function run() :bool |
112
|
|
|
{ |
113
|
|
|
$status = '<info>PASS</info>'; |
114
|
|
|
|
115
|
|
|
$process = $this->processBuilder->create($this->generateCommand()); |
116
|
|
|
$process->run(); |
117
|
|
|
$processStatus = $process->isSuccessful(); |
118
|
|
|
|
119
|
|
|
if (!$processStatus) { |
120
|
|
|
$status = '<error>FAIL</error>'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->output->writeln(sprintf('[%s] Processed url: %s', $status, $this->url)); |
124
|
|
|
|
125
|
|
|
if (!$processStatus) { |
126
|
|
|
throw new ProcessFailedException($process); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $processStatus; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|