Completed
Pull Request — master (#3)
by Matt
02:18
created

manager   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 9
Bugs 0 Features 4
Metric Value
wmc 16
c 9
b 0
f 4
lcom 1
cbo 1
dl 0
loc 110
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get_prefix() 0 6 2
A get_prefixes() 0 9 2
A get_active_prefixes() 0 4 1
A add_prefix() 0 10 2
A delete_prefix() 0 4 1
A update_prefix() 0 4 1
A move_prefix() 0 7 2
A is_enabled() 0 4 1
A prepend_prefix() 0 9 3
1
<?php
2
/**
3
 *
4
 * Topic Prefixes extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2016 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\topicprefixes\prefixes;
12
13
class manager implements manager_interface
14
{
15
	/**
16
	 * @var \phpbb\topicprefixes\prefixes\nestedset_prefixes
17
	 */
18
	protected $nestedset;
19
20
	/**
21
	 * Listener constructor
22
	 *
23
	 * @param \phpbb\topicprefixes\prefixes\nestedset_prefixes $nestedset
24
	 */
25
	public function __construct(\phpbb\topicprefixes\prefixes\nestedset_prefixes $nestedset)
26
	{
27
		$this->nestedset = $nestedset;
28
	}
29
30
	/**
31
	 * @inheritdoc
32
	 */
33
	public function get_prefix($id)
34
	{
35
		$prefix = $this->nestedset->get_subtree_data($id);
36
37
		return sizeof($prefix) ? $prefix[$id] : false;
38
	}
39
40
	/**
41
	 * @inheritdoc
42
	 */
43
	public function get_prefixes($forum_id = 0)
44
	{
45
		if ($forum_id)
46
		{
47
			$this->nestedset->set_forum_id($forum_id);
48
		}
49
50
		return $this->nestedset->get_all_tree_data();
51
	}
52
53
	/**
54
	 * @inheritdoc
55
	 */
56
	public function get_active_prefixes($forum_id = 0)
57
	{
58
		return array_filter($this->get_prefixes($forum_id), [$this, 'is_enabled']);
59
	}
60
61
	/**
62
	 * @inheritdoc
63
	 */
64
	public function add_prefix($tag, $forum_id)
65
	{
66
		$data = [
67
			'prefix_tag'		=> $tag,
68
			'forum_id'			=> (int) $forum_id,
69
			'prefix_enabled'	=> true,
70
		];
71
72
		return $tag !== '' ? $this->nestedset->insert($data) : false;
73
	}
74
75
	/**
76
	 * @inheritdoc
77
	 */
78
	public function delete_prefix($id)
79
	{
80
		return $this->nestedset->delete($id);
81
	}
82
83
	/**
84
	 * @inheritdoc
85
	 */
86
	public function update_prefix($id, array $data)
87
	{
88
		return $this->nestedset->update_item($id, $data);
89
	}
90
91
	/**
92
	 * @inheritdoc
93
	 */
94
	public function move_prefix($id, $direction = 'up', $amount = 1)
95
	{
96
		$id = (int) $id;
97
		$amount = (int) $amount;
98
99
		$this->nestedset->move($id, ($direction !== 'up' ? -$amount : $amount));
100
	}
101
102
	/**
103
	 * @inheritdoc
104
	 */
105
	public function is_enabled(array $row)
106
	{
107
		return $row['prefix_enabled'];
108
	}
109
110
	/**
111
	 * @inheritdoc
112
	 */
113
	public function prepend_prefix($prefix, $subject)
114
	{
115
		if ($prefix && strpos($subject, $prefix) !== 0)
116
		{
117
			$subject = $prefix . ' ' . $subject;
118
		}
119
120
		return $subject;
121
	}
122
}
123