summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Importer/ContentCreators/DispatchingContentCreator.php
blob: 867c44c7c1bc3ee66f31b99ffc6de504f8a55bd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php

namespace SMW\Importer\ContentCreators;

use Onoi\MessageReporter\MessageReporter;
use RuntimeException;
use SMW\Importer\ContentCreator;
use SMW\Importer\ImportContents;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class DispatchingContentCreator implements ContentCreator {

	/**
	 * @var MessageReporter
	 */
	private $messageReporter;

	/**
	 * @var ContentCreator[]
	 */
	private $contentCreators = [];

	/**
	 * @since 3.0
	 *
	 * @param ContentCreator[]
	 */
	public function __construct( array $contentCreators ) {
		$this->contentCreators = $contentCreators;
	}

	/**
	 * @see MessageReporterAware::setMessageReporter
	 *
	 * @since 3.0
	 *
	 * @param MessageReporter $messageReporter
	 */
	public function setMessageReporter( MessageReporter $messageReporter ) {
		$this->messageReporter = $messageReporter;
	}

	/**
	 * @since 3.0
	 *
	 * @param ImportContents $importContents
	 */
	public function canCreateContentsFor( ImportContents $importContents ) {

		foreach ( $this->contentCreators as $contentCreator ) {
			if ( $contentCreator->canCreateContentsFor( $importContents ) ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * @since 3.0
	 *
	 * @param ImportContents $importContents
	 * @throws RuntimeException
	 */
	public function create( ImportContents $importContents ) {

		foreach ( $this->contentCreators as $contentCreator ) {
			if ( $contentCreator->canCreateContentsFor( $importContents ) ) {
				$contentCreator->setMessageReporter( $this->messageReporter );
				return $contentCreator->create( $importContents );
			}
		}

		throw new RuntimeException( "No dispatchable ContentsCreator is assigned to type " . $importContents->getContentType() );
	}

}