Card   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 1
dl 0
loc 95
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A setDatabaseType() 0 3 1
A getDuedate() 0 10 4
A jsonSerialize() 0 31 5
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Db;
25
26
use DateTime;
27
28
class Card extends RelationalEntity {
29
30
	protected $title;
31
	protected $description;
32
	protected $descriptionPrev;
33
	protected $stackId;
34
	protected $type;
35
	protected $lastModified;
36
	protected $lastEditor;
37
	protected $createdAt;
38
	protected $labels;
39
	protected $assignedUsers;
40
	protected $attachments;
41
	protected $attachmentCount;
42
	protected $owner;
43
	protected $order;
44
	protected $archived = false;
45
	protected $duedate;
46
	protected $notified = false;
47
	protected $deletedAt = 0;
48
	protected $commentsUnread = 0;
49
50
	private $databaseType = 'sqlite';
51
52
	const DUEDATE_FUTURE = 0;
53
	const DUEDATE_NEXT = 1;
54
	const DUEDATE_NOW = 2;
55
	const DUEDATE_OVERDUE = 3;
56
57
	public function __construct() {
58
		$this->addType('id', 'integer');
59
		$this->addType('stackId', 'integer');
60
		$this->addType('order', 'integer');
61
		$this->addType('lastModified', 'integer');
62
		$this->addType('createdAt', 'integer');
63
		$this->addType('archived', 'boolean');
64
		$this->addType('notified', 'boolean');
65
		$this->addType('deletedAt', 'integer');
66
		$this->addRelation('labels');
67
		$this->addRelation('assignedUsers');
68
		$this->addRelation('attachments');
69
		$this->addRelation('attachmentCount');
70
		$this->addRelation('participants');
71
		$this->addRelation('commentsUnread');
72
		$this->addResolvable('owner');
73
	}
74
75
	public function setDatabaseType($type) {
76
		$this->databaseType = $type;
77
	}
78
79
	public function getDuedate($isoFormat = false) {
80
		if ($this->duedate === null) {
81
			return null;
82
		}
83
		$dt = new DateTime($this->duedate);
84
		if (!$isoFormat && $this->databaseType === 'mysql') {
85
			return $dt->format('Y-m-d H:i:s');
86
		}
87
		return $dt->format('c');
88
	}
89
90
	public function jsonSerialize() {
91
		$json = parent::jsonSerialize();
92
		$json['overdue'] = self::DUEDATE_FUTURE;
93
		$due = strtotime($this->duedate);
94
95
		$today = new DateTime();
96
		$today->setTime(0, 0);
97
98
		$match_date = new DateTime($this->duedate);
99
100
		$match_date->setTime(0, 0);
101
102
		$diff = $today->diff($match_date);
103
		$diffDays = (integer) $diff->format('%R%a'); // Extract days count in interval
104
105
		if ($due !== false) {
106
			if ($diffDays === 1) {
107
				$json['overdue'] = self::DUEDATE_NEXT;
108
			}
109
			if ($diffDays === 0) {
110
				$json['overdue'] = self::DUEDATE_NOW;
111
			}
112
			if ($diffDays < 0) {
113
				$json['overdue'] = self::DUEDATE_OVERDUE;
114
			}
115
		}
116
		$json['duedate'] = $this->getDuedate(true);
117
		unset($json['notified']);
118
		unset($json['descriptionPrev']);
119
		return $json;
120
	}
121
122
}
123