Plugin   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 0
cts 23
cp 0
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 2 1
B instantiateServices() 0 18 5
A getServices() 0 3 1
A registerServices() 0 6 1
1
<?php
2
3
namespace Mamaduka\BpNotifications;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Class Plugin.
9
 *
10
 * Main plugin controller class that hooks the plugin's functionality into the
11
 * WordPress request lifecycle.
12
 */
13
final class Plugin implements Registerable {
14
15
	/**
16
	 * Register the current Registerable.
17
	 *
18
	 * @return void
19
	 */
20
	public function register() {
21
		add_action( 'bp_loaded', [ $this, 'registerServices' ] );
22
	}
23
24
	/**
25
	 * Register the individual services of this plugin.
26
	 *
27
	 * @throws InvalidArgumentException
28
	 */
29
	public function registerServices() {
30
		$services = $this->getServices();
31
		$services = array_map( [ $this, 'instantiateServices' ], $services );
32
		
33
		array_walk( $services, function ( Service $service ) {
34
			$service->register();
35
		} );
36
	}
37
38
	/**
39
	 * Instantiate a single service.
40
	 *
41
	 * @param string $class
42
	 *
43
	 * @return Service
44
	 * @throws InvalidArgumentException
45
	 */
46
	protected function instantiateServices( $class ) {
47
		if ( ! class_exists( $class ) ) {
48
			throw new InvalidArgumentException( sprintf(
49
				'The service "%s" is not recognized and cannot be registered.',
50
				is_object( $class ) ? get_class( $class ) : (string) $class
51
			) );
52
		}
53
54
		$service = new $class();
55
56
		if ( ! $service instanceof Service ) {
57
			throw new InvalidArgumentException( sprintf(
58
				'The service "%s" is not recognized and cannot be registered.',
59
				is_object( $service ) ? get_class( $service ) : (string) $service
60
			) );
61
		}
62
63
		return $service;
64
	}
65
66
	/**
67
	 * Get list of the services.
68
	 *
69
	 * @return array
70
	 */
71
	protected function getServices() {
72
		return [
73
			MessagesService::class,
74
		];
75
	}
76
}