Merge "Simplify HTMLTitleTextField::validate"
[lhc/web/wiklou.git] / includes / libs / stats / PrefixingStatsdDataFactoryProxy.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
22 use Liuggio\StatsdClient\Entity\StatsdDataInterface;
23
24 /**
25 * Proxy to prefix metric keys sent to a StatsdDataFactoryInterface
26 *
27 * @since 1.32
28 */
29 class PrefixingStatsdDataFactoryProxy implements StatsdDataFactoryInterface {
30
31 /**
32 * @var string
33 */
34 private $prefix;
35
36 /**
37 * @var StatsdDataFactoryInterface
38 */
39 private $factory;
40
41 /**
42 * @param StatsdDataFactoryInterface $factory
43 * @param string $prefix
44 */
45 public function __construct(
46 StatsdDataFactoryInterface $factory,
47 $prefix
48 ) {
49 $this->factory = $factory;
50 $this->prefix = rtrim( $prefix, '.' );
51 }
52
53 /**
54 * @param string $key
55 * @return string
56 */
57 private function addPrefixToKey( $key ) {
58 return $this->prefix . '.' . $key;
59 }
60
61 public function timing( $key, $time ) {
62 return $this->factory->timing( $this->addPrefixToKey( $key ), $time );
63 }
64
65 public function gauge( $key, $value ) {
66 return $this->factory->gauge( $this->addPrefixToKey( $key ), $value );
67 }
68
69 public function set( $key, $value ) {
70 return $this->factory->set( $this->addPrefixToKey( $key ), $value );
71 }
72
73 public function increment( $key ) {
74 return $this->factory->increment( $this->addPrefixToKey( $key ) );
75 }
76
77 public function decrement( $key ) {
78 return $this->factory->decrement( $this->addPrefixToKey( $key ) );
79 }
80
81 public function updateCount( $key, $delta ) {
82 return $this->factory->updateCount( $this->addPrefixToKey( $key ), $delta );
83 }
84
85 public function produceStatsdData(
86 $key,
87 $value = 1,
88 $metric = StatsdDataInterface::STATSD_METRIC_COUNT
89 ) {
90 return $this->factory->produceStatsdData(
91 $this->addPrefixToKey( $key ),
92 $value,
93 $metric
94 );
95 }
96 }