Completed
Branch newinternal (104de7)
by Simon
10:16
created

RequestSearchHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
c 2
b 0
f 0
lcom 1
cbo 3
dl 0
loc 150
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A get() 0 5 1
A fetch() 0 13 2
A byIp() 0 8 1
A byEmailAddress() 0 7 1
A byName() 0 7 1
A excludingRequest() 0 7 1
A withConfirmedEmail() 0 7 1
A excludingPurgedData() 0 8 1
1
<?php
2
3
namespace Waca\Helpers;
4
5
use PDO;
6
use Waca\DataObjects\Request;
7
use Waca\PdoDatabase;
8
use Waca\SiteConfiguration;
9
10
class RequestSearchHelper
11
{
12
	/** @var PdoDatabase */
13
	private $database;
14
	/** @var string */
15
	private $query;
16
	/** @var array */
17
	private $parameterList = array();
18
19
	/**
20
	 * RequestSearchHelper constructor.
21
	 *
22
	 * @param PdoDatabase $database
23
	 */
24
	private function __construct(PdoDatabase $database)
25
	{
26
		$this->database = $database;
27
28
		// initialise query
29
		// (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here)
30
		// Note that we use positional parameters instead of named parameters because we don't know many times different
31
		// options will be called (looking at excluding() here, but there's the option for others).
32
		$this->query = <<<SQL
33
SELECT * /* RequestSearchHelper */
34
FROM request WHERE 1 = 1
35
SQL;
36
	}
37
38
	/**
39
	 * Initiates a search for requests
40
	 *
41
	 * @param PdoDatabase $database
42
	 *
43
	 * @return RequestSearchHelper
44
	 */
45
	public static function get(PdoDatabase $database){
46
		$helper = new RequestSearchHelper($database);
47
48
		return $helper;
49
	}
50
51
	/**
52
	 * Returns the requested requests
53
	 *
54
	 * @return Request[]
55
	 */
56
	public function fetch()
57
	{
58
		$statement = $this->database->prepare($this->query);
59
		$statement->execute($this->parameterList);
60
61
		/** @var Request[] $returnedObjects */
62
		$returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, Request::class);
63
		foreach ($returnedObjects as $req) {
64
			$req->setDatabase($this->database);
65
		}
66
67
		return $returnedObjects;
68
	}
69
70
	/**
71
	 * Filters the results by IP address
72
	 *
73
	 * @param string $ipAddress
74
	 *
75
	 * @return $this
76
	 */
77
	public function byIp($ipAddress)
78
	{
79
		$this->query .= ' AND (ip LIKE ? OR forwardedip LIKE ?)';
80
		$this->parameterList[] = $ipAddress;
81
		$this->parameterList[] = '%' . trim($ipAddress, '%') . '%';
82
83
		return $this;
84
	}
85
86
	/**
87
	 * Filters the results by email address
88
	 *
89
	 * @param string $emailAddress
90
	 *
91
	 * @return $this
92
	 */
93
	public function byEmailAddress($emailAddress)
94
	{
95
		$this->query .= ' AND email LIKE ?';
96
		$this->parameterList[] = $emailAddress;
97
98
		return $this;
99
	}
100
101
	/**
102
	 * Filters the results by name
103
	 *
104
	 * @param string $name
105
	 *
106
	 * @return $this
107
	 */
108
	public function byName($name)
109
	{
110
		$this->query .= ' AND name LIKE ?';
111
		$this->parameterList[] = $name;
112
113
		return $this;
114
	}
115
116
	/**
117
	 * Excludes a request from the results
118
	 *
119
	 * @param int $requestId
120
	 *
121
	 * @return $this
122
	 */
123
	public function excludingRequest($requestId)
124
	{
125
		$this->query .= ' AND id <> ?';
126
		$this->parameterList[] = $requestId;
127
128
		return $this;
129
	}
130
131
	/**
132
	 * Filters the results to only those with a confirmed email address
133
	 *
134
	 * @return $this
135
	 */
136
	public function withConfirmedEmail()
137
	{
138
		$this->query .= ' AND emailconfirm = ?';
139
		$this->parameterList[] = 'Confirmed';
140
141
		return $this;
142
	}
143
144
	/**
145
	 * Filters the results to exclude purged data
146
	 *
147
	 * @param SiteConfiguration $configuration
148
	 *
149
	 * @return $this
150
	 */
151
	public function excludingPurgedData(SiteConfiguration $configuration)
152
	{
153
		$this->query .= ' AND ip <> ? AND email <> ?';
154
		$this->parameterList[] = $configuration->getDataClearIp();
155
		$this->parameterList[] = $configuration->getDataClearEmail();
156
157
		return $this;
158
	}
159
}