Passed
Pull Request — master (#2028)
by Gabriel
61:03
created

EnvironmentBootstrapper::newFunFunFactory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 13
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Infrastructure;
6
7
use FileFetcher\SimpleFileFetcher;
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 $environmentName;
24
25 238
	private $environmentMap;
26 238
27 238
	public function __construct( string $environmentName, array $environmentMap = [] ) {
28 238
		$this->environmentName = $environmentName;
29
		$this->environmentMap = array_merge( self::DEFAULT_ENVIRONMENT_SETUP_MAP, $environmentMap );
30 234
	}
31 234
32 234
	public function getConfigurationPathsForEnvironment( string $configPath ): array {
33 234
		$paths = self::removeNonexistentOptionalPaths( ...[
34 234
			$configPath . '/config.dist.json',
35
			$configPath . '/config.' . $this->environmentName . '.json',
36 234
			$configPath . '/config.' . $this->environmentName . '.local.json',
37 232
		] );
38
		self::checkIfPathsExist( ...$paths );
39
		return $paths;
40 234
	}
41 234
42 3
	private static function removeNonexistentOptionalPaths( string ...$paths ): array {
43
		if ( !file_exists( $paths[2] ) ) {
44 234
			array_splice( $paths, 2 );
45
		}
46
		return $paths;
47 234
	}
48 234
49
	private static function checkIfPathsExist( string ...$paths ): void {
50 234
		array_map(
51 2
			function ( $path ) {
52
				if ( !is_readable( $path ) ) {
53 234
					throw new \RuntimeException( 'Configuration file "' . $path . '" not found' );
54 234
				}
55
			},
56 232
			$paths
57
		);
58 235
	}
59 235
60 1
	public function getEnvironmentSetupInstance(): EnvironmentSetup {
61
		if ( !isset( $this->environmentMap[$this->environmentName] ) ) {
62 234
			throw new EnvironmentSetupException( $this->environmentName );
63 234
		}
64
		$class = $this->environmentMap[$this->environmentName];
65
		return new $class;
66
	}
67
68
	public function newFunFunFactory(): FunFunFactory {
69
		$configReader = new ConfigReader(
70
			new SimpleFileFetcher(),
71
			...$this->getConfigurationPathsForEnvironment( __DIR__ . '/../../app/config' )
72
		);
73
74
		$config = $configReader->getConfig();
75
		$factory = new FunFunFactory( $config );
76
77
		$this->getEnvironmentSetupInstance()
78
			->setEnvironmentDependentInstances( $factory, $config );
79
80
		return $factory;
81
	}
82
}
83