absences_RightInPeriod   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 124
Duplicated Lines 15.32 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 19
loc 124
rs 10
wmc 12
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getFromId() 0 6 1
A getRow() 19 19 3
A setRight() 0 5 1
A getRight() 0 10 2
A save() 0 13 2
B insert() 0 27 1
A update() 0 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/************************************************************************
3
 * OVIDENTIA http://www.ovidentia.org                                   *
4
 ************************************************************************
5
 * Copyright (c) 2003 by CANTICO ( http://www.cantico.fr )              *
6
 *                                                                      *
7
 * This file is part of Ovidentia.                                      *
8
 *                                                                      *
9
 * Ovidentia is free software; you can redistribute it and/or modify    *
10
 * it under the terms of the GNU General Public License as published by *
11
 * the Free Software Foundation; either version 2, or (at your option)  *
12
 * any later version.													*
13
 *																		*
14
 * This program is distributed in the hope that it will be useful, but  *
15
 * WITHOUT ANY WARRANTY; without even the implied warranty of			*
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.					*
17
 * See the  GNU General Public License for more details.				*
18
 *																		*
19
 * You should have received a copy of the GNU General Public License	*
20
 * along with this program; if not, write to the Free Software			*
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,*
22
 * USA.																	*
23
************************************************************************/
24
25
require_once dirname(__FILE__).'/record.class.php';
26
require_once dirname(__FILE__).'/right.class.php';
27
28
/**
29
 * Absence InPeriod
30
 * 
31
 * @property 	int		$id_right
32
 * @property	string	$period_start
33
 * @property	string	$period_end
34
 * @property	int		$right_inperiod
35
 * @property	string	$uuid
36
 * 
37
 */
38
class absences_RightInPeriod extends absences_Record 
39
{
40
	private $right;
41
	
42
	private $id_right;
43
	
44
	public static function getFromId($id)
45
	{
46
		$rightinperiod = new absences_RightInPeriod;
47
		$rightinperiod->id = $id;
48
		return $rightinperiod;
49
	}
50
	
51
	/**
52
	 * 
53
	 * @return array
54
	 */
55 View Code Duplication
	public function getRow()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
	{
57
		if (null === $this->row)
58
		{
59
			global $babDB;
60
			
61
			$query = 'SELECT * FROM absences_rights_inperiod WHERE ';
62
			
63
			if (isset($this->id))
64
			{
65
				$query .= 'id='.$babDB->quote($this->id);
66
			}
67
			
68
			$res = $babDB->db_query($query);
69
			$this->setRow($babDB->db_fetch_assoc($res));
70
		}
71
		
72
		return $this->row;
73
	}
74
	
75
	/**
76
	 * 
77
	 * @param absences_Right $right
78
	 * @return absences_RightInPeriod
79
	 */
80
	public function setRight(absences_Right $right)
81
	{
82
		$this->right = $right;
83
		return $this;
84
	}
85
	
86
	/**
87
	 * @return absences_Right
88
	 */
89
	public function getRight()
90
	{
91
		if (!isset($this->right))
92
		{
93
			$row = $this->getRow();
94
			$this->right = new absences_Right($row['id_right']);
95
		}
96
		
97
		return $this->right;
98
	}
99
	
100
	
101
	
102
	public function save()
103
	{
104
		global $babDB;
105
		
106
		$res = $babDB->db_query('SELECT * FROM absences_rights_inperiod WHERE uuid='.$babDB->quote($this->uuid));
107
		if (0 === $babDB->db_num_rows($res))
108
		{
109
			$this->insert();
110
		} else {
111
			
112
			$this->update();
113
		}
114
	}
115
	
116
	
117
	public function insert()
118
	{
119
		global $babDB;
120
		require_once $GLOBALS['babInstallPath'].'utilit/uuid.php';
121
		
122
		$right = $this->getRight();
123
		$this->id_right = $right->id;
124
		
125
		$babDB->db_query('
126
			INSERT INTO absences_rights_inperiod 
127
				(
128
					id_right,
129
					period_start,
130
					period_end,
131
					right_inperiod,
132
					uuid
133
				)
134
			VALUES 
135
				(
136
					'.$babDB->quote($this->id_right).',
137
					'.$babDB->quote($this->period_start).',
138
					'.$babDB->quote($this->period_end).',
139
					'.$babDB->quote($this->right_inperiod).',
140
					'.$babDB->quote($this->uuid).'
141
				)
142
		');
143
	}
144
	
145
	
146
	public function update()
147
	{
148
		global $babDB;
149
		
150
		if (empty($this->uuid))
151
		{
152
			throw new Exception('missing absences_rights_inperiod.uuid');
153
		}
154
		
155
		$babDB->db_query('UPDATE absences_rights_inperiod SET 
156
			period_start	='.$babDB->quote($this->period_start).',
157
			period_end		='.$babDB->quote($this->period_end).',
158
			right_inperiod	='.$babDB->quote($this->right_inperiod).' 
159
		WHERE uuid='.$babDB->quote($this->uuid));
160
	}
161
}
162