GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( dec958...1175d2 )
by Jacky
32s
created

NotificationRepository::cleanNotifications()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Asylamba\Modules\Hermes\Repository;
4
5
use Asylamba\Classes\Entity\AbstractRepository;
6
7
use Asylamba\Modules\Hermes\Model\Notification;
8
9
class NotificationRepository extends AbstractRepository
10
{
11
	/**
12
	 * @param int $id
13
	 * @return Notification
14
	 */
15
	public function get($id)
16
	{
17
		if (($n = $this->unitOfWork->getObject(Notification::class, $id)) !== null) {
18
			return $n;
19
		}
20
		$statement = $this->connection->prepare('SELECT * FROM notification WHERE id = :id');
21
		$statement->execute(['id' => $id]);
22
		
23
		if (($row = $statement->fetch()) === false) {
24
			return null;
25
		}
26
		$notification = $this->format($row);
27
		$this->unitOfWork->addObject($notification);
28
		return $notification;
29
	}
30
	
31
	public function getUnreadNotifications($playerId)
32
	{
33
		$statement = $this->connection->prepare('SELECT * FROM notification WHERE rPlayer = :player_id AND readed = 0 ORDER BY dSending DESC');
34
		$statement->execute(['player_id' => $playerId]);
35
		$data = [];
36
		while ($row = $statement->fetch()) {
37
			if (($n = $this->unitOfWork->getObject(Notification::class, $row['id'])) !== null) {
38
				$data[] = $n;
39
				continue;
40
			}
41
			$notification = $this->format($row);
42
			$this->unitOfWork->addObject($notification);
43
			$data[] = $notification;
44
		}
45
		return $data;
46
	}
47
	
48
	public function getPlayerNotificationsByArchive($playerId, $isArchived)
49
	{
50
		$statement = $this->connection->prepare(
51
			'SELECT * FROM notification WHERE rPlayer = :player_id AND archived = :is_archived ORDER BY dSending DESC LIMIT 50');
52
		$statement->execute(['player_id' => $playerId, 'is_archived' => $isArchived]);
53
		$data = [];
54
		while ($row = $statement->fetch()) {
55
			if (($n = $this->unitOfWork->getObject(Notification::class, $row['id'])) !== null) {
56
				$data[] = $n;
57
				continue;
58
			}
59
			$notification = $this->format($row);
60
			$this->unitOfWork->addObject($notification);
61
			$data[] = $notification;
62
		}
63
		return $data;
64
	}
65
	
66
	public function getAllByReadState($isReaded)
67
	{
68
		$statement = $this->connection->prepare(
69
			'SELECT * FROM notification WHERE readed = :is_readed');
70
		$statement->execute(['is_readed' => $isReaded]);
71
		$data = [];
72
		while ($row = $statement->fetch()) {
73
			if (($n = $this->unitOfWork->getObject(Notification::class, $row['id'])) !== null) {
74
				$data[] = $n;
75
				continue;
76
			}
77
			$notification = $this->format($row);
78
			$this->unitOfWork->addObject($notification);
79
			$data[] = $notification;
80
		}
81
		return $data;
82
	}
83
	
84
	/**
85
	 * @param int $commanderPlayerId
86
	 * @param int $placePlayerId
87
	 * @param string $arrivedAt
88
	 * @return array
89
	 */
90
	public function getMultiCombatNotifications($commanderPlayerId, $placePlayerId, $arrivedAt)
91
	{
92
		
93
		$statement = $this->connection->prepare(
94
			'SELECT * FROM notification WHERE (rPlayer = :commander_player_id OR rPlayer = :place_player_id) AND dSending = :arrived_at');
95
		$statement->execute([
96
			'commander_player_id' => $commanderPlayerId,
97
			'place_player_id' => $placePlayerId,
98
			'arrived_at' => $arrivedAt
99
		]);
100
		$data = [];
101
		while ($row = $statement->fetch()) {
102
			if (($n = $this->unitOfWork->getObject(Notification::class, $row['id'])) !== null) {
103
				$data[] = $n;
104
				continue;
105
			}
106
			$notification = $this->format($row);
107
			$this->unitOfWork->addObject($notification);
108
			$data[] = $notification;
109
		}
110
		return $data;
111
	}
112
	
113
    public function insert($notification)
114
    {
115
		$qr = $this->connection->prepare('INSERT INTO
116
			notification(rPlayer, title, content, dSending, readed, archived)
117
			VALUES(?, ?, ?, ?, ?, ?)');
118
		$qr->execute(array(
119
			$notification->getRPlayer(),
120
			$notification->getTitle(),
121
			$notification->getContent(),
122
			$notification->getDSending(),
123
			(int) $notification->getReaded(),
124
			(int) $notification->getArchived()
125
		));
126
		$notification->setId($this->connection->lastInsertId());
127
    }
128
    
129
    public function update($notification)
130
    {
131
        $statement = $this->connection->prepare(
132
            'UPDATE notification SET id = ?,
133
                rPlayer = ?,
134
                title = ?,
135
                content = ?,
136
                dSending = ?,
137
                readed = ?,
138
                archived = ?
139
            WHERE id = ?'
140
        );
141
        $statement->execute(array(
142
            $notification->getId(),
143
            $notification->getRPlayer(),
144
            $notification->getTitle(),
145
            $notification->getContent(),
146
            $notification->getDSending(),
147
            (int) $notification->getReaded(),
148
            (int) $notification->getArchived(),
149
            $notification->getId()
150
        ));
151
    }
152
    
153
    public function remove($notification)
154
    {
155
		$statement = $this->connection->prepare('DELETE FROM notification WHERE id = ?');
156
		$statement->execute(array($notification->id));
157
    }
158
    
159
    /**
160
     * @param int $playerId
161
     * @return int
162
     */
163
    public function removePlayerNotifications($playerId)
164
    {
165
		$statement = $this->connection->prepare('DELETE FROM notification WHERE rPlayer = ? AND archived = 0');
166
		$statement->execute(array($playerId));
167
        
168
        return $statement->rowCount();
169
    }
170
	
171
	/**
172
	 * @param string $readTimeout
173
	 * @param string $unreadTimeout
174
	 * @return int
175
	 */
176
	public function cleanNotifications($readTimeout, $unreadTimeout)
177
	{
178
		$statement = $this->connection->prepare(
179
			'DELETE FROM notification WHERE
180
			(readed = 0 AND TIMESTAMPDIFF(HOUR, dSending, NOW()) < :unread_timeout) OR
181
			(readed = 1 AND TIMESTAMPDIFF(HOUR, dSending, NOW()) < :read_timeout)'
182
		);
183
		$statement->execute(['read_timeout' => $readTimeout, 'unread_timeout' => $unreadTimeout]);
184
		return $statement->rowCount();
185
	}
186
    
187
    /**
188
     * @param array $data
189
     * @return Notification
190
     */
191
    public function format($data)
192
    {
193
        $notification = new Notification();
194
        $notification->setId((int) $data['id']);
195
        $notification->setRPlayer((int) $data['rPlayer']);
196
        $notification->setTitle($data['title']);
197
        $notification->setContent($data['content']);
198
        $notification->setDSending($data['dSending']);
199
        $notification->setReaded((bool) $data['readed']);
200
        $notification->setArchived((bool) $data['archived']);
201
        return $notification;
202
    }
203
}