LighthouseClient   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 14
c 3
b 1
f 1
dl 0
loc 39
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 14 2
A __construct() 0 3 1
A __destruct() 0 3 1
A clearFiles() 0 3 2
1
<?php
2
3
namespace Jackal\Lighthouse;
4
5
use Jackal\Lighthouse\Process\LightHouseProcess;
6
use Jackal\Lighthouse\Result\Result;
7
use Symfony\Component\Process\Exception\RuntimeException;
8
9
class LighthouseClient
10
{
11
    /** @var LightHouseProcess $lighthouseProcess */
12
    protected $lighthouseProcess;
13
14
    public function __construct(array $options)
15
    {
16
        $this->lighthouseProcess = new LightHouseProcess($options);
17
    }
18
19
    /**
20
     * @param $url
21
     * @return Result|null
22
     */
23
    public function run($url) : Result
24
    {
25
        $process = $this->lighthouseProcess->getProcess($url);
26
27
        $process->setTimeout(3600);
28
        $process->run();
29
        if (!$process->isSuccessful()) {
30
            throw new RuntimeException($process->getErrorOutput());
31
        }
32
33
        $result = new Result(json_decode(file_get_contents($this->lighthouseProcess->getOutputPath()), true));
34
        $this->clearFiles();
35
36
        return $result;
37
    }
38
39
    protected function clearFiles(){
40
        if(is_file($this->lighthouseProcess->getOutputPath())) {
41
            unlink($this->lighthouseProcess->getOutputPath());
42
        }
43
    }
44
45
    public function __destruct()
46
    {
47
        $this->clearFiles();
48
    }
49
}
50