Passed
Pull Request — master (#2028)
by Gabriel
203:46 queued 138:41
created

ValidateCampaignConfigCommand::getFactory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Cli\CampaignConfigValidation;
6
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputArgument;
9
use Symfony\Component\Console\Input\InputDefinition;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use WMDE\Fundraising\Frontend\BucketTesting\Validation\CampaignErrorCollection;
13
use WMDE\Fundraising\Frontend\BucketTesting\Validation\CampaignValidator;
14
use WMDE\Fundraising\Frontend\BucketTesting\Validation\LoggingCampaignConfigurationLoader;
15
use WMDE\Fundraising\Frontend\Infrastructure\EnvironmentBootstrapper;
16
17
/**
18
 * @license GPL-2.0-or-later
19
 */
20
class ValidateCampaignConfigCommand extends Command {
21
22
	private const NAME = 'app:validate:campaigns';
23
	private const RETURN_CODE_OK = 0;
24
	private const RETURN_CODE_ERROR = 1;
25
26
	protected function configure(): void {
27
		$this->setName( self::NAME )
28
			->setDescription( 'Validate campaign configuration files' )
29
			->setHelp(
30
				'This command validates the Campaign configuration files for errors after they have been merged'
31
			)
32
			->setDefinition(
33
				new InputDefinition(
34
					[
35
						new InputArgument(
36
							'environment',
37
							InputArgument::OPTIONAL,
38
							'Specify a specific campaign environment which is to be tested',
39
							'prod'
40
						)
41
					]
42
				)
43
			);
44
	}
45
46
	protected function execute( InputInterface $input, OutputInterface $output ): int {
47
		$environment = $input->getArgument( 'environment' );
48
		$errorLogger = new CampaignErrorCollection();
49
		$bootstrapper = new EnvironmentBootstrapper( $environment );
0 ignored issues
show
Bug introduced by
It seems like $environment can also be of type null and string[]; however, parameter $environmentName of WMDE\Fundraising\Fronten...strapper::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
		$bootstrapper = new EnvironmentBootstrapper( /** @scrutinizer ignore-type */ $environment );
Loading history...
50
		$factory = $bootstrapper->newFunFunFactory();
51
		$factory->setCampaignConfigurationLoader(
52
			new LoggingCampaignConfigurationLoader(
53
				$factory->getCampaignConfigurationLoader(), $errorLogger
54
			)
55
		);
56
57
		$campaignCollection = $factory->getCampaignCollection();
58
		if ( $errorLogger->hasErrors() === false ) {
59
			$validator = new CampaignValidator( $campaignCollection, $errorLogger );
60
61
			if ( $validator->isPassing() ) {
62
				return self::RETURN_CODE_OK;
63
			}
64
		}
65
66
		foreach ( $errorLogger->getErrors() as $error ) {
67
			$output->writeln( $error );
68
		}
69
		$output->writeln( 'Campaign YAML validation failed.' );
70
		return self::RETURN_CODE_ERROR;
71
	}
72
}
73