Passed
Pull Request — master (#2028)
by Gabriel
64:17
created

ValidateCampaignCodeUtilizationCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 53
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getCampaigns() 0 4 1
A configure() 0 14 1
A execute() 0 18 3
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\CampaignCollection;
15
use WMDE\Fundraising\Frontend\BucketTesting\Validation\CampaignErrorCollection;
16
use WMDE\Fundraising\Frontend\BucketTesting\Validation\CampaignUtilizationValidator;
17
use WMDE\Fundraising\Frontend\BucketTesting\Validation\FeatureToggleParser;
18
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
19
use WMDE\Fundraising\Frontend\Infrastructure\ConfigReader;
20
use WMDE\Fundraising\Frontend\Infrastructure\EnvironmentBootstrapper;
21
22
/**
23
 * @license GPL-2.0-or-later
24
 */
25
class ValidateCampaignCodeUtilizationCommand extends Command {
26
27
	private const NAME = 'app:validate:campaigns:utilization';
28
	private const RETURN_CODE_OK = 0;
29
	private const RETURN_CODE_ERROR = 1;
30
31
	/** @see \WMDE\Fundraising\Frontend\Factories\ChoiceFactory */
32
	private const CHOICE_FACTORY_LOCATION = 'src/Factories/ChoiceFactory.php';
33
34
	protected function configure(): void {
35
		$this->setName( self::NAME )
36
			->setDescription( 'Validate campaign configuration utilization in source code' )
37
			->setHelp(
38
				'This command validates that all campaign configurations are related to a specific entry point in the code'
39
			)
40
			->setDefinition(
41
				new InputDefinition(
42
					[
43
						new InputArgument(
44
							'environment',
45
							InputArgument::OPTIONAL,
46
							'Specify a specific campaign environment which is to be tested',
47
							'prod'
48
						)
49
					]
50
				)
51
			);
52
	}
53
54
	protected function execute( InputInterface $input, OutputInterface $output ): int {
55
		$errorLogger = new CampaignErrorCollection();
56
		$validator = new CampaignUtilizationValidator(
57
			$this->getCampaigns( $input->getArgument( 'environment' ) ),
0 ignored issues
show
Bug introduced by
It seems like $input->getArgument('environment') can also be of type null and string[]; however, parameter $environment of WMDE\Fundraising\Fronten...Command::getCampaigns() 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

57
			$this->getCampaigns( /** @scrutinizer ignore-type */ $input->getArgument( 'environment' ) ),
Loading history...
58
			[ 'campaigns.skins.test' ],
59
			FeatureToggleParser::getFeatureToggleChecks( self::CHOICE_FACTORY_LOCATION ),
60
			$errorLogger
61
		);
62
63
		if ( $validator->isPassing() ) {
64
			return self::RETURN_CODE_OK;
65
		}
66
67
		foreach ( $errorLogger->getErrors() as $error ) {
68
			$output->writeln( $error );
69
		}
70
		$output->writeln( 'Campaign utilization validation failed.' );
71
		return self::RETURN_CODE_ERROR;
72
	}
73
74
	private function getCampaigns( string $environment ): CampaignCollection {
75
		$bootstrapper = new EnvironmentBootstrapper( $environment );
76
		$factory = $bootstrapper->newFunFunFactory();
77
		return $factory->getCampaignCollection();
78
	}
79
80
}
81