Completed
Push — develop ( 273208...5b590b )
by Daniel
09:31
created

attachments::get_attachments()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 13
cts 13
cp 1
rs 9.4285
cc 3
eloc 10
nc 2
nop 6
crap 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2016 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\sitemaker\services\forum;
11
12
class attachments
13
{
14
	/** @var \phpbb\auth\auth */
15
	protected $auth;
16
17
	/** @var \phpbb\db\driver\driver_interface */
18
	protected $db;
19
20
	/**
21
	 * Constructor
22
	 *
23
	 * @param \phpbb\auth\auth						$auth		Auth object
24
	 * @param \phpbb\db\driver\driver_interface		$db     	Database connection
25
	 */
26 6
	public function __construct(\phpbb\auth\auth $auth, \phpbb\db\driver\driver_interface $db)
27
	{
28 6
		$this->auth = $auth;
29 6
		$this->db = $db;
30 6
	}
31
32
	/**
33
	 * Get attachments...
34
	 *
35
	 * @param int $forum_id
36
	 * @param array $attach_ids
37
	 * @param array $allowed_extensions
38
	 * @param int|bool $limit
39
	 * @param bool $exclude_in_message
40
	 * @param string $order_by
41
	 * @return array
42
	 */
43 6
	public function get_attachments($forum_id = 0, array $attach_ids, $allowed_extensions = array(), $limit = false, $exclude_in_message = true, $order_by = 'filetime DESC')
44
	{
45 6
		$attachments = array();
46 6
		if ($this->user_can_download_attachments($forum_id))
47 6
		{
48 6
			$sql = $this->get_attachment_sql($attach_ids, $allowed_extensions, $exclude_in_message, $order_by);
49 6
			$result = $this->db->sql_query_limit($sql, $limit);
50
51 6
			while ($row = $this->db->sql_fetchrow($result))
52
			{
53 6
				$attachments[$row['post_msg_id']][] = $row;
54 6
			}
55 6
			$this->db->sql_freeresult($result);
56 6
		}
57 6
		$this->store['attachments'] = array();
0 ignored issues
show
Bug introduced by
The property store does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
59 6
		return $attachments;
60
	}
61
62
	/**
63
	 * @param int $forum_id
64
	 * @return bool
65
	 */
66 6
	protected function user_can_download_attachments($forum_id)
67
	{
68 6
		return ($this->auth->acl_get('u_download') && (!$forum_id || $this->auth->acl_get('f_download', $forum_id))) ? true : false;
69
	}
70
71
	/**
72
	 * @param array $attach_ids
73
	 * @param array $allowed_extensions
74
	 * @param bool $exclude_in_message
75
	 * @param string $order_by
76
	 * @return string
77
	 */
78 6
	protected function get_attachment_sql(array $attach_ids, array $allowed_extensions, $exclude_in_message, $order_by)
79
	{
80
		return 'SELECT *
81 6
			FROM ' . ATTACHMENTS_TABLE . '
82 6
			WHERE ' . $this->db->sql_in_set('post_msg_id', $attach_ids) .
83 6
				(($exclude_in_message) ? ' AND in_message = 0' : '') .
84 6
				(sizeof($allowed_extensions) ? ' AND ' . $this->db->sql_in_set('extension', $allowed_extensions) : '') . '
85 6
			ORDER BY ' . $order_by;
86
	}
87
}
88