GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( c45f40...f7d778 )
by Freek
01:36
created

Varnish   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 8 1
A getHosts() 0 10 2
A executeCommand() 0 12 2
A generateBanCommand() 0 16 2
1
<?php
2
3
namespace Spatie\Varnish;
4
5
use Symfony\Component\Process\Exception\ProcessFailedException;
6
use Symfony\Component\Process\Process;
7
8
class Varnish
9
{
10
    /**
11
     * @param string|array $host
12
     *
13
     * @return \Symfony\Component\Process\Process
14
     */
15
    public function flush($host = null)
16
    {
17
        $host = $this->getHosts($host);
18
19
        $command = $this->generateBanCommand($host);
20
21
        return $this->executeCommand($command);
22
    }
23
24
    /**
25
     * @param array|string $host
26
     *
27
     * @return array
28
     */
29
    protected function getHosts($host = null): array
30
    {
31
        $host = $host ?? config('laravel-varnish.host');
32
33
        if (! is_array($host)) {
34
            $host = [$host];
35
        }
36
37
        return $host;
38
    }
39
40
    public function generateBanCommand(array $hosts): string
41
    {
42
        if (! is_array($hosts)) {
43
            $hosts = [$hosts];
44
        }
45
46
        $hostsRegex = collect($hosts)
47
            ->map(function (string $host) {
48
                return "(^{$host}$)";
49
            })
50
            ->implode('|');
51
52
        $config = config('laravel-varnish');
53
54
        return "sudo varnishadm -S {$config['secret']} -T 127.0.0.1:{$config['administrative_port']} 'ban req.http.host ~ {$hostsRegex}'";
55
    }
56
57
    protected function executeCommand(string $command): Process
58
    {
59
        $process = new Process($command);
60
61
        $process->run();
62
63
        if (! $process->isSuccessful()) {
64
            throw new ProcessFailedException($process);
65
        }
66
67
        return $process;
68
    }
69
}
70