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
Pull Request — master (#34)
by Mathias
02:33
created

Varnish::getSecret()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
namespace Spatie\Varnish;
4
5
use Symfony\Component\Process\Process;
6
7
class Varnish
8
{
9
    /*
10
     * Known exec types
11
     */
12
    const EXEC_SOCKET = 'socket';
13
    const EXEC_COMMAND = 'command';
14
15
    /**
16
     * @param string|array $host
17
     *
18
     * @return bool|null
19
     *
20
     * @throws \Exception
21
     */
22
    public function flush($host = null): bool
23
    {
24
        $config = config('varnish');
25
26
        $host = $this->getHosts($host);
27
        $expr = $this->generateBanExpr($host);
28
29
        // Default to execution_type command when the config parameter is not set
30
        switch ($config['execution_type'] ?? self::EXEC_COMMAND) {
31
            case self::EXEC_SOCKET:
32
                return $this->executeSocketCommand($expr);
33
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
34
            case self::EXEC_COMMAND:
35
                $command = $this->generateBanCommand($expr);
36
                return $this->executeCommand($command);
37
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
38
            default:
39
                throw new \Exception(sprintf(
40
                    'Unknown execution type: %s', $config['execution_type']
41
                ));
42
        }
43
    }
44
45
    /**
46
     * @param array|string $host
47
     *
48
     * @return array
49
     */
50
    protected function getHosts($host = null): array
51
    {
52
        $host = $host ?? config('varnish.host');
53
54
        if (! is_array($host)) {
55
            $host = [$host];
56
        }
57
58
        return $host;
59
    }
60
61
    /**
62
     * @param string $expr
63
     *
64
     * @return string
65
     */
66
    public function generateBanCommand($expr = ''): string
67
    {
68
        $config = config('varnish');
69
70
        return "sudo varnishadm -S {$config['administrative_secret']} -T 127.0.0.1:{$config['administrative_port']} '{$expr}'";
71
    }
72
73
    /**
74
     * @param array $hosts
75
     *
76
     * @return string
77
     */
78
    public function generateBanExpr(array $hosts): string
79
    {
80
        $hostsRegex = collect($hosts)
81
            ->map(function (string $host) {
82
                return "(^{$host}$)";
83
            })
84
            ->implode('|');
85
86
        return sprintf('ban req.http.host ~ %s', $hostsRegex);
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getSecret()
93
    {
94
        $config = config('varnish');
95
        if (! $secret = $config['administrative_secret_string']) {
96
            $secret = '';
97
            if (file_exists($config['administrative_secret'])) {
98
                $secret = trim(file_get_contents($config['administrative_secret']));
99
            }
100
        }
101
102
        return $secret;
103
    }
104
105
    /**
106
     * @param string $command
107
     *
108
     * @return bool
109
     *
110
     * @throws \Exception When the command fails
111
     */
112
    protected function executeCommand(string $command): bool
113
    {
114
        $process = new Process($command);
115
        $process->run();
116
117
        return $process->isSuccessful();
118
    }
119
120
    /**
121
     * @param string $command
122
     *
123
     * @return bool
124
     *
125
     * @throws \Exception When connection to socket or command failed
126
     */
127
    protected function executeSocketCommand(string $command): bool
128
    {
129
        $config = config('varnish');
130
        $socket = new VarnishSocket();
131
132
        try {
133
            if ($socket->connect(
134
                $config['administrative_host'],
135
                $config['administrative_port'],
136
                $this->getSecret()
137
            )) {
138
                $socket->command($command);
139
                $socket->quit();
140
            }
141
        } catch(\Exception $e) {
142
            return false;
143
        } finally {
144
            $socket->close();
145
        }
146
147
        return true;
148
    }
149
}
150