forked from AppStateESS/InternshipInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmergencyContactFactory.php
95 lines (74 loc) · 2.59 KB
/
EmergencyContactFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace Intern;
class EmergencyContactFactory {
/**
* Returns an array of EmergencyContact objects for the given Internship.
*
* @param Internship $i
* @return Array<EmergencyContact> Array of EmergencyContact objects for the given Internship, or an empty array if none exist.
* @throws InvalidArgumentException
* @throws Exception
* @see EmergencyContactDB
*/
public static function getContactsForInternship(Internship $i)
{
$internshipId = $i->getId();
if(is_null($internshipId) || !isset($internshipId)){
throw new \InvalidArgumentException('Internship ID is required.');
}
$db = new \PHPWS_DB('intern_emergency_contact');
$db->addWhere('internship_id', $internshipId);
$db->addOrder('id ASC'); // Get them in order of ID, so earliest contacts come first
$result = $db->getObjects('Intern\EmergencyContactDB');
if(\PHPWS_Error::logIfError($result)){
throw new \Exception($result->toString());
}
if(sizeof($result) <= 0){
return array(); // Return an empty array
}
return $result;
}
/**
* Returns the EmergencyContact object with the given id from the database.
*
* @param int $id
* @return EmergencyContact
* @throws InvalidArgumentException
* @throws Exception
* @see EmergencyContactDB
*/
public static function getContactById($id)
{
if(is_null($id) || !isset($id)){
throw new \InvalidArgumentException('Missing contact id.');
}
$db = new \PHPWS_DB('intern_emergency_contact');
$db->addWHere('id', $id);
$contact = new EmergencyContactDB();
$result = $db->loadObject($contact);
if(\PHPWS_Error::logIfError($result)){
throw new \Exception($result->toString());
}
return $contact;
}
/**
* Deletes the passed in EmergencyContact from the database.
* @param EmergencyContact $contact
* @throws InvalidArgumentException
* @throws Exception
*/
public static function delete(EmergencyContact $contact)
{
$contactId = $contact->getId();
if(is_null($contactId) || !isset($contactId)){
throw new \InvalidArgumentException('Missing contact id.');
}
$db = new \PHPWS_DB('intern_emergency_contact');
$db->addWhere('id', $contactId);
$result = $db->delete();
if(\PHPWS_Error::logIfError($result)){
throw new \Exception($result->toString());
}
return true;
}
}