Skip to content

Commit b9650b2

Browse files
Merge pull request #142 from neo4j-php/analytics-submit-update
update sending analytics
2 parents 438c021 + f04bcd4 commit b9650b2

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

src/Bolt.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,8 @@ final class Bolt
2626

2727
public function __construct(private IConnection $connection)
2828
{
29-
$_ENV['TEMP_DIR'] = getenv('TEMP') ?: getenv('TMPDIR') ?: (dirname(__DIR__) . DIRECTORY_SEPARATOR . 'temp');
30-
var_dump($_ENV['TEMP_DIR']);
31-
if (!file_exists($_ENV['TEMP_DIR'])) {
32-
mkdir($_ENV['TEMP_DIR'], recursive: true);
33-
}
34-
35-
if (!getenv('BOLT_ANALYTICS_OPTOUT')) {
29+
$this->tempDirectoryInit();
30+
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
3631
if (!file_exists($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
3732
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics');
3833
}
@@ -42,18 +37,31 @@ public function __construct(private IConnection $connection)
4237
$this->setProtocolVersions(5.4, 5, 4.4);
4338
}
4439

40+
private function tempDirectoryInit(): void
41+
{
42+
$_ENV['TEMP_DIR'] = getenv('TEMP');
43+
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
44+
$_ENV['TEMP_DIR'] = getenv('TMPDIR');
45+
}
46+
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
47+
$_ENV['TEMP_DIR'] = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'temp';
48+
}
49+
if (!file_exists($_ENV['TEMP_DIR'])) {
50+
mkdir($_ENV['TEMP_DIR'], recursive: true);
51+
}
52+
}
53+
4554
private function track(): void
4655
{
4756
foreach (glob($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.*.cnt') as $file) {
4857
$time = intval(explode('.', basename($file))[1]);
4958
if ($time < strtotime('today')) {
5059
$count = file_get_contents($file);
51-
unlink($file);
5260

5361
$curl = curl_init();
5462
curl_setopt_array($curl, [
5563
CURLOPT_URL => 'https://api-eu.mixpanel.com/import?strict=0&project_id=3355308',
56-
CURLOPT_RETURNTRANSFER => true,
64+
CURLOPT_RETURNTRANSFER => false,
5765
CURLOPT_ENCODING => '',
5866
CURLOPT_MAXREDIRS => 10,
5967
CURLOPT_TIMEOUT => $this->connection->getTimeout(),
@@ -77,7 +85,10 @@ private function track(): void
7785
'authorization: Basic MDJhYjRiOWE2YTM4MThmNWFlZDEzYjNiMmE5M2MxNzQ6',
7886
],
7987
]);
80-
curl_exec($curl);
88+
89+
if (curl_exec($curl)) {
90+
unlink($file);
91+
}
8192
curl_close($curl);
8293
}
8394
}

src/protocol/AProtocol.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,19 +63,19 @@ public function __construct(
6363
*/
6464
protected function write(iterable $generator): void
6565
{
66-
if (!getenv('BOLT_ANALYTICS_OPTOUT')) {
67-
$this->track();
68-
}
66+
$this->track();
6967
foreach ($generator as $buffer) {
7068
$this->connection->write($buffer);
7169
}
7270
}
7371

7472
private function track(): void
7573
{
76-
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
77-
$count = file_exists($file) ? intval(file_get_contents($file)) : 0;
78-
file_put_contents($file, $count + 1);
74+
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR']. DIRECTORY_SEPARATOR)) {
75+
$file = $_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR . 'queries.' . strtotime('today') . '.cnt';
76+
$count = file_exists($file) ? intval(file_get_contents($file)) : 0;
77+
file_put_contents($file, $count + 1);
78+
}
7979
}
8080

8181
/**

tests/protocol/ProtocolLayer.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,18 @@ public function readCallback(int $length = 2048): string
100100
*/
101101
protected function setUp(): void
102102
{
103-
$_ENV['TEMP_DIR'] = getenv('TEMP') ?: getenv('TMPDIR') ?: (dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'temp');
103+
$_ENV['TEMP_DIR'] = getenv('TEMP');
104+
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
105+
$_ENV['TEMP_DIR'] = getenv('TMPDIR');
106+
}
107+
if ($_ENV['TEMP_DIR'] === false || !is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
108+
$_ENV['TEMP_DIR'] = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'temp';
109+
}
104110
if (!file_exists($_ENV['TEMP_DIR'])) {
105111
mkdir($_ENV['TEMP_DIR'], recursive: true);
106112
}
107113

108-
if (!getenv('BOLT_ANALYTICS_OPTOUT')) {
114+
if (!getenv('BOLT_ANALYTICS_OPTOUT') && is_writable($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR)) {
109115
if (!file_exists($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics' . DIRECTORY_SEPARATOR)) {
110116
mkdir($_ENV['TEMP_DIR'] . DIRECTORY_SEPARATOR . 'php-bolt-analytics');
111117
}

0 commit comments

Comments
 (0)