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 (#28)
by
unknown
01:26
created

Varnish::flush()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 15
nc 3
nop 1
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)
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:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
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:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

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