Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Monitors
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 2
30
0.00% covered (danger)
0.00%
0 / 1
 get
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 initialize
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Automattic\Jetpack_Inspect;
4
5use Automattic\Jetpack_Inspect\Monitor\Incoming_REST_API;
6use Automattic\Jetpack_Inspect\Monitor\Outgoing;
7
8/**
9 * The Monitors class.
10 */
11class Monitors {
12    const AVAILABLE_OBSERVERS = array(
13        'outgoing' => Outgoing::class,
14        'incoming' => Incoming_REST_API::class,
15    );
16
17    /**
18     * Array of existing instances.
19     *
20     * @var array
21     */
22    protected static $instances = array();
23
24    /**
25     * Returns an instance that observer with a specific name.
26     *
27     * @param String $name observer name.
28     */
29    public static function get( $name ) {
30
31        if ( ! isset( static::AVAILABLE_OBSERVERS[ $name ] ) ) {
32            return new \WP_Error( "The requested monitor doesn't exist." );
33        }
34
35        if ( ! isset( static::$instances[ $name ] ) ) {
36            $class                      = static::AVAILABLE_OBSERVERS[ $name ];
37            static::$instances[ $name ] = new Monitor( "observer_{$name}", new $class() );
38        }
39
40        return static::$instances[ $name ];
41    }
42
43    /**
44     * Initializes the instance holder.
45     */
46    public static function initialize() {
47        foreach ( self::AVAILABLE_OBSERVERS as $name => $class ) {
48            self::get( $name )->initialize();
49        }
50    }
51}