LightHouseProcess::isInteractive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
namespace Jackal\Lighthouse\Process;
4
5
use Jackal\BinLocator\BinLocator;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
use Symfony\Component\Process\Process;
8
9
class LightHouseProcess
10
{
11
    /** @var array $options */
12
    protected $options;
13
14
    /**
15
     * LightHouseProcess constructor.
16
     * @param array $options
17
     */
18
    public function __construct(array $options = [])
19
    {
20
        $resolver = new OptionsResolver();
21
        $resolver->setDefaults([
22
            'interactive' => false,
23
            'output' => 'json',
24
            'path' => sys_get_temp_dir() . '/' . uniqid() . '.json',
25
            'username' => null,
26
            'password' => null,
27
        ]);
28
29
        $resolver->setAllowedValues('output', [
30
            'json','html',
31
        ]);
32
33
        $this->options = $resolver->resolve($options);
34
    }
35
36
    /**
37
     * @return string|null
38
     */
39
    protected function getBasicAuth() : ?string
40
    {
41
        if($this->getUsername() and $this->getPassword()) {
42
            return sprintf('Basic %s', base64_encode($this->getUsername() . ':' . $this->getPassword()));
43
        }
44
45
        return null;
46
    }
47
48
    /**
49
     * @param $url
50
     * @return array
51
     */
52
    protected function getShellCommand($url) : array {
53
        $headers = addcslashes(json_encode(['authorization' => $this->getBasicAuth()]), '"');
54
55
        $options = [
56
            $this->isInteractive() ? '' : '--chrome-flags="--headless --no-sandbox"',
57
            $this->getOutputPath() ? '--output=' . $this->getOutputType() : '--output=json',
58
            '--output-path=' . $this->getOutputPath(),
59
            $this->getBasicAuth() ? '--extra-headers="' . $headers . '"' : '',
60
        ];
61
62
        $options = array_reduce($options, function ($options, $currOption){
63
            if($currOption != ''){
64
                $options[] = $currOption;
65
            }
66
67
            return $options;
68
        }, []);
69
70
        return array_merge([$url], $options);
71
    }
72
73
    /**
74
     * @param $url
75
     * @return Process
76
     */
77
    public function getProcess($url) : Process{
78
79
        $binLocator = new BinLocator('lighthouse');
80
81
        return $binLocator->getProcess($this->getShellCommand($url));
82
83
    }
84
85
    /**
86
     * @return string
87
     */
88
    public function getOutputPath() : string {
89
        return $this->options['path'];
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    public function getOutputType() : string {
96
        return $this->options['output'];
97
    }
98
99
    /**
100
     * @return bool
101
     */
102
    public function isInteractive() : bool {
103
        return $this->options['interactive'] == true;
104
    }
105
106
    /**
107
     * @return string|null
108
     */
109
    public function getUsername() : ?string {
110
        return $this->options['username'];
111
    }
112
113
    /**
114
     * @return string|null
115
     */
116
    public function getPassword() : ?string {
117
        return $this->options['password'];
118
    }
119
}