Completed
Push — renovate/mocha-8.x ( 07030e...e8e64c )
by
unknown
28:17 queued 19:09
created

Manifest_Handler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 93
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A register_plugin_manifests() 0 14 2
A register_manifest() 0 14 4
A register_record() 0 14 3
1
<?php
2
/* HEADER */ // phpcs:ignore
3
4
/**
5
 * This class selects the most recent version of a registered path.
6
 */
7
class Manifest_Handler {
8
9
	/**
10
	 * An array of the active plugin paths we want to search.
11
	 *
12
	 * @var array
13
	 */
14
	private $active_plugin_paths;
15
16
	/**
17
	 * The Version_Selector object.
18
	 *
19
	 * @var Version_Selector
20
	 */
21
	private $version_selector;
22
23
	/**
24
	 * The constructor.
25
	 *
26
	 * @param array            $active_plugin_paths An array of the active plugin paths we want to search.
27
	 * @param Version_Selector $version_selector The Version_Selector object.
28
	 */
29
	public function __construct( $active_plugin_paths, $version_selector ) {
30
		$this->active_plugin_paths = $active_plugin_paths;
31
		$this->version_selector    = $version_selector;
32
	}
33
34
	/**
35
	 * Registers all of the paths in a given manifest.
36
	 *
37
	 * @param string $manifest_path The path that we're loading the manifest from in each plugin.
38
	 * @param array  $path_map The path map to add the contents of the manifests to.
39
	 *
40
	 * @return array $path_map The path map we've built using the manifests in each plugin.
41
	 */
42
	public function register_plugin_manifests( $manifest_path, &$path_map ) {
43
		$file_paths = array_map(
44
			function ( $path ) use ( $manifest_path ) {
45
				return trailingslashit( $path ) . $manifest_path;
46
			},
47
			$this->active_plugin_paths
48
		);
49
50
		foreach ( $file_paths as $path ) {
51
			$this->register_manifest( $path, $path_map );
52
		}
53
54
		return $path_map;
55
	}
56
57
	/**
58
	 * Registers a plugin's manifest file with the path map.
59
	 *
60
	 * @param string $manifest_path The absolute path to the manifest that we're loading.
61
	 * @param array  $path_map The path map to add the contents of the manifest to.
62
	 */
63
	protected function register_manifest( $manifest_path, &$path_map ) {
64
		if ( ! is_readable( $manifest_path ) ) {
65
			return;
66
		}
67
68
		$manifest = require $manifest_path;
69
		if ( ! is_array( $manifest ) ) {
70
			return;
71
		}
72
73
		foreach ( $manifest as $key => $data ) {
74
			$this->register_record( $key, $data, $path_map );
75
		}
76
	}
77
78
	/**
79
	 * Registers an entry from the manifest in the path map.
80
	 *
81
	 * @param string $key The identifier for the entry we're registering.
82
	 * @param array  $data The data for the entry we're registering.
83
	 * @param array  $path_map The path map to add the contents of the manifest to.
84
	 */
85
	protected function register_record( $key, $data, &$path_map ) {
86
		if ( isset( $path_map[ $key ]['version'] ) ) {
87
			$selected_version = $path_map[ $key ]['version'];
88
		} else {
89
			$selected_version = null;
90
		}
91
92
		if ( $this->version_selector->is_version_update_required( $selected_version, $data['version'] ) ) {
93
			$path_map[ $key ] = array(
94
				'version' => $data['version'],
95
				'path'    => $data['path'],
96
			);
97
		}
98
	}
99
}
100