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.

ClearCodeCaches::assemble()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 1
nop 0
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
1
<?php
2
/**
3
 * See class comment
4
 *
5
 * PHP Version 5
6
 *
7
 * @category Netresearch
8
 * @package  Netresearch\Kite\Workflow
9
 * @author   Christian Opitz <[email protected]>
10
 * @license  http://www.netresearch.de Netresearch Copyright
11
 * @link     http://www.netresearch.de
12
 */
13
14
namespace Netresearch\Kite\Workflow;
15
use Netresearch\Kite\Workflow;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
/**
19
 * Clears code caches not available from shell and calls (statcache, opcache and apc).
20
 *
21
 * Creates a PHP script on the nodes or locally and calls it via the webUrl or node.webUrl
22
 *
23
 * @category Netresearch
24
 * @package  Netresearch\Kite\Workflow
25
 * @author   Christian Opitz <[email protected]>
26
 * @license  http://www.netresearch.de Netresearch Copyright
27
 * @link     http://www.netresearch.de
28
 */
29
class ClearCodeCaches extends Workflow
30
{
31
    /**
32
     * Variable configuration
33
     *
34
     * @return array
35
     */
36
    protected function configureVariables()
37
    {
38
        return array(
39
            'webUrl' => array(
40
                'type' => 'string',
41
                'label' => 'URL to the current web root. Set this if you want to clear caches locally - otherwise this WF will clear the node(s) caches'
42
            ),
43
            'baseDir' => array(
44
                'type' => 'string',
45
                'default' => '{config["workspace"]}',
46
                'label' => 'Path relative to current application root and webUrl, where the temp script will be stored'
47
            )
48
        ) + parent::configureVariables();
49
    }
50
51
52
    /**
53
     * Override to assemble the tasks
54
     *
55
     * @return void
56
     */
57
    public function assemble()
58
    {
59
        $this->callback(
60
            function () {
61
                $scriptPath = $this->createScript();
62
                if ($webUrl = $this->get('webUrl')) {
63
                    $this->callScript($webUrl, $scriptPath);
64
                } else {
65
                    $this->scp($scriptPath, '{node}:{node.webRoot}/' . $scriptPath);
66
                    foreach ($this->get('nodes') as $node) {
67
                        /* @var \Netresearch\Kite\Node $node */
68
                        if ($webUrl = $node->get('webUrl', null)) {
69
                            $this->callScript($webUrl, $scriptPath);
70
                        } else {
71
                            $this->console->output("<warning>Node $node ({$node->get('id')}) has no webUrl set</warning>");
72
                        }
73
                    }
74
                }
75
            }
76
        );
77
    }
78
79
    /**
80
     * Create the script
81
     *
82
     * @return string The relative script path
83
     */
84
    protected function createScript()
85
    {
86
        $scriptPath = $this->get('baseDir') . '/ccc-' . uniqid() . '.php';
87
        file_put_contents(
88
            $scriptPath,
89
            '<?' . "php
90
            if (function_exists('clearstatcache')) {
91
                echo 'statcache|';
92
                clearstatcache(true);
93
            }
94
            if (function_exists('opcache_reset')) {
95
                echo 'opcache|';
96
                opcache_reset();
97
            }
98
            if (function_exists('apc_clear_cache')) {
99
                echo 'apc|';
100
                apc_clear_cache();
101
            }
102
            if (function_exists('xcache_clear_cache')) {
103
                echo 'xcache|';
104
                xcache_clear_cache(XC_TYPE_PHP);
105
            }
106
            unlink(__FILE__);
107
            echo 'SUCCESS';
108
            ?>\n"
109
        );
110
        return $scriptPath;
111
    }
112
113
    /**
114
     * Call the script via web URL
115
     *
116
     * @param string $baseUrl    URL to web root
117
     * @param string $scriptPath Path to script (relative to baseUrl)
118
     *
119
     * @return void
120
     */
121
    protected function callScript($baseUrl, $scriptPath)
122
    {
123
        $url = rtrim($baseUrl, '/') . '/' . ltrim($scriptPath, '/');
124
125
        $this->console->output('Calling ' . $url, OutputInterface::VERBOSITY_DEBUG);
126
        $ch = curl_init();
127
        curl_setopt($ch, CURLOPT_URL, $url);
128
        curl_setopt($ch, CURLOPT_HEADER, 1);
129
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
130
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
131
        $response = curl_exec($ch);
132
        $content = substr($response, curl_getinfo($ch, CURLINFO_HEADER_SIZE));
133
134
        $caches = explode('|', $content);
135
        if (array_pop($caches) !== 'SUCCESS') {
136
            $this->console->output("<error>Clearcache not run successfully</error>");
137
            $this->console->indent();
138
            if ($response === false) {
139
                $msg = curl_error($ch);
140
            } else {
141
                $msg = $response;
142
            }
143
            $this->console->output($msg, OutputInterface::VERBOSITY_DEBUG);
144
            $this->console->outdent();
145
        } else {
146
            if ($caches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $caches of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
147
                $this->console->output('Cleared code caches (' . implode(', ', $caches) . ')');
148
            } else {
149
                $this->console->output('No code caches found');
150
            }
151
        }
152
    }
153
}
154
155
?>
156