Completed
Pull Request — master (#111)
by Luca
02:07
created

ImportersBaseFactory::make()   C

Complexity

Conditions 7
Paths 18

Size

Total Lines 41
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 18
nop 1
dl 0
loc 41
rs 6.7272
c 0
b 0
f 0
1
<?php
2
3
namespace lloc\Msls\ContentImport\Importers;
4
5
use lloc\Msls\ContentImport\ImportCoordinates;
6
use lloc\Msls\MslsRegistryInstance;
7
8
abstract class ImportersBaseFactory extends MslsRegistryInstance implements ImportersFactory {
9
10
	/**
11
	 * The type of this importers factory; should be overridden by child classes.
12
	 */
13
	const TYPE = 'none';
14
15
	/**
16
	 * @var array An array defining the slug and Importer class relationships in
17
	 *            the shape [ <slug> => <importer-class> ]
18
	 */
19
	protected $importers_map = [];
20
21
	/**
22
	 * @return Importer
23
	 */
24
	public function make( ImportCoordinates $import_coordinates ) {
25
		if ( static::TYPE === self::TYPE ) {
26
			// this is a developer-land exception, no need to localize it
27
			throw new \RuntimeException( 'Importers factories should define their own type' );
28
		}
29
30
		$type = static::TYPE;
31
32
		/**
33
		 * Filters the importer that should be used to import this factory type of content.
34
		 *
35
		 * Returning an Importer instance here will force the class to return that.
36
		 *
37
		 * @param                   $importer Importer
38
		 * @param ImportCoordinates $import_coordinates
39
		 */
40
		$importer = apply_filters( "msls_content_import_{$type}_importer", null, $import_coordinates );
41
		if ( $importer instanceof Importer ) {
42
			return $importer;
43
		}
44
45
		/**
46
		 * Filters the importers map.
47
		 *
48
		 * @since TBD
49
		 *
50
		 * @param array $map A map of importers in the shape [ <importer-slug> => <importer-class> ]
51
		 * @param ImportCoordinates $import_coordinates
52
		 */
53
		$map = apply_filters( "msls_content_import_{$type}_importers_map", $this->importers_map, $import_coordinates );
54
55
		$first = count( $map ) > 0 ? reset( $map ) : null;
56
		$slug  = $import_coordinates->get_importer_for( $type ) ?: $first;
57
58
		// if there is some incoherence return the null-doing base importer
59
		$class = ! empty( $slug ) && isset( $map[ $slug ] )
60
			? $map[ $slug ]
61
			: BaseImporter::class;
62
63
		return new $class( $import_coordinates );
64
	}
65
66
	/**
67
	 * Returns the factory details.
68
	 *
69
	 * @return \stdClass
70
	 */
71
	public function details() {
72
		return (object) [
73
			'name'      => 'Base Factory',
74
			'importers' => [],
75
		];
76
	}
77
78
	/**
79
	 * Returns the slug of the selected importer for this factory.
80
	 *
81
	 * @return string
82
	 */
83
	public function selected() {
84
		$selected = array_keys( $this->importers_map )[0];
85
		$slug     = static::TYPE;
86
87
		/**
88
		 * Filters the selected importer that among the available ones.
89
		 *
90
		 * @since TBD
91
		 *
92
		 * @param string $selected The selected importer slug.
93
		 * @param ImportersFactory $this
94
		 */
95
		$selected = apply_filters( "msls_content_import_{$slug}_selected", $selected, $this );
96
97
		return $selected;
98
	}
99
100
	protected function importers_info() {
101
		return array_combine(
102
			array_keys( $this->importers_map ),
103
			array_map( function ( $importer_class ) {
104
				return $importer_class::info();
105
			}, $this->importers_map )
106
		);
107
	}
108
}