CampaignCollection::getCampaign()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\Entity\BannerSelection;
6
7
/**
8
 * @license GPL-2.0-or-later
9
 */
10
class CampaignCollection {
11
12
	/**
13
	 * @var Campaign[]
14
	 */
15
	private array $campaigns;
16
17 6
	public function __construct( Campaign ...$campaigns ) {
18 6
		$this->campaigns = $campaigns;
19
	}
20
21 2
	public function getCampaign( \DateTime $dateTime ): ?Campaign {
22 2
		foreach ( $this->campaigns as $campaign ) {
23 2
			if ( $campaign->isInActiveDateRange( $dateTime ) ) {
24 1
				return $campaign;
25
			}
26
		}
27 1
		return null;
28
	}
29
30 1
	public function filter( callable $isValid ): CampaignCollection {
31 1
		return new CampaignCollection( ...array_filter( $this->campaigns, $isValid ) );
32
	}
33
34 2
	public function getFirstCampaign(): Campaign {
35 2
		if ( $this->isEmpty() ) {
36 1
			throw new \OutOfBoundsException( 'No campaigns found.' );
37
		}
38 1
		return $this->campaigns[0];
39
	}
40
41 4
	public function isEmpty(): bool {
42 4
		return count( $this->campaigns ) === 0;
43
	}
44
}
45