Completed
Branch newinternal (ffe884)
by Simon
04:07
created

SearchHelperBase   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 118
ccs 0
cts 54
cp 0
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A fetchObjects() 0 17 2
A getRecordCount() 0 11 1
A limit() 0 7 1
A applyLimit() 0 15 3
A applyOrder() 0 8 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Helpers\SearchHelpers;
10
11
use PDO;
12
use Waca\DataObject;
13
use Waca\PdoDatabase;
14
15
abstract class SearchHelperBase
16
{
17
	/** @var PdoDatabase */
18
	protected $database;
19
	/** @var array */
20
	protected $parameterList = array();
21
	/** @var null|int */
22
	private $limit = null;
23
	/** @var null|int */
24
	private $offset = null;
25
	private $orderBy = null;
26
	/**
27
	 * @var string The where clause.
28
	 *
29
	 * (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here). Note
30
	 * that we use positional parameters instead of named parameters because we don't know many times different options
31
	 * will be called (looking at excluding() here, but there's the option for others).
32
	 */
33
	protected $whereClause = 'WHERE 1 = 1';
34
	/** @var string */
35
	private $table;
36
37
	/**
38
	 * SearchHelperBase constructor.
39
	 *
40
	 * @param PdoDatabase $database
41
	 * @param string      $table
42
	 * @param null        $order Order by clause, excluding ORDER BY.
43
	 */
44
	protected function __construct(PdoDatabase $database, $table, $order = null)
45
	{
46
		$this->database = $database;
47
		$this->table = $table;
48
		$this->orderBy = $order;
49
	}
50
51
	/**
52
	 * @param $targetClass
53
	 *
54
	 * @return DataObject[]
55
	 */
56
	protected function fetchObjects($targetClass)
57
	{
58
		$query = 'SELECT * FROM ' . $this->table . ' ' . $this->whereClause;
59
		$query .= $this->applyOrder();
60
		$query .= $this->applyLimit();
61
62
		$statement = $this->database->prepare($query);
63
		$statement->execute($this->parameterList);
64
65
		/** @var DataObject[] $returnedObjects */
66
		$returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $targetClass);
67
		foreach ($returnedObjects as $req) {
68
			$req->setDatabase($this->database);
69
		}
70
71
		return $returnedObjects;
72
	}
73
74
	/**
75
	 * @param int $count Returns the record count of the result set
76
	 *
77
	 * @return $this
78
	 */
79
	public function getRecordCount(&$count)
80
	{
81
		$query = 'SELECT COUNT(1) FROM ' . $this->table . ' ' . $this->whereClause;
82
		$statement = $this->database->prepare($query);
83
		$statement->execute($this->parameterList);
84
85
		$count = $statement->fetchColumn(0);
86
		$statement->closeCursor();
87
88
		return $this;
89
	}
90
91
	/**
92
	 * Limits the results
93
	 *
94
	 * @param int $limit
95
	 * @param int $offset
0 ignored issues
show
Documentation introduced by
Should the type for parameter $offset not be integer|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
96
	 *
97
	 * @return $this
98
	 *
99
	 */
100
	public function limit($limit, $offset = null)
101
	{
102
		$this->limit = $limit;
103
		$this->offset = $offset;
104
105
		return $this;
106
	}
107
108
	private function applyLimit()
109
	{
110
		$clause = '';
111
		if ($this->limit !== null) {
112
			$clause = ' LIMIT ?';
113
			$this->parameterList[] = $this->limit;
114
115
			if ($this->offset !== null) {
116
				$clause .= ' OFFSET ?';
117
				$this->parameterList[] = $this->offset;
118
			}
119
		}
120
121
		return $clause;
122
	}
123
124
	private function applyOrder()
125
	{
126
		if ($this->orderBy !== null) {
127
			return ' ORDER BY ' . $this->orderBy;
128
		}
129
130
		return '';
131
	}
132
}