Skip to content
This repository was archived by the owner on Dec 22, 2025. It is now read-only.

Commit 79e8262

Browse files
author
Eduardo Kasper
committed
[PATCH/PO-2958] PO-2958: creates a client that does not throw an error if configuration is set to
1 parent 81db372 commit 79e8262

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/StatsD/SilentClient.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace HelloFresh\Stats\StatsD;
3+
4+
use League\StatsD\Client;
5+
use League\StatsD\Exception\ConnectionException;
6+
7+
class SilentClient extends Client
8+
{
9+
/**
10+
* @inheritdoc
11+
*/
12+
protected function send(array $data, array $tags = []) : Client
13+
{
14+
$tagsData = $this->serializeTags(array_replace($this->tags, $tags));
15+
16+
try {
17+
$socket = $this->getSocket();
18+
$messages = [];
19+
$prefix = $this->namespace ? $this->namespace . '.' : '';
20+
foreach ($data as $key => $value) {
21+
$messages[] = $prefix . $key . ':' . $value . $tagsData;
22+
}
23+
$this->message = implode("\n", $messages);
24+
@fwrite($socket, $this->message);
25+
fflush($socket);
26+
} catch (ConnectionException $e) {
27+
if ($this->throwConnectionExceptions) {
28+
throw $e;
29+
}
30+
}
31+
32+
return $this;
33+
}
34+
}

tests/StatsD/SilentClientTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use HelloFresh\Stats\StatsD\SilentClient;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class SilentClientTest extends TestCase
7+
{
8+
public function testNewInstance()
9+
{
10+
$client = new SilentClient();
11+
$this->assertInstanceOf(SilentClient::class, $client);
12+
$this->assertRegExp('/^StatsD\\\Client::\[[a-zA-Z0-9]+\]$/', (string) $client);
13+
}
14+
15+
public function testStaticInstance()
16+
{
17+
$client1 = SilentClient::instance('instance1');
18+
$this->assertInstanceOf(SilentClient::class, $client1);
19+
$client2 = SilentClient::instance('instance2');
20+
$client3 = SilentClient::instance('instance1');
21+
$this->assertEquals('StatsD\Client::[instance2]', (string) $client2);
22+
$this->assertFalse((string) $client1 === (string) $client2);
23+
$this->assertTrue((string) $client1 === (string) $client3);
24+
}
25+
}

0 commit comments

Comments
 (0)