OpcacheResetCommandHelper::handleResponse()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 6
eloc 13
nc 6
nop 1
1
<?php
2
3
namespace Timegryd\OpcacheResetBundle\Helper;
4
5
use Symfony\Component\HttpKernel\KernelInterface;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\Filesystem\Filesystem;
8
use GuzzleHttp\Psr7\Response as HttpResponse;
9
10
class OpcacheResetCommandHelper
11
{
12
    const FILE_NAME_MODEL = 'opcache-reset-%s.php';
13
14
    /**
15
     * @var KernelInterface
16
     */
17
    protected $kernel;
18
19
    public function __construct(KernelInterface $kernel)
20
    {
21
        $this->kernel = $kernel;
22
    }
23
24
    /**
25
     * Return generated file name.
26
     *
27
     * @param string $replacement
28
     *
29
     * @return string
30
     */
31
    public function getFileName($replacement)
32
    {
33
        return sprintf(self::FILE_NAME_MODEL, $replacement);
34
    }
35
36
    /**
37
     * Return generated file path.
38
     *
39
     * @param string $dir
40
     * @param string $replacement
41
     *
42
     * @return string
43
     */
44
    public function getFilePath($dir, $replacement)
45
    {
46
        return sprintf('%s/%s', $dir, $this->getFileName($replacement));
47
    }
48
49
    /**
50
     * Generate url with given host and token.
51
     *
52
     * @param string $host
53
     * @param string $token
54
     *
55
     * @return string
56
     */
57
    public function generateUrl($host, $token)
58
    {
59
        return sprintf('%s/%s', $host, $this->getFileName($token));
60
    }
61
62
    /**
63
     * Create file in given directory.
64
     *
65
     * @param string $dir
66
     * @param string $token
67
     *
68
     * @return OpcacheResetCommandHelper
69
     */
70
    public function createFile($dir, $token)
71
    {
72
        if (!is_dir($dir)) {
73
            throw new \RuntimeException(sprintf('Web directory does not exists : %s', $dir));
74
        }
75
76
        $templatePath = $this->kernel->locateResource('@TimegrydOpcacheResetBundle/Resources/template/opcacheReset.php');
77
78
        $fileSystem = new FileSystem();
79
80
        $fileSystem->copy(
81
            $templatePath,
0 ignored issues
show
Bug introduced by
It seems like $templatePath defined by $this->kernel->locateRes...late/opcacheReset.php') on line 76 can also be of type array; however, Symfony\Component\Filesystem\Filesystem::copy() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
82
            $this->getFilePath($dir, $token)
83
        );
84
85
        return $this;
86
    }
87
88
    /**
89
     * Remove all opcache reset files.
90
     *
91
     * @param string $dir
92
     *
93
     * @return OpcacheResetCommandHelper
94
     */
95
    public function clean($dir)
96
    {
97
        array_map('unlink', glob($this->getFilePath($dir, '*')));
98
99
        return $this;
100
    }
101
102
    /**
103
     * Handle given response and return message from it.
104
     *
105
     * @param HttpResponse $response
106
     *
107
     * @return string
108
     */
109
    public function handleResponse(HttpResponse $response)
110
    {
111
        if (Response::HTTP_OK !== $response->getStatusCode()) {
112
            throw new \RuntimeException('Unable to access url');
113
        }
114
115
        $result = json_decode($response->getBody());
116
117
        if (null === $result) {
118
            throw new \RuntimeException('Unable to decode json response.');
119
        }
120
121
        if (!property_exists($result, 'success')) {
122
            throw new \InvalidArgumentException('"success" property is missing in returned json.');
123
        }
124
125
        if (!property_exists($result, 'message')) {
126
            throw new \InvalidArgumentException('"message" property is missing in returned json.');
127
        }
128
129
        if (false === $result->success) {
130
            throw new \RuntimeException($result->message);
131
        }
132
133
        return $result->message;
134
    }
135
}
136