Completed
Push — develop-stable ( 2a7b07...4a69f7 )
by Paul
07:32 queued 05:20
created

delete::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 12
Ratio 100 %

Importance

Changes 4
Bugs 1 Features 2
Metric Value
c 4
b 1
f 2
dl 12
loc 12
rs 9.4286
cc 1
eloc 10
nc 1
nop 7
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\log\log  */
27
	private $log;
28
29
	/** @var \phpbb\request\request  */
30
	private $request;
31
32
	/** @var \phpbb\user  */
33
	private $user;
34
35
	/** @var string */
36
	private $table;
37
38
	/**
39
	 * @param \phpbb\config\config               $config
40
	 * @param \phpbb\db\driver\driver_interface  $db
41
	 * @param \phpbb\auth\auth                   $auth
42
	 * @param \phpbb\log\log                     $log
43
	 * @param \phpbb\request\request             $request
44
	 * @param \phpbb\user                        $user
45
	 * @param string                             $table
46
	 */
47 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...
48
								\phpbb\auth\auth $auth, \phpbb\log\log $log, \phpbb\request\request $request,
49
								\phpbb\user $user, $table)
50
	{
51
		$this->config   = $config;
52
		$this->db       = $db;
53
		$this->auth     = $auth;
54
		$this->log      = $log;
55
		$this->request  = $request;
56
		$this->user     = $user;
57
		$this->table    = $table;
58
	}
59
60
	/**
61
	 * Delete a shoutbox post
62
	 *
63
	 * If push is enabled, we first make sure it is deleted on the server.
64
	 * When we delete first here, we have a problem when the server fails.
65
	 *
66
	 * @param int $id
67
	 *
68
	 * @throws \paul999\ajaxshoutbox\exceptions\shoutbox_exception
69
	 */
70
	public function delete_post($id)
71
	{
72
		if (!$id)
73
		{
74
			$id = $this->request->variable('id', 0);
75
		}
76
		$sql = 'SELECT user_id
77
					FROM ' . $this->table . '
78
					WHERE shout_id = ' . (int) $id;
79
		$result = $this->db->sql_query($sql);
80
		$row = $this->db->sql_fetchrow();
81
		$this->db->sql_freeresult($result);
82
83
		if (!$row)
84
		{
85
			throw new shoutbox_exception('AJAX_SHOUTBOX_NO_SUCH_POST');
86
		}
87
		if (!$this->auth->acl_get('m_shoutbox_delete'))
88
		{
89
			// User has no m_ permission.
90
91
			if ($row['user_id'] != $this->user->data['user_id'])
0 ignored issues
show
Bug introduced by
The property data cannot be accessed from this context as it is declared private in class phpbb\session.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
92
			{
93
				throw new shoutbox_exception('AJAX_SHOUTBOX_NO_SUCH_POST');
94
			}
95
			if (!$this->auth->acl_get('u_shoutbox_delete'))
96
			{
97
				throw new shoutbox_exception('AJAX_SHOUTBOX_NO_PERMISSION');
98
			}
99
		}
100
101
		$sql = 'DELETE FROM ' . $this->table .'
102
					WHERE shout_id =  ' . (int) $id;
103
		$this->db->sql_query($sql);
104
	}
105
}
106