Completed
Pull Request — master (#131)
by Matt
01:30
created

base   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 127
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A insert_idea_data() 0 8 1
A update_idea_data() 0 7 1
A delete_idea_data() 0 8 1
1
<?php
2
/**
3
 *
4
 * Ideas extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ideas\factory;
12
13
use phpbb\auth\auth;
14
use phpbb\config\config;
15
use phpbb\db\driver\driver_interface;
16
use phpbb\language\language;
17
use phpbb\user;
18
19
/**
20
 * Base ideas class
21
 */
22
class base
23
{
24
	public const SORT_AUTHOR = 'author';
25
	public const SORT_DATE = 'date';
26
	public const SORT_NEW = 'new';
27
	public const SORT_SCORE = 'score';
28
	public const SORT_TITLE = 'title';
29
	public const SORT_TOP = 'top';
30
	public const SORT_VOTES = 'votes';
31
	public const SORT_MYIDEAS = 'egosearch';
32
	public const SUBJECT_LENGTH = 120;
33
34
	/** @var array Idea status names and IDs */
35
	public static $statuses = array(
36
		'NEW'			=> 1,
37
		'IN_PROGRESS'	=> 2,
38
		'IMPLEMENTED'	=> 3,
39
		'DUPLICATE'		=> 4,
40
		'INVALID'		=> 5,
41
	);
42
43
	/** @var auth */
44
	protected $auth;
45
46
	/* @var config */
47
	protected $config;
48
49
	/* @var driver_interface */
50
	protected $db;
51
52
	/** @var language */
53
	protected $language;
54
55
	/* @var user */
56
	protected $user;
57
58
	/** @var string */
59
	protected $table_ideas;
60
61
	/** @var string */
62
	protected $table_votes;
63
64
	/** @var string */
65
	protected $table_topics;
66
67
	/** @var string */
68
	protected $php_ext;
69
70
	/**
71
	 * Constructor
72
	 *
73
	 * @param auth             $auth
74
	 * @param config           $config
75
	 * @param driver_interface $db
76
	 * @param language         $language
77
	 * @param user             $user
78
	 * @param string           $table_ideas
79
	 * @param string           $table_votes
80
	 * @param string           $table_topics
81
	 * @param string           $phpEx
82
	 */
83
	public function __construct(auth $auth, config $config, driver_interface $db, language $language, user $user, $table_ideas, $table_votes, $table_topics, $phpEx)
84
	{
85
		$this->auth = $auth;
86
		$this->config = $config;
87
		$this->db = $db;
88
		$this->language = $language;
89
		$this->user = $user;
90
91
		$this->php_ext = $phpEx;
92
93
		$this->table_ideas = $table_ideas;
94
		$this->table_votes = $table_votes;
95
		$this->table_topics = $table_topics;
96
	}
97
98
	/**
99
	 * Helper method for inserting new idea data
100
	 *
101
	 * @param array  $data  The array of data to insert
102
	 * @param string $table The name of the table
103
	 *
104
	 * @return int The ID of the inserted row
105
	 */
106
	protected function insert_idea_data(array $data, $table)
107
	{
108
		$sql = 'INSERT INTO ' . $table . '
109
		' . $this->db->sql_build_array('INSERT', $data);
110
		$this->db->sql_query($sql);
111
112
		return (int) $this->db->sql_nextid();
113
	}
114
115
	/**
116
	 * Helper method for updating idea data
117
	 *
118
	 * @param array  $data  The array of data to insert
119
	 * @param int    $id    The ID of the idea
120
	 * @param string $table The name of the table
121
	 *
122
	 * @return void
123
	 */
124
	protected function update_idea_data(array $data, $id, $table)
125
	{
126
		$sql = 'UPDATE ' . $table . '
127
			SET ' . $this->db->sql_build_array('UPDATE', $data) . '
128
			WHERE idea_id = ' . (int) $id;
129
		$this->db->sql_query($sql);
130
	}
131
132
	/**
133
	 * Helper method for deleting idea data
134
	 *
135
	 * @param int    $id    The ID of the idea
136
	 * @param string $table The name of the table
137
	 *
138
	 * @return bool True if idea was deleted, false otherwise
139
	 */
140
	protected function delete_idea_data($id, $table)
141
	{
142
		$sql = 'DELETE FROM ' . $table . '
143
			WHERE idea_id = ' . (int) $id;
144
		$this->db->sql_query($sql);
145
146
		return (bool) $this->db->sql_affectedrows();
147
	}
148
}
149