Debian Codegraph @

includes/libs/Stats/OutputFormats.php

raw symbols SCIP
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 * @file
18 */
19
20 declare( strict_types=1 );
21
22 namespace Wikimedia\Stats;
23
24 use Wikimedia\Stats\Emitters\EmitterInterface;
25 use Wikimedia\Stats\Emitters\NullEmitter;
26 use Wikimedia\Stats\Emitters\UDPEmitter;
27 use Wikimedia\Stats\Exceptions\UnsupportedFormatException;
28 use Wikimedia\Stats\Formatters\DogStatsdFormatter;
29 use Wikimedia\Stats\Formatters\FormatterInterface;
30 use Wikimedia\Stats\Formatters\NullFormatter;
31 use Wikimedia\Stats\Formatters\StatsdFormatter;
32
33 /**
34 * Metrics Format and Output Helpers
35 *
36 * @author Cole White
37 * @since 1.41
38 */
39 class OutputFormats {
40
41 public const NULL = 1;
42 public const STATSD = 2;
43 public const DOGSTATSD = 3;
44
45 private const SUPPORTED_FORMATS = [
46 'null' => self::NULL,
47 'statsd' => self::STATSD,
48 'dogstatsd' => self::DOGSTATSD
49 ];
50
51 /**
52 * Convert friendly format name to integer.
53 *
54 * @param string $format
55 * @return int
56 */
57 public static function getFormatFromString( string $format ): int {
58 if ( self::SUPPORTED_FORMATS[$format] ?? false ) {
59 return self::SUPPORTED_FORMATS[$format];
60 }
61 throw new UnsupportedFormatException(
62 "Format '" . $format . "' not supported. Expected one of "
63 . json_encode( array_keys( self::SUPPORTED_FORMATS ) )
64 );
65 }
66
67 /**
68 * Returns an instance of the requested formatter.
69 *
70 * @param int $format
71 * @return FormatterInterface
72 */
73 public static function getNewFormatter( int $format ): FormatterInterface {
74 switch ( $format ) {
75 case self::DOGSTATSD:
76 return new DogStatsdFormatter();
77 case self::STATSD:
78 return new StatsdFormatter();
79 case self::NULL:
80 return new NullFormatter();
81 default:
82 throw new UnsupportedFormatException( 'Unsupported metrics format. Got format: ' . $format );
83 }
84 }
85
86 /**
87 * Returns an emitter instance appropriate the formatter instance.
88 *
89 * @param string $prefix
90 * @param StatsCache $cache
91 * @param FormatterInterface $formatter
92 * @param string|null $target
93 * @return EmitterInterface
94 */
95 public static function getNewEmitter(
96 string $prefix,
97 StatsCache $cache,
98 FormatterInterface $formatter,
99 ?string $target = null
100 ): EmitterInterface {
101 switch ( get_class( $formatter ) ) {
102 case StatsdFormatter::class:
103 case DogStatsdFormatter::class:
104 return new UDPEmitter( $prefix, $cache, $formatter, $target );
105 case NullFormatter::class:
106 return new NullEmitter;
107 default:
108 throw new UnsupportedFormatException(
109 'Unsupported metrics format. Got format: ' . get_class( $formatter )
110 );
111 }
112 }
113 }