Completed
Pull Request — master (#21)
by Tim
03:04
created

buildBannersFromData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\UseCase\BannerSelection;
6
7
use Psr\Log\LoggerInterface;
8
use Symfony\Component\Yaml\Exception\ParseException;
9
use Symfony\Component\Yaml\Yaml;
10
use WMDE\BannerServer\Entity\BannerSelection\Banner;
11
use WMDE\BannerServer\Entity\BannerSelection\Bucket;
12
use WMDE\BannerServer\Entity\BannerSelection\Campaign;
13
use WMDE\BannerServer\Entity\BannerSelection\CampaignCollection;
14
use WMDE\BannerServer\Utils\SystemRandomIntegerGenerator;
15
16
/**
17
 * @license GNU GPL v2+
18
 */
19
class CampaignConfigurationLoader {
20
21
	private const DEFAULT_CONFIGURATION_FILE_PATH = 'config/campaigns/campaigns.yml';
22
23
	private $configFile;
24
	private $logger;
25
26 2
	public function __construct( LoggerInterface $logger, string $configFile = self::DEFAULT_CONFIGURATION_FILE_PATH ) {
27 2
		$this->configFile = $this->getAbsolutePath( $configFile );
28 2
		$this->logger = $logger;
29 2
	}
30
31 2
	public function getCampaignCollection(): CampaignCollection {
32 2
		$campaigns = [];
33 2
		foreach ( $this->parseConfiguration() as $campaignName => $campaignData ) {
34 1
			$campaign = $this->buildCampaignFromData( $campaignName, $campaignData );
35 1
			if ( $campaign ) {
36 1
				$campaigns[] = $campaign;
37
			}
38
		}
39 2
		return new CampaignCollection( ...$campaigns );
40
	}
41
42 1
	private function buildCampaignFromData( string $campaignName, array $campaignData ): ?Campaign {
43 1
		$buckets = $this->buildBucketsFromData( $campaignData );
44 1
		if ( empty( $buckets ) ) {
45
			return null;
46
		}
47 1
		return new Campaign(
48 1
			$campaignName,
49 1
			new \DateTime( $campaignData['start'] ),
50 1
			new \DateTime( $campaignData['end'] ),
51 1
			(int)$campaignData['trafficLimit'],
52 1
			new SystemRandomIntegerGenerator(),
53 1
			array_shift( $buckets ),
54 1
			...$buckets
55
		);
56
	}
57
58 1
	private function buildBucketsFromData( array $campaignData ): array {
59 1
		$buckets = [];
60 1
		foreach ( $campaignData['buckets'] as $bucketData ) {
61 1
			$bucket = $this->buildBucketFromData( $bucketData );
62 1
			if ( $bucket ) {
63 1
				$buckets[] = $bucket;
64
			}
65
		}
66 1
		return $buckets;
67
	}
68
69 1
	private function buildBucketFromData( array $bucketData ): ?Bucket {
70 1
		if ( !isset( $bucketData['name'] ) ) {
71
			return null;
72
		}
73 1
		$banners = $this->buildBannersFromData( $bucketData['banners'] );
74 1
		if ( empty( $banners ) ) {
75
			return null;
76
		}
77 1
		return new Bucket( $bucketData['name'], array_shift( $banners ), ...$banners );
78
	}
79
80 1
	private function buildBannersFromData( array $bannerData ): array {
81 1
		$banners = [];
82 1
		foreach ( $bannerData as $bannerIdentifier ) {
83 1
			$banners[] = new Banner( $bannerIdentifier );
84
		}
85 1
		return $banners;
86
	}
87
88 2
	private function parseConfiguration(): array {
89
		try {
90 2
			return Yaml::parseFile( $this->configFile );
91
		}
92 1
		catch ( ParseException $exception ) {
93 1
			$this->logger->critical( 'Unable to read banner server config file.', [ 'exception' => $exception ] );
94 1
			return [];
95
		}
96
	}
97
98 2
	private function getAbsolutePath( string $path ): string {
99 2
		if ( $path[0] === '/' ) {
100
			return $path;
101
		}
102 2
		return __DIR__ . '/../../../' . $path;
103
	}
104
}