Completed
Push — develop ( 755a17...843010 )
by Daniel
07:33
created

posting   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 201
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 1
dl 0
loc 201
ccs 0
cts 121
cp 0
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 14 1
B init_builder() 0 10 5
A build_message() 0 9 2
A show_errors() 0 7 2
A modify_post_data() 0 9 2
A modify_sql_data() 0 9 2
A set_content_post_id() 0 7 2
A modify_topic_review() 0 10 3
B set_redirect_url() 0 22 4
A save_fields() 0 7 3
A build_form() 0 19 3
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2013 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\event;
11
12
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
class posting implements EventSubscriberInterface
15
{
16
	/** @var \blitze\content\services\form\builder */
17
	protected $builder;
18
19
	/** @var string */
20
	protected $content_type = '';
21
22
	/** @var bool */
23
	protected $build_content = false;
24
25
	/** @var int */
26
	protected $content_post_id = 0;
27
28
	/**
29
	 * Constructor
30
	 *
31
	 * @param \blitze\content\services\form\builder		$builder		Form builder object
32
	*/
33
	public function __construct(\blitze\content\services\form\builder $builder)
34
	{
35
		$this->builder = $builder;
36
	}
37
38
	/**
39
	 * @return array
40
	 */
41
	static public function getSubscribedEvents()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
42
	{
43
		return array(
44
			'core.modify_posting_auth'					=> 'init_builder',
45
			'core.posting_modify_message_text'			=> 'build_message',
46
			'core.posting_modify_submission_errors'		=> 'show_errors',
47
			'core.posting_modify_submit_post_before'	=> 'modify_post_data',
48
			'core.submit_post_modify_sql_data'			=> 'modify_sql_data',
49
			'core.posting_modify_submit_post_after'		=> array('save_fields', 'set_redirect_url'),
50
			'core.topic_review_modify_post_list'		=> 'set_content_post_id',
51
			'core.topic_review_modify_row'				=> 'modify_topic_review',
52
			'core.posting_modify_template_vars'			=> 'build_form',
53
		);
54
	}
55
56
	/**
57
	 * @param \phpbb\event\data $event
58
	 * @return void
59
	 */
60
	public function init_builder(\phpbb\event\data $event)
61
	{
62
		$this->content_type = $this->builder->init($event['forum_id'], $event['topic_id'], $event['mode'], $event['save']);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->builder->init($ev...mode'], $event['save']) can also be of type boolean. However, the property $content_type is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
63
64
		$topic_first_post_id = $event['post_data']['topic_first_post_id'];
65
		if (!$topic_first_post_id || $topic_first_post_id == $event['post_id'])
66
		{
67
			$this->build_content = ($this->content_type && $event['mode'] !== 'reply') ? true : false;
68
		}
69
	}
70
71
	/**
72
	 * @param \phpbb\event\data $event
73
	 * @return void
74
	 */
75
	public function build_message(\phpbb\event\data $event)
76
	{
77
		if ($this->build_content)
78
		{
79
			$message_parser = $event['message_parser'];
80
			$message_parser->message = $this->builder->generate_message();
81
			$event['message_parser'] = $message_parser;
82
		}
83
	}
84
85
	/**
86
	 * @param \phpbb\event\data $event
87
	 * @return void
88
	 */
89
	public function show_errors(\phpbb\event\data $event)
90
	{
91
		if ($this->build_content)
92
		{
93
			$event['error'] = $this->builder->get_errors();
94
		}
95
	}
96
97
	/**
98
	 * @param \phpbb\event\data $event
99
	 * @return void
100
	 */
101
	public function modify_post_data(\phpbb\event\data $event)
102
	{
103
		if ($this->build_content)
104
		{
105
			$data = $event['data'];
106
			$this->builder->force_visibility($data);
107
			$event['data'] = $data;
108
		}
109
	}
110
111
	/**
112
	 * @param \phpbb\event\data $event
113
	 * @return void
114
	 */
115
	public function modify_sql_data(\phpbb\event\data $event)
116
	{
117
		if ($this->build_content)
118
		{
119
			$sql_data = $event['sql_data'];
120
			$this->builder->modify_posting_data($sql_data[TOPICS_TABLE]['sql']);
121
			$event['sql_data'] = $sql_data;
122
		}
123
	}
124
125
	/**
126
	 * @param \phpbb\event\data $event
127
	 * @return void
128
	 */
129
	public function set_content_post_id(\phpbb\event\data $event)
130
	{
131
		if ($this->content_type)
132
		{
133
			$this->content_post_id = array_pop($event['post_list']);
134
		}
135
	}
136
137
	/**
138
	 * @param \phpbb\event\data $event
139
	 * @return void
140
	 */
141
	public function modify_topic_review(\phpbb\event\data $event)
142
	{
143
		if ($this->content_type && $event['row']['post_id'] === $this->content_post_id)
144
		{
145
			$post_row = $event['post_row'];
146
			$post_row['MESSAGE'] = $this->builder->get_content_view($this->content_type, $post_row, 'summary');
147
			$event['post_row'] = $post_row;
148
			unset($post_row);
149
		}
150
	}
151
152
	/**
153
	 * @param \phpbb\event\data $event
154
	 * @return void
155
	 */
156
	public function set_redirect_url(\phpbb\event\data $event)
157
	{
158
		if ($this->content_type)
159
		{
160
			if ($this->build_content)
161
			{
162
				$event['redirect_url'] = $this->builder->get_cp_url();
163
			}
164
			else
165
			{
166
				$topic_url = $this->builder->get_post_url($this->content_type, $event['post_data']);
167
168
				$post_id = $event['post_id'];
169
				if ($post_id != $event['post_data']['topic_first_post_id'])
170
				{
171
					$topic_url .= "?p=$post_id#p$post_id";
172
				}
173
174
				$event['redirect_url'] = $topic_url;
175
			}
176
		}
177
	}
178
179
	/**
180
	 * @param \phpbb\event\data $event
181
	 * @return void
182
	 */
183
	public function save_fields(\phpbb\event\data $event)
184
	{
185
		if ($this->build_content && in_array($event['mode'], array('post', 'edit', 'save')))
186
		{
187
			$this->builder->save_db_fields($event['topic_id']);
188
		}
189
	}
190
191
	/**
192
	 * @param \phpbb\event\data $event
193
	 * @return void
194
	 */
195
	public function build_form(\phpbb\event\data $event)
196
	{
197
		if ($this->build_content)
198
		{
199
			$post_data = $event['post_data'];
200
			$page_data = $event['page_data'];
201
202
			$post_data['TOPIC_URL'] = './';
203
			$page_data['SITEMAKER_FORM'] = $this->builder->generate_form($event['topic_id'], $post_data, $page_data);
204
205
			if ($event['preview'])
206
			{
207
				$page_data['PREVIEW_MESSAGE'] = $this->builder->generate_preview($this->content_type, $post_data);
208
			}
209
210
			$event['page_data'] = $page_data;
211
			unset($post_data, $page_data);
212
		}
213
	}
214
}
215