Skip to content

Commit

Permalink
Merge pull request #3 from Slickdeals/add-aware-trait
Browse files Browse the repository at this point in the history
Add aware interface to easily auto-inject client
  • Loading branch information
bfeaver authored Jun 4, 2021
2 parents e44b710 + 8143f4a commit cd6fec0
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 38 deletions.
1 change: 1 addition & 0 deletions .cz.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"commitizen": {"name": "cz_conventional_commits", "version": "3.0.2", "tag_format": "$version"}}
2 changes: 1 addition & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: composer install --prefer-dist --no-progress --no-suggest --no-interaction

- name: phpcs
run: vendor/bin/phpcs -s --standard=vendor/flyeralarm/php-code-validator/ruleset.xml src/ tests/
run: vendor/bin/php-cs-fixer fix --diff --dry-run -v

- name: psalm
run: vendor/bin/psalm --show-info=false
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ stats.log
/vendor
/composer.lock
/composer.phar

# php-cs-fixer
.php-cs-fixer.cache
13 changes: 13 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;

$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR12' => true,
'array_syntax' => ['syntax' => 'short'],
])
->setFinder($finder)
;
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,33 @@ To disable sending any metrics to the statsd server, you can use the `Domnikl\St

class instead of the default socket abstraction. This may be incredibly useful for feature flags. Another options is
to use `Domnikl\Statsd\Connection\InMemory` connection class, that will collect your messages but won't actually send them.

## StatsdAwareInterface

You can use the `StatsdAwareInterface` and `StatsdAwareTrait` in order to have dependency injection containers (such as
Symfony's DI component) automatically detect the StatsdAwareInterface and inject the client into your service.

### Symfony

```yaml
# config/services.yaml
services:
_instanceof:
Domnikl\Statsd\StatsdAwareInterface:
calls:
- [setStatsdClient, ['@Domnikl\Statsd\Client']]

Domnikl\Statsd\Client:
arguments:
$connection: '@app.statsd_connection'
$namespace: '<namespace>'

app.statsd_connection:
class: Domnikl\Statsd\Connection\UdpSocket
arguments:
$host: '%env(STATSD_HOST)%'
$port: '%env(STATSD_PORT)%'
```
## Authors
Original author: Dominik Liebler <[email protected]>
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
},
"require-dev": {
"phpunit/phpunit": "^9",
"flyeralarm/php-code-validator": "^3.2",
"vimeo/psalm": "^4.6"
"vimeo/psalm": "^4.6",
"friendsofphp/php-cs-fixer": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
11 changes: 0 additions & 11 deletions phpcs.xml.dist

This file was deleted.

14 changes: 14 additions & 0 deletions src/StatsdAwareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Domnikl\Statsd;

/**
* Describes a StatsD-aware instance.
*/
interface StatsdAwareInterface
{
/**
* Sets a StatsD client instance on the object.
*/
public function setStatsdClient(Client $client): void;
}
22 changes: 22 additions & 0 deletions src/StatsdAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Domnikl\Statsd;

/**
* Basic implementation of StatsdAwareInterface.
*/
trait StatsdAwareTrait
{
/**
* @var Client
*/
protected $statsd;

/**
* Sets the StatsD client.
*/
public function setStatsdClient(Client $client)
{
$this->statsd = $client;
}
}
48 changes: 24 additions & 24 deletions tests/unit/ClientBatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,47 +32,47 @@ public function testInit()

public function testStartBatch()
{
$this->client->startBatch();
$this->assertTrue($this->client->isBatch());
$this->client->startBatch();
$this->assertTrue($this->client->isBatch());
}

public function testSendIsRecordingInBatch()
{
$this->client->startBatch();
$this->client->increment("foobar", 1);
$this->client->startBatch();
$this->client->increment("foobar", 1);

$message = $this->connection->getLastMessage();
$this->assertNull($message);
$message = $this->connection->getLastMessage();
$this->assertNull($message);
}

public function testEndBatch()
{
$this->client->startBatch();
$this->client->count("foobar", 1);
$this->client->count("foobar", 2);
$this->client->endBatch();
$this->client->startBatch();
$this->client->count("foobar", 1);
$this->client->count("foobar", 2);
$this->client->endBatch();

$this->assertFalse($this->client->isBatch());
$this->assertSame("foobar:1|c\nfoobar:2|c", $this->connection->getLastMessage());
$this->assertFalse($this->client->isBatch());
$this->assertSame("foobar:1|c\nfoobar:2|c", $this->connection->getLastMessage());

// run a new batch => don't send the old values!
// run a new batch => don't send the old values!

$this->client->startBatch();
$this->client->count("baz", 100);
$this->client->count("baz", 300);
$this->client->endBatch();
$this->client->startBatch();
$this->client->count("baz", 100);
$this->client->count("baz", 300);
$this->client->endBatch();

$this->assertFalse($this->client->isBatch());
$this->assertSame("baz:100|c\nbaz:300|c", $this->connection->getLastMessage());
$this->assertFalse($this->client->isBatch());
$this->assertSame("baz:100|c\nbaz:300|c", $this->connection->getLastMessage());
}

public function testCancelBatch()
{
$this->client->startBatch();
$this->client->count("foobar", 4);
$this->client->cancelBatch();
$this->client->startBatch();
$this->client->count("foobar", 4);
$this->client->cancelBatch();

$this->assertFalse($this->client->isBatch());
$this->assertNull($this->connection->getLastMessage());
$this->assertFalse($this->client->isBatch());
$this->assertNull($this->connection->getLastMessage());
}
}

0 comments on commit cd6fec0

Please sign in to comment.