| 1 |
<?php |
| 2 |
|
| 3 |
namespace Liuggio\StatsdClient\Factory; |
| 4 |
|
| 5 |
use Liuggio\StatsdClient\Entity\StatsdDataInterface; |
| 6 |
|
| 7 |
class StatsdDataFactory implements StatsdDataFactoryInterface |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var StatsdDataInterface |
| 11 |
*/ |
| 12 |
private $entityClass; |
| 13 |
|
| 14 |
public function __construct($entity_class = '\Liuggio\StatsdClient\Entity\StatsdData') |
| 15 |
{ |
| 16 |
$this->setEntityClass($entity_class); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* {@inheritDoc} |
| 21 |
**/ |
| 22 |
public function timing($key, $time) |
| 23 |
{ |
| 24 |
return $this->produceStatsdData($key, $time, StatsdDataInterface::STATSD_METRIC_TIMING); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* {@inheritDoc} |
| 29 |
**/ |
| 30 |
public function gauge($key, $value) |
| 31 |
{ |
| 32 |
return $this->produceStatsdData($key, $value, StatsdDataInterface::STATSD_METRIC_GAUGE); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* {@inheritDoc} |
| 37 |
**/ |
| 38 |
public function set($key, $value) |
| 39 |
{ |
| 40 |
return $this->produceStatsdData($key, $value, StatsdDataInterface::STATSD_METRIC_SET); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* {@inheritDoc} |
| 45 |
**/ |
| 46 |
public function increment($key) |
| 47 |
{ |
| 48 |
return $this->produceStatsdData($key, 1, StatsdDataInterface::STATSD_METRIC_COUNT); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* {@inheritDoc} |
| 53 |
**/ |
| 54 |
public function decrement($key) |
| 55 |
{ |
| 56 |
return $this->produceStatsdData($key, -1, StatsdDataInterface::STATSD_METRIC_COUNT); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* {@inheritDoc} |
| 61 |
**/ |
| 62 |
public function updateCount($key, $delta) |
| 63 |
{ |
| 64 |
return $this->produceStatsdData($key, $delta, StatsdDataInterface::STATSD_METRIC_COUNT); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* {@inheritDoc} |
| 69 |
**/ |
| 70 |
public function produceStatsdData($key, $value = 1, $metric = StatsdDataInterface::STATSD_METRIC_COUNT) |
| 71 |
{ |
| 72 |
$statsdData = $this->produceStatsdDataEntity(); |
| 73 |
|
| 74 |
if (null !== $key) { |
| 75 |
$statsdData->setKey($key); |
| 76 |
} |
| 77 |
|
| 78 |
if (null !== $value) { |
| 79 |
$statsdData->setValue($value); |
| 80 |
} |
| 81 |
|
| 82 |
if (null !== $metric) { |
| 83 |
$statsdData->setMetric($metric); |
| 84 |
} |
| 85 |
|
| 86 |
return $statsdData; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* {@inheritDoc} |
| 91 |
**/ |
| 92 |
public function produceStatsdDataEntity() |
| 93 |
{ |
| 94 |
$statsdData = $this->getEntityClass(); |
| 95 |
|
| 96 |
return new $statsdData(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* {@inheritDoc} |
| 101 |
**/ |
| 102 |
public function setFailSilently($failSilently) |
| 103 |
{ |
| 104 |
$this->failSilently = $failSilently; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* {@inheritDoc} |
| 109 |
**/ |
| 110 |
public function getFailSilently() |
| 111 |
{ |
| 112 |
return $this->failSilently; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* {@inheritDoc} |
| 117 |
**/ |
| 118 |
public function setEntityClass($entityClass) |
| 119 |
{ |
| 120 |
$this->entityClass = $entityClass; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* {@inheritDoc} |
| 125 |
**/ |
| 126 |
public function getEntityClass() |
| 127 |
{ |
| 128 |
return $this->entityClass; |
| 129 |
} |
| 130 |
} |