LighthouseClient::clearFiles()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 3
rs 10
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