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

Commit e548b35

Browse files
committed
Fixes for PR comments, updated phpunit livrary version
1 parent 0215a94 commit e548b35

File tree

14 files changed

+85
-46
lines changed

14 files changed

+85
-46
lines changed

.travis.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ language: php
33
php:
44
- '5.6'
55
- '7.0'
6-
7-
install:
8-
- composer install -n
6+
- '7.1'
97

108
script:
11-
- phpunit
9+
- composer install -n
10+
- vendor/bin/phpunit
11+
- rm -rf vendor
12+
- composer update --prefer-lowest -n
13+
- vendor/bin/phpunit

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"behat/transliterator": "^1.2"
1919
},
2020
"require-dev": {
21-
"phpunit/phpunit": "^4.8.0",
22-
"friendsofphp/php-cs-fixer": "^2.3"
21+
"friendsofphp/php-cs-fixer": "^2.3",
22+
"phpunit/phpunit": "^5.7"
2323
}
2424
}

src/Bucket.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace HelloFresh\Stats;
44

5-
5+
/**
6+
* Describes a bucket instance, responsible for building different types of metrics for an operation.
7+
*/
68
interface Bucket
79
{
810
const TOTAL_BUCKET = 'total';
@@ -18,21 +20,29 @@ interface Bucket
1820
const DEFAULT_HTTP_REQUEST_SECTION = 'request';
1921

2022
/**
23+
* Builds simple metric name in the form "<section>.<operation-0>.<operation-1>.<operation-2>".
24+
*
2125
* @return string
2226
*/
2327
public function metric();
2428

2529
/**
30+
* Builds metric name with success suffix in the form "<section>-ok|fail.<operation-0>.<operation-1>.<operation-2>".
31+
*
2632
* @return string
2733
*/
2834
public function metricWithSuffix();
2935

3036
/**
37+
* Builds simple total metric name in the form total.<section>".
38+
*
3139
* @return string
3240
*/
3341
public function metricTotal();
3442

3543
/**
44+
* Builds total metric name with success suffix in the form total-ok|fail.<section>"
45+
*
3646
* @return string
3747
*/
3848
public function metricTotalWithSuffix();

src/Bucket/HTTPRequest.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88

99
class HTTPRequest extends Plain
1010
{
11-
/** @var RequestInterface */
12-
protected $request;
13-
/** @var HTTPMetricAlterCallback */
14-
protected $httpMetricAlterCallback;
15-
1611
/**
1712
* HTTPRequest constructor.
1813
*
@@ -23,22 +18,22 @@ class HTTPRequest extends Plain
2318
*/
2419
public function __construct($section, RequestInterface $request, $success, HTTPMetricAlterCallback $callback = null)
2520
{
26-
$this->request = $request;
27-
$this->httpMetricAlterCallback = $callback;
28-
29-
parent::__construct($section, $this->buildMetricOperation(), $success);
21+
parent::__construct($section, $this->buildMetricOperation($request, $callback), $success);
3022
}
3123

3224
/**
33-
* @return MetricOperation
25+
* @param RequestInterface $request
26+
* @param HTTPMetricAlterCallback $callback
27+
*
28+
* @return MetricOperation|mixed
3429
*/
35-
public function buildMetricOperation()
30+
public function buildMetricOperation(RequestInterface $request, HTTPMetricAlterCallback $callback)
3631
{
37-
$operation = new MetricOperation(strtolower($this->request->getMethod()));
38-
if ($this->request->getUri()->getPath() != '/') {
32+
$operation = new MetricOperation(strtolower($request->getMethod()));
33+
if ($request->getUri()->getPath() !== '/') {
3934
$partsFilled = 1;
40-
foreach (explode('/', $this->request->getUri()->getPath()) as $fragment) {
41-
if ($fragment == '') {
35+
foreach (explode('/', $request->getUri()->getPath()) as $fragment) {
36+
if ($fragment === '') {
4237
continue;
4338
}
4439

@@ -50,8 +45,8 @@ public function buildMetricOperation()
5045
}
5146
}
5247

53-
if (null != $this->httpMetricAlterCallback) {
54-
$operation = call_user_func_array($this->httpMetricAlterCallback, [$operation, $this->request]);
48+
if (null !== $callback) {
49+
$operation = call_user_func_array($callback, [$operation, $request]);
5550
}
5651

5752
return $operation;

src/Bucket/MetricOperation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(array $operations = [])
2121
// ensure that operations is not bigger than allowed
2222
array_splice($operations, static::LENGTH);
2323

24-
foreach ($operations as $key => $value) {
24+
foreach (array_values($operations) as $key => $value) {
2525
$this->offsetSet($key, $value);
2626
}
2727

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use HelloFresh\Stats\Timer;
1313
use Psr\Http\Message\RequestInterface;
1414

15-
abstract class Base implements Client
15+
abstract class AbstractClient implements Client
1616
{
1717
/** @var string */
1818
protected $httpRequestSection;

src/Client/Log.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@
99
use HelloFresh\Stats\Timer;
1010
use Psr\Log\LoggerInterface;
1111

12-
class Log extends Base implements Client
12+
class Log extends AbstractClient implements Client
1313
{
1414
/** @var LoggerInterface */
1515
protected $logger;
1616

17+
/** @var Incrementer\Log */
18+
protected $incrementer;
19+
/** @var State\Log */
20+
protected $state;
21+
1722
/**
1823
* Log constructor.
1924
*
@@ -38,14 +43,20 @@ public function buildTimer()
3843
*/
3944
protected function getIncrementer()
4045
{
41-
return new Incrementer\Log($this->logger);
46+
if (null === $this->incrementer) {
47+
$this->incrementer = new Incrementer\Log($this->logger);
48+
}
49+
return $this->incrementer;
4250
}
4351

4452
/**
4553
* @inheritdoc
4654
*/
4755
protected function getState()
4856
{
49-
return new State\Log($this->logger);
57+
if (null === $this->state) {
58+
$this->state = new State\Log($this->logger);
59+
}
60+
return $this->state;
5061
}
5162
}

src/Client/NoOp.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
use HelloFresh\Stats\State;
99
use HelloFresh\Stats\Timer\Memory;
1010

11-
class NoOp extends Base implements Client
11+
class NoOp extends AbstractClient implements Client
1212
{
13+
/** @var Incrementer\NoOp */
14+
protected $incrementer;
15+
/** @var State\NoOp */
16+
protected $state;
17+
1318
/**
1419
* @inheritdoc
1520
*/
@@ -19,18 +24,24 @@ public function buildTimer()
1924
}
2025

2126
/**
22-
* @return Incrementer
27+
* @inheritdoc
2328
*/
2429
protected function getIncrementer()
2530
{
26-
return new Incrementer\NoOp();
31+
if (null === $this->incrementer) {
32+
$this->incrementer = new Incrementer\NoOp();
33+
}
34+
return $this->incrementer;
2735
}
2836

2937
/**
30-
* @return State
38+
* @inheritdoc
3139
*/
3240
protected function getState()
3341
{
34-
return new State\NoOp();
42+
if (null === $this->state) {
43+
$this->state = new State\NoOp();
44+
}
45+
return $this->state;
3546
}
3647
}

src/Client/StatsD.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use HelloFresh\Stats\Timer\StatsD as StatsDTimer;
1111
use League\StatsD\Client as StatsDClient;
1212

13-
class StatsD extends Base implements Client
13+
class StatsD extends AbstractClient implements Client
1414
{
1515
/** @var string */
1616
protected $httpRequestSection;
@@ -19,19 +19,24 @@ class StatsD extends Base implements Client
1919
/** @var StatsDClient */
2020
protected $client;
2121

22+
/** @var Incrementer\StatsD */
23+
protected $incrementer;
24+
/** @var State\StatsD */
25+
protected $state;
26+
2227
/**
2328
* StatsD constructor.
2429
*
2530
* @param string $dsn statsd connection dsn
2631
*/
2732
public function __construct($dsn)
2833
{
29-
$url = parse_url($dsn);
34+
$url = (array)parse_url($dsn);
3035

3136
$params = parse_str(empty($url['query']) ? '' : $url['query']);
3237
$options = [
33-
'host' => $url['host'],
34-
'port' => $url['port'],
38+
'host' => empty($url['host']) ? 'localhost' : '',
39+
'port' => empty($url['port']) ? $url['port'] : 8125,
3540
'namespace' => empty($params['ns']) ? '' : $params['ns'],
3641
'timeout' => empty($params['timeout']) ? null : (float)$params['timeout'],
3742
'throwConnectionExceptions' => empty($params['error']) ? true : (bool)$params['error'],
@@ -53,14 +58,20 @@ public function buildTimer()
5358
*/
5459
protected function getIncrementer()
5560
{
56-
return new Incrementer\StatsD($this->client);
61+
if (null === $this->incrementer) {
62+
$this->incrementer = new Incrementer\StatsD($this->client);
63+
}
64+
return $this->incrementer;
5765
}
5866

5967
/**
6068
* @inheritdoc
6169
*/
6270
protected function getState()
6371
{
64-
return new State\StatsD($this->client);
72+
if (null === $this->state) {
73+
$this->state = new State\StatsD($this->client);
74+
}
75+
return $this->state;
6576
}
6677
}

src/Factory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ class Factory
2525
*/
2626
public static function build($dsn, LoggerInterface $logger)
2727
{
28-
$url = parse_url($dsn);
29-
switch ($url['scheme']) {
28+
switch (parse_url($dsn, PHP_URL_SCHEME)) {
3029
case static::STATSD:
3130
return new StatsD($dsn);
3231

0 commit comments

Comments
 (0)