Completed
Pull Request — master (#41)
by
unknown
63:48
created

CampaignCollection::getFirstCampaign()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
cc 2
nc 2
nop 0
crap 6
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\BannerServer\Entity\BannerSelection;
6
7
/**
8
 * @license GNU GPL v2+
9
 */
10
class CampaignCollection {
11
12
	/**
13
	 * @var Campaign[]
14
	 */
15
	private array $campaigns;
16
17 2
	public function __construct( Campaign ...$campaigns ) {
18 2
		$this->campaigns = $campaigns;
19 2
	}
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
	public function filter( callable $isValid ): CampaignCollection {
31
		return new CampaignCollection( ...array_filter( $this->campaigns, $isValid ) );
32
	}
33
34
	public function getFirstCampaign(): Campaign {
35
		if( $this->isEmpty() ) {
36
			throw new \OutOfBoundsException("No campaigns found.");
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
Coding Style Comprehensibility introduced by
The string literal No campaigns found. does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
37
		}
38
		return $this->campaigns[0];
39
	}
40
41
	public function isEmpty(): bool {
42
		return count ( $this->campaigns ) === 0;
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
43
	}
44
}