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

ValidateCampaignCodeUtilizationCommand::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 Method

Rating   Name   Duplication   Size   Complexity  
A ValidateCampaignCodeUtilizationCommand::getCampaigns() 0 4 1
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 WMDE\Fundraising\Frontend\BucketTesting\CampaignCollection;
14
use WMDE\Fundraising\Frontend\BucketTesting\Validation\CampaignErrorCollection;
15
use WMDE\Fundraising\Frontend\BucketTesting\Validation\CampaignUtilizationValidator;
16
use WMDE\Fundraising\Frontend\BucketTesting\Validation\FeatureToggleParser;
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 ValidateCampaignCodeUtilizationCommand extends Command {
25
26
	private const NAME = 'app:validate:campaigns:utilization';
27
	private const RETURN_CODE_OK = 0;
28
	private const RETURN_CODE_ERROR = 1;
29
30
	/** @see \WMDE\Fundraising\Frontend\Factories\ChoiceFactory */
31
	private const CHOICE_FACTORY_LOCATION = 'src/Factories/ChoiceFactory.php';
32
33
	protected function configure(): void {
34
		$this->setName( self::NAME )
35
			->setDescription( 'Validate campaign configuration utilization in source code' )
36
			->setHelp(
37
				'This command validates that all campaign configurations are related to a specific entry point in the code'
38
			)
39
			->setDefinition(
40
				new InputDefinition(
41
					[
42
						new InputArgument(
43
							'environment',
44
							InputArgument::OPTIONAL,
45
							'Specify a specific campaign environment which is to be tested',
46
							'prod'
47
						)
48
					]
49
				)
50
			);
51
	}
52
53
	protected function execute( InputInterface $input, OutputInterface $output ): int {
54
		$errorLogger = new CampaignErrorCollection();
55
		$validator = new CampaignUtilizationValidator(
56
			$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

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