delete::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 11
rs 9.4285
cc 1
eloc 9
nc 1
nop 6
1
<?php
2
/**
3
 *
4
 * Ajax Shoutbox extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2014 Paul Sohier <http://www.ajax-shoutbox.com>
7
 * @license       GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace paul999\ajaxshoutbox\actions;
12
13
use paul999\ajaxshoutbox\exceptions\shoutbox_exception;
14
15
class delete
16
{
17
	/** @var \phpbb\config\config  */
18
	private $config;
19
20
	/** @var \phpbb\db\driver\driver_interface  */
21
	private $db;
22
23
	/** @var \phpbb\auth\auth  */
24
	private $auth;
25
26
	/** @var \phpbb\request\request  */
27
	private $request;
28
29
	/** @var \phpbb\user  */
30
	private $user;
31
32
	/** @var string */
33
	private $table;
34
35
	/**
36
	 * @param \phpbb\config\config               $config
37
	 * @param \phpbb\db\driver\driver_interface  $db
38
	 * @param \phpbb\auth\auth                   $auth
39
	 * @param \phpbb\request\request             $request
40
	 * @param \phpbb\user                        $user
41
	 * @param string                             $table
42
	 */
43 View Code Duplication
	public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
44
								\phpbb\auth\auth $auth, \phpbb\request\request $request,
45
								\phpbb\user $user, $table)
46
	{
47
		$this->config   = $config;
48
		$this->db       = $db;
49
		$this->auth     = $auth;
50
		$this->request  = $request;
51
		$this->user     = $user;
52
		$this->table    = $table;
53
	}
54
55
	/**
56
	 * Delete a shoutbox post
57
	 *
58
	 * If push is enabled, we first make sure it is deleted on the server.
59
	 * When we delete first here, we have a problem when the server fails.
60
	 *
61
	 * @param int $id
62
	 *
63
	 * @throws \paul999\ajaxshoutbox\exceptions\shoutbox_exception
64
	 */
65
	public function delete_post($id)
66
	{
67
		if (!$id)
68
		{
69
			$id = $this->request->variable('id', 0);
70
		}
71
		$sql = 'SELECT user_id
72
					FROM ' . $this->table . '
73
					WHERE shout_id = ' . (int) $id;
74
		$result = $this->db->sql_query($sql);
75
		$row = $this->db->sql_fetchrow();
76
		$this->db->sql_freeresult($result);
77
78
		if (!$row)
79
		{
80
			throw new shoutbox_exception('AJAX_SHOUTBOX_NO_SUCH_POST');
81
		}
82
		if (!$this->auth->acl_get('m_shoutbox_delete'))
83
		{
84
			// User has no m_ permission.
85
86
			if ($row['user_id'] != $this->user->data['user_id'])
87
			{
88
				throw new shoutbox_exception('AJAX_SHOUTBOX_NO_SUCH_POST');
89
			}
90
			if (!$this->auth->acl_get('u_shoutbox_delete'))
91
			{
92
				throw new shoutbox_exception('AJAX_SHOUTBOX_NO_PERMISSION');
93
			}
94
		}
95
96
		$sql = 'DELETE FROM ' . $this->table .'
97
					WHERE shout_id =  ' . (int) $id;
98
		$this->db->sql_query($sql);
99
	}
100
}
101