BoardIndex   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
eloc 19
dl 0
loc 58
rs 10
c 0
b 0
f 0
ccs 0
cts 32
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A post_load() 0 10 4
A hooks() 0 12 2
A pre_load() 0 19 3
1
<?php
2
3
/**
4
 * This file contains several functions for retrieving and manipulating calendar events, birthdays and holidays.
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * This file contains code covered by:
11
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
12
 *
13
 * @version 2.0 dev
14
 *
15
 */
16
17
namespace ElkArte\Modules\Calendar;
18
19
use ElkArte\Cache\Cache;
20
use ElkArte\EventManager;
21
use ElkArte\Modules\AbstractModule;
22
23
/**
24
 * This class's task is to show the upcoming events in the BoardIndex.
25
 */
26
class BoardIndex extends AbstractModule
27
{
28
	/**
29
	 * {@inheritDoc}
30
	 */
31
	public static function hooks(EventManager $eventsManager)
32
	{
33
		// Load the calendar?
34
		if (allowedTo('calendar_view'))
35
		{
36
			return [
37
				['pre_load', [BoardIndex::class, 'pre_load'], []],
38
				['post_load', [BoardIndex::class, 'post_load'], []],
39
			];
40
		}
41
42
		return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the return type mandated by ElkArte\Modules\ModuleInterface::hooks() of array.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
43
	}
44
45
	/**
46
	 * Pre-load hooks as part of board index
47
	 */
48
	public function pre_load(): void
49
	{
50
		global $modSettings, $context;
51
52
		// Retrieve the calendar data (events, birthdays, holidays).
53
		$eventOptions = [
54
			'include_holidays' => $modSettings['cal_showholidays'] > 1,
55
			'include_birthdays' => $modSettings['cal_showbdays'] > 1,
56
			'include_events' => $modSettings['cal_showevents'] > 1,
57
			'num_days_shown' => empty($modSettings['cal_days_for_index']) || $modSettings['cal_days_for_index'] < 1 ? 1 : $modSettings['cal_days_for_index'],
58
		];
59
60
		$context += Cache::instance()->quick_get('calendar_index_offset_' . ($this->user->time_offset + $modSettings['time_offset']), 'subs/Calendar.subs.php', 'cache_getRecentEvents', [$eventOptions]);
0 ignored issues
show
Bug Best Practice introduced by
The property time_offset does not exist on ElkArte\UserInfo. Since you implemented __get, consider adding a @property annotation.
Loading history...
61
62
		// Whether one or multiple days are shown on the board index.
63
		$context['calendar_only_today'] = (int) $modSettings['cal_days_for_index'] === 1;
64
65
		// This is used to show the "how-do-I-edit" help.
66
		$context['calendar_can_edit'] = allowedTo('calendar_edit_any');
67
	}
68
69
	/**
70
	 * post load functions, load calendar events for the board index as part of BoardIndex
71
	 *
72
	 * @param array $callbacks
73
	 */
74
	public function post_load(&$callbacks): void
75
	{
76
		global $context;
77
78
		if (empty($context['calendar_holidays']) && empty($context['calendar_birthdays']) && empty($context['calendar_events']))
79
		{
80
			return;
81
		}
82
83
		$callbacks = elk_array_insert($callbacks, 'recent_posts', ['show_events'], 'after', false);
84
	}
85
}
86