Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
25.00% covered (danger)
25.00%
4 / 16
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
OutputFormats
25.00% covered (danger)
25.00%
4 / 16
0.00% covered (danger)
0.00%
0 / 3
72.75
0.00% covered (danger)
0.00%
0 / 1
 getFormatFromString
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 getNewFormatter
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
12.41
 getNewEmitter
50.00% covered (danger)
50.00%
2 / 4
0.00% covered (danger)
0.00%
0 / 1
8.12
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7declare( strict_types=1 );
8
9namespace Wikimedia\Stats;
10
11use Wikimedia\Stats\Emitters\EmitterInterface;
12use Wikimedia\Stats\Emitters\NullEmitter;
13use Wikimedia\Stats\Emitters\UDPEmitter;
14use Wikimedia\Stats\Exceptions\UnsupportedFormatException;
15use Wikimedia\Stats\Formatters\DogStatsdFormatter;
16use Wikimedia\Stats\Formatters\FormatterInterface;
17use Wikimedia\Stats\Formatters\NullFormatter;
18use Wikimedia\Stats\Formatters\StatsdFormatter;
19
20/**
21 * Metrics Format and Output Helpers
22 *
23 * @author Cole White
24 * @since 1.41
25 */
26class OutputFormats {
27
28    public const NULL = 1;
29    public const STATSD = 2;
30    public const DOGSTATSD = 3;
31
32    private const SUPPORTED_FORMATS = [
33        'null' => self::NULL,
34        'statsd' => self::STATSD,
35        'dogstatsd' => self::DOGSTATSD
36    ];
37
38    /**
39     * Convert friendly format name to integer.
40     *
41     * @param string $format
42     * @return int
43     */
44    public static function getFormatFromString( string $format ): int {
45        if ( self::SUPPORTED_FORMATS[$format] ?? false ) {
46            return self::SUPPORTED_FORMATS[$format];
47        }
48        throw new UnsupportedFormatException(
49            "Format '" . $format . "' not supported. Expected one of "
50            . json_encode( array_keys( self::SUPPORTED_FORMATS ) )
51        );
52    }
53
54    /**
55     * Returns an instance of the requested formatter.
56     *
57     * @param int $format
58     * @return FormatterInterface
59     */
60    public static function getNewFormatter( int $format ): FormatterInterface {
61        switch ( $format ) {
62            case self::DOGSTATSD:
63                return new DogStatsdFormatter();
64            case self::STATSD:
65                return new StatsdFormatter();
66            case self::NULL:
67                return new NullFormatter();
68            default:
69                throw new UnsupportedFormatException(
70                    "Unsupported metrics format '{$format}' - See OutputFormats::class."
71                );
72        }
73    }
74
75    /**
76     * Returns an emitter instance appropriate the formatter instance.
77     *
78     * @param string $prefix
79     * @param StatsCache $cache
80     * @param FormatterInterface $formatter
81     * @param string|null $target
82     * @return EmitterInterface
83     */
84    public static function getNewEmitter(
85        string $prefix,
86        StatsCache $cache,
87        FormatterInterface $formatter,
88        ?string $target = null
89    ): EmitterInterface {
90        $formatterClass = get_class( $formatter );
91        switch ( $formatterClass ) {
92            case StatsdFormatter::class:
93            case DogStatsdFormatter::class:
94                return new UDPEmitter( $prefix, $cache, $formatter, $target );
95            case NullFormatter::class:
96                return new NullEmitter;
97            default:
98                throw new UnsupportedFormatException( "Unsupported metrics formatter '{$formatterClass}'" );
99        }
100    }
101}