Passed
Pull Request — master (#2028)
by Gabriel
60:53
created

getConfigurationPathsForEnvironment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Infrastructure;
6
7
use WMDE\Fundraising\Frontend\Factories\EnvironmentDependentConfigReaderFactory;
8
use WMDE\Fundraising\Frontend\Factories\EnvironmentSetup\DevelopmentEnvironmentSetup;
9
use WMDE\Fundraising\Frontend\Factories\EnvironmentSetup\EnvironmentSetup;
10
use WMDE\Fundraising\Frontend\Factories\EnvironmentSetup\EnvironmentSetupException;
11
use WMDE\Fundraising\Frontend\Factories\EnvironmentSetup\ProductionEnvironmentSetup;
12
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
13
14
class EnvironmentBootstrapper {
15
16
	private const DEFAULT_ENVIRONMENT_SETUP_MAP = [
17
		'dev' => DevelopmentEnvironmentSetup::class,
18
		// User Acceptance Testing should be as close to production as possible
19
		'uat' => ProductionEnvironmentSetup::class,
20
		'prod' => ProductionEnvironmentSetup::class
21
	];
22
23
	private string $environmentName;
24
25 238
	/**
26 238
	 * @var array<string,class-string>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string,class-string> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string,class-string>.
Loading history...
27 238
	 */
28 238
	private array $environmentMap;
29
30 234
	public function __construct( string $environmentName, array $environmentMap = [] ) {
31 234
		$this->environmentName = $environmentName;
32 234
		$this->environmentMap = array_merge( self::DEFAULT_ENVIRONMENT_SETUP_MAP, $environmentMap );
33 234
	}
34 234
35
	public function getEnvironmentSetupInstance(): EnvironmentSetup {
36 234
		if ( !isset( $this->environmentMap[$this->environmentName] ) ) {
37 232
			throw new EnvironmentSetupException( $this->environmentName );
38
		}
39
		$class = $this->environmentMap[$this->environmentName];
40 234
		return new $class;
41 234
	}
42 3
43
	public function newFunFunFactory(): FunFunFactory {
44 234
		$config = $this->getConfiguration();
45
		$factory = new FunFunFactory( $config );
46
47 234
		$this->getEnvironmentSetupInstance()
48 234
			->setEnvironmentDependentInstances( $factory );
49
50 234
		return $factory;
51 2
	}
52
53 234
	protected function getConfiguration(): array {
54 234
		$configReader = ( new EnvironmentDependentConfigReaderFactory( $this->environmentName ) )->getConfigReader();
55
		return $configReader->getConfig();
56 232
	}
57
}
58