Passed
Push — templating ( 0cf8c2 )
by Daniel
20:43
created

birthday   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_template() 0 3 1
A get_user_age() 0 4 2
A __construct() 0 7 1
A display() 0 15 2
A adjust_leap_year() 0 9 4
A find_birthday_users() 0 29 2
1
<?php
2
3
/**
4
 *
5
 * @package sitemaker
6
 * @copyright (c) 2013 Daniel A. (blitze)
7
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
8
 *
9
 */
10
11
namespace blitze\sitemaker\blocks;
12
13
use blitze\sitemaker\services\blocks\driver\block;
14
15
/**
16
 * Birthday Block
17
 */
18
class birthday extends block
19
{
20
	/** @var \phpbb\cache\driver\driver_interface */
21
	protected $cache;
22
23
	/** @var \phpbb\db\driver\driver_interface */
24
	protected $db;
25
26
	/** @var \phpbb\template\template */
27
	protected $template;
28
29
	/** @var \phpbb\user */
30
	protected $user;
31
32
	/** @var string */
33
	protected $time;
34
35
	/**
36
	 * Constructor
37
	 *
38
	 * @param \phpbb\cache\driver\driver_interface	$cache		Cache driver interface
39
	 * @param \phpbb\db\driver\driver_interface		$db     	Database connection
40
	 * @param \phpbb\template\template				$template	Template object
41
	 * @param \phpbb\user                           $user		User object
42
	 * @param string								$time		String in a format accepted by strtotime().
43
	 */
44
	public function __construct(\phpbb\cache\driver\driver_interface $cache, \phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, $time = 'now')
45
	{
46
		$this->cache = $cache;
47
		$this->db = $db;
48
		$this->template = $template;
49
		$this->user = $user;
50
		$this->time = $time;
51
	}
52
53
	/**
54
	 * {@inheritdoc}
55
	 */
56
	public function display(array $bdata, $edit_mode = false)
57
	{
58
		if (($data = $this->cache->get('pt_block_data_' . $bdata['bid'])) === false)
59
		{
60
			$data = $this->find_birthday_users();
61
62
			// we only check birthdays every hour, may make this an admin choice
63
			$this->cache->put('pt_block_data_' . $bdata['bid'], $data, 3600);
64
		}
65
66
		$this->template->assign_var('S_DISPLAY_BIRTHDAY_LIST', false);
67
68
		return array(
69
			'title'	=> 'BIRTHDAYS',
70
			'data'	=> $data,
71
		);
72
	}
73
74
	/**
75
	 * @return bool
76
	 */
77
	private function find_birthday_users()
78
	{
79
		$time = $this->user->create_datetime($this->time);
80
		$now = phpbb_gmgetdate($time->getTimestamp() + $time->getOffset());
81
82
		$leap_year_birthdays = $this->adjust_leap_year($now, $time);
83
84
		$sql = 'SELECT u.user_id, u.username, u.user_colour, u.user_birthday 
85
				FROM ' . USERS_TABLE . ' u
86
				LEFT JOIN ' . BANLIST_TABLE . ' b ON (u.user_id = b.ban_userid)
87
				WHERE (b.ban_id IS NULL
88
					OR b.ban_exclude = 1)
89
					AND (u.user_birthday ' . $this->db->sql_like_expression(sprintf('%2d-%2d-', $now['mday'], $now['mon']) . $this->db->get_any_char()) . " $leap_year_birthdays)
90
					AND u.user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')
91
				ORDER BY u.username ASC';
92
		$result = $this->db->sql_query($sql);
93
94
		$birthdays = [];
95
		while ($row = $this->db->sql_fetchrow($result))
96
		{
97
			$show_birthday = true;
0 ignored issues
show
Unused Code introduced by
The assignment to $show_birthday is dead and can be removed.
Loading history...
98
			$birthdays[] = array(
99
				'USERNAME'		=> get_username_string('full', $row['user_id'], $row['username'], $row['user_colour']),
100
				'USER_AGE'		=> $this->get_user_age($row['user_birthday'], $now['year']),
101
			);
102
		}
103
		$this->db->sql_freeresult($result);
104
105
		return $birthdays;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $birthdays returns the type array|array<mixed,array<string,string>> which is incompatible with the documented return type boolean.
Loading history...
106
	}
107
108
	/**
109
	 * Display birthdays of 29th february on 28th february in non-leap-years
110
	 *
111
	 * @param array $now
112
	 * @param \DateTime $time
113
	 * @return string
114
	 */
115
	private function adjust_leap_year(array $now, \DateTime $time)
116
	{
117
		$leap_year_birthdays = '';
118
		if ($now['mday'] == 28 && $now['mon'] == 2 && !$time->format('L'))
119
		{
120
			$leap_year_birthdays = ' OR u.user_birthday ' . $this->db->sql_like_expression(sprintf('%2d-%2d-', 29, 2) . $this->db->get_any_char());
121
		}
122
123
		return $leap_year_birthdays;
124
	}
125
126
	/**
127
	 * @param string $user_birthday
128
	 * @param int $year
129
	 * @return string
130
	 */
131
	private function get_user_age($user_birthday, $year)
132
	{
133
		$birthday_year = (int) substr($user_birthday, -4);
134
		return ($birthday_year) ? max(0, $year - $birthday_year) : '';
135
	}
136
137
	/**
138
	 * {@inheritdoc}
139
	 */
140
	public function get_template()
141
	{
142
		return '@blitze_sitemaker/blocks/birthday.html';
143
	}
144
}
145