Passed
Pull Request — master (#2028)
by Gabriel
64:17
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 FileFetcher\SimpleFileFetcher;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputDefinition;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
14
use WMDE\Fundraising\Frontend\BucketTesting\Validation\CampaignErrorCollection;
15
use WMDE\Fundraising\Frontend\BucketTesting\Validation\CampaignValidator;
16
use WMDE\Fundraising\Frontend\BucketTesting\Validation\LoggingCampaignConfigurationLoader;
17
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
18
use WMDE\Fundraising\Frontend\Infrastructure\ConfigReader;
19
use WMDE\Fundraising\Frontend\Infrastructure\EnvironmentBootstrapper;
20
21
/**
22
 * @license GPL-2.0-or-later
23
 */
24
class ValidateCampaignConfigCommand extends Command {
25
26
	private const NAME = 'app:validate:campaigns';
27
	private const RETURN_CODE_OK = 0;
28
	private const RETURN_CODE_ERROR = 1;
29
30
	protected function configure(): void {
31
		$this->setName( self::NAME )
32
			->setDescription( 'Validate campaign configuration files' )
33
			->setHelp(
34
				'This command validates the Campaign configuration files for errors after they have been merged'
35
			)
36
			->setDefinition(
37
				new InputDefinition(
38
					[
39
						new InputArgument(
40
							'environment',
41
							InputArgument::OPTIONAL,
42
							'Specify a specific campaign environment which is to be tested',
43
							'prod'
44
						)
45
					]
46
				)
47
			);
48
	}
49
50
	protected function execute( InputInterface $input, OutputInterface $output ): int {
51
		$environment = $input->getArgument( 'environment' );
52
		$errorLogger = new CampaignErrorCollection();
53
		$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

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