display::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 7
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * Topic Preview
5
 *
6
 * @copyright (c) 2013 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\topicpreview\core;
12
13
use phpbb\config\config;
0 ignored issues
show
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use phpbb\event\dispatcher_interface;
0 ignored issues
show
Bug introduced by
The type phpbb\event\dispatcher_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use phpbb\language\language;
0 ignored issues
show
Bug introduced by
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use phpbb\template\template;
0 ignored issues
show
Bug introduced by
The type phpbb\template\template was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use phpbb\user;
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use vse\topicpreview\core\trim\trim;
19
20
class display extends base
21
{
22
	/** @var int default width of topic preview */
23
	const PREVIEW_SIZE = 360;
24
25
	/** @var int default height and width of topic preview avatars */
26
	const AVATAR_SIZE = 60;
27
28
	/** @var string */
29
	const NO_AVATAR = 'no-avatar';
30
31
	/** @var dispatcher_interface */
32
	protected $dispatcher;
33
34
	/** @var language */
35
	protected $language;
36
37
	/** @var template */
38
	protected $template;
39
40
	/** @var string phpBB root path */
41
	protected $root_path;
42
43
	/** @var trim */
44
	protected $trim;
45
46
	/**
47
	 * Constructor
48
	 *
49
	 * @param config               $config     Config object
50
	 * @param dispatcher_interface $dispatcher Event dispatcher object
51
	 * @param language             $language   Language object
52
	 * @param template             $template   Template object
53
	 * @param trim                 $trim       Trim text object
54
	 * @param user                 $user       User object
55
	 * @param string               $root_path
56
	 */
57 9
	public function __construct(config $config, dispatcher_interface $dispatcher, language $language, template $template, trim $trim, user $user, $root_path)
58
	{
59 9
		$this->dispatcher = $dispatcher;
60 9
		$this->language = $language;
61 9
		$this->template = $template;
62 9
		$this->trim = $trim;
63 9
		$this->root_path = $root_path;
64 9
		parent::__construct($config, $user);
65
66 9
		$this->setup();
67 9
	}
68
69
	/**
70
	 * Set up some common components
71
	 */
72 9
	public function setup()
73
	{
74
		// Load our language file (only needed if showing last post text)
75 9
		if ($this->last_post_enabled())
76 9
		{
77 6
			$this->language->add_lang('topic_preview', 'vse/topicpreview');
78 6
		}
79
80 9
		$this->template->assign_vars(array(
81 9
			'S_TOPICPREVIEW'		=> $this->is_enabled(),
82 9
			'TOPICPREVIEW_THEME'	=> $this->get_theme(),
83 9
			'TOPICPREVIEW_DELAY'	=> $this->config['topic_preview_delay'],
84 9
			'TOPICPREVIEW_DRIFT'	=> $this->config['topic_preview_drift'],
85 9
			'TOPICPREVIEW_WIDTH'	=> !empty($this->config['topic_preview_width']) ? $this->config['topic_preview_width'] : self::PREVIEW_SIZE,
86 9
		));
87 9
	}
88
89
	/**
90
	 * Inject topic preview text into the template
91
	 *
92
	 * @param array $row   Row data
93
	 * @param array $block Template vars array
94
	 *
95
	 * @return array Template vars array
96
	 */
97 6
	public function display_topic_preview($row, $block)
98
	{
99 6
		if (!$this->is_enabled())
100 6
		{
101 1
			return $block;
102
		}
103
104 5
		$block = array_merge($block, array(
105 5
			'TOPIC_PREVIEW_FIRST_POST'		=> $this->get_text_helper($row, 'first_post_text'),
106 5
			'TOPIC_PREVIEW_LAST_POST'		=> $this->get_text_helper($row, 'last_post_text'),
107 5
			'TOPIC_PREVIEW_FIRST_AVATAR'	=> $this->get_user_avatar_helper($row, 'fp'),
108 5
			'TOPIC_PREVIEW_LAST_AVATAR'		=> $this->get_user_avatar_helper($row, 'lp'),
109 5
		));
110
111 5
		$tp_avatars = $this->avatars_enabled();
112
113
		/**
114
		 * EVENT to modify the topic preview display output before it gets inserted in the template block
115
		 *
116
		 * @event vse.topicpreview.display_topic_preview
117
		 * @var array row        Topic row data
118
		 * @var array block      Template vars array
119
		 * @var bool  tp_avatars Display avatars setting
120
		 * @since 2.1.0
121
		 */
122 5
		$vars = array('row', 'block', 'tp_avatars');
123 5
		extract($this->dispatcher->trigger_event('vse.topicpreview.display_topic_preview', compact($vars)));
124
125 5
		return $block;
126
	}
127
128
	/**
129
	 * Get topic preview text helper function
130
	 * This handles the trimming and censoring
131
	 *
132
	 * @param array  $row  User row data
133
	 * @param string $post The first or last post text column key
134
	 *
135
	 * @return string The trimmed and censored topic preview text
136
	 */
137 5
	protected function get_text_helper($row, $post)
138
	{
139
		// Ignore empty/unset text or when the last post is also the first (and only) post
140 5
		if (empty($row[$post]) || ($post === 'last_post_text' && $row['topic_first_post_id'] === $row['topic_last_post_id']))
141 5
		{
142 3
			return '';
143
		}
144
145 4
		return censor_text($this->trim->trim_text($row[$post], $this->config['topic_preview_limit']));
0 ignored issues
show
Bug introduced by
The function censor_text was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
		return /** @scrutinizer ignore-call */ censor_text($this->trim->trim_text($row[$post], $this->config['topic_preview_limit']));
Loading history...
146
	}
147
148
	/**
149
	 * Get user avatar helper function
150
	 *
151
	 * @param array  $row    User row data
152
	 * @param string $poster Type of poster, fp or lp
153
	 *
154
	 * @return string Avatar image
155
	 */
156 5
	protected function get_user_avatar_helper($row, $poster)
157
	{
158 5
		if (!$this->avatars_enabled())
159 5
		{
160 1
			return '';
161
		}
162
163 4
		$avatar = '';
164 4
		if (!empty($row[$poster . '_avatar']))
165 4
		{
166
			$map = array(
167 3
				'avatar'		=> $row[$poster . '_avatar'],
168 3
				'avatar_type'	=> $row[$poster . '_avatar_type'],
169 3
				'avatar_width'	=> $row[$poster . '_avatar_width'],
170 3
				'avatar_height'	=> $row[$poster . '_avatar_height'],
171 3
			);
172 3
			$avatar = phpbb_get_user_avatar($map, 'USER_AVATAR', false, true);
0 ignored issues
show
Bug introduced by
The function phpbb_get_user_avatar was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

172
			$avatar = /** @scrutinizer ignore-call */ phpbb_get_user_avatar($map, 'USER_AVATAR', false, true);
Loading history...
173 3
		}
174
175
		// If avatar string is empty, fall back to no_avatar.gif
176 4
		return $avatar ?: self::NO_AVATAR;
177
	}
178
179
	/**
180
	 * Get user's style topic preview theme
181
	 * Fall back to no theme if expected theme not found
182
	 *
183
	 * @return mixed Theme name if theme file found, false otherwise
184
	 */
185 9
	protected function get_theme()
186
	{
187 9
		if (!empty($this->user->style['topic_preview_theme']) && file_exists($this->root_path . 'ext/vse/topicpreview/styles/all/theme/' . $this->user->style['topic_preview_theme'] . '.css'))
188 9
		{
189 1
			return $this->user->style['topic_preview_theme'];
190
		}
191
192 8
		return false;
193
	}
194
}
195