Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 86c7ee6

Browse files
committedSep 20, 2023
[GT-184] Apply styles for the PR (#464)
1 parent 7bc3b3f commit 86c7ee6

11 files changed

+2036
-963
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
2+
23
/*______________________________________________________
34
*======================================================
45
* File: edit_api_auth.php
56
* Author: George Ryall
6-
* Description: Processes a edit API Authentication entity request. If the user
7-
* hasn't POSTed any data we draw the new site
7+
* Description: Processes a edit API Authentication entity request.
8+
* If the user hasn't POSTed any data we draw the new site
89
* form. If they post data we assume they've posted it from
910
* the form and validate then insert it into the DB.
1011
*
@@ -21,24 +22,38 @@
2122
* See the License for the specific language governing permissions and
2223
* limitations under the License.
2324
/*======================================================*/
24-
require_once __DIR__.'/../../../web_portal/components/Get_User_Principle.php';
25-
require_once __DIR__.'/../utils.php';
26-
require_once __DIR__.'/../../../../lib/Gocdb_Services/Factory.php';
25+
require_once __DIR__
26+
. '/../../../web_portal/components/Get_User_Principle.php';
27+
require_once __DIR__ . '/../utils.php';
28+
require_once __DIR__ . '/../../../../lib/Gocdb_Services/Factory.php';
29+
30+
use Exception;
2731

2832
/**
2933
* Controller to edit authentication entity request
30-
* @global array $_POST only set if the browser has POSTed data
34+
*
35+
* @global array $_POST only set if the browser has posted data
36+
*
3137
* @return null
3238
*/
33-
function edit_entity() {
39+
function edit_entity()
40+
{
3441
$dn = Get_User_Principle();
3542
$user = \Factory::getUserService()->getUserByPrinciple($dn);
3643

37-
//Check the portal is not in read only mode, returns exception if it is and user is not an admin
44+
/**
45+
* Check the portal is not in read only mode,
46+
* returns exception if it is and user is not an admin
47+
*/
3848
checkPortalIsNotReadOnlyOrUserIsAdmin($user);
3949

40-
if (!isset($_REQUEST['authentityid']) || !is_numeric($_REQUEST['authentityid']) ){
41-
throw new Exception("A authentication entity id must be specified in the url");
50+
if (
51+
!isset($_REQUEST['authentityid'])
52+
|| !is_numeric($_REQUEST['authentityid'])
53+
) {
54+
throw new Exception(
55+
"A authentication entity id must be specified in the url"
56+
);
4257
}
4358

4459
$serv = \Factory::getSiteService();
@@ -47,48 +62,67 @@ function edit_entity() {
4762

4863
// Validate the user has permission to edit properties
4964
if (!$serv->userCanEditSite($user, $site)) {
50-
throw new \Exception("Permission denied: a site role is required to edit authentication entities at " . $site->getShortName());
65+
throw new Exception(
66+
"Permission denied: a site role is required to edit "
67+
. "authentication entities at "
68+
. $site->getShortName()
69+
);
5170
}
5271

53-
if($_POST) { // If we receive a POST request it's to edit an authentication entity
72+
// If we receive a POST request it's to edit an authentication entity
73+
if ($_POST) {
5474
submit($user, $authEnt, $site, $serv);
55-
} else { // If there is no post data, draw the edit authentication entity form
75+
} else {
76+
// If there is no post data, draw the edit authentication entity form
5677
draw($user, $authEnt, $site);
5778
}
5879
}
5980

60-
function draw(\User $user = null, \APIAuthentication $authEnt = null, \Site $site = null) {
61-
if(is_null($user)){
62-
throw new Exception("Unregistered users can't edit authentication credentials");
81+
function draw(
82+
\User $user = null,
83+
\APIAuthentication $authEnt = null,
84+
\Site $site = null
85+
) {
86+
if (is_null($user)) {
87+
throw new Exception(
88+
"Unregistered users can't edit authentication credentials"
89+
);
6390
}
6491

6592
$params = array();
6693
$params['site'] = $site;
6794
$params['authEnt'] = $authEnt;
6895
$params['authTypes'] = array();
69-
$params['authTypes'][]='X.509';
70-
$params['authTypes'][]='OIDC Subject';
96+
$params['authTypes'][] = 'X.509';
97+
$params['authTypes'][] = 'OIDC Subject';
7198
$params['user'] = $user;
7299

73100
show_view("site/edit_api_auth.php", $params);
101+
74102
die();
75103
}
76104

77-
function submit(\User $user, \APIAuthentication $authEnt, \Site $site, org\gocdb\services\Site $serv) {
105+
function submit(
106+
\User $user,
107+
\APIAuthentication $authEnt,
108+
\Site $site,
109+
org\gocdb\services\Site $serv
110+
) {
78111
$newValues = getAPIAuthenticationFromWeb();
79112

80113
try {
81114
$authEnt = $serv->editAPIAuthEntity($authEnt, $user, $newValues);
82-
} catch(Exception $e) {
115+
} catch (Exception $e) {
83116
show_view('error.php', $e->getMessage());
117+
84118
die();
85119
}
86120

87121
$params = array();
88122
$params['apiAuthenticationEntity'] = $authEnt;
89123
$params['site'] = $site;
90-
show_view("site/edited_api_auth.php", $params);
91-
die();
92124

125+
show_view("site/edited_api_auth.php", $params);
93126

127+
die();
94128
}
+62-20
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<div class="rightPageContainer">
22
<?php
3-
43
$user = $params['user'];
54
$entUser = $params['authEnt']->getUser();
65

@@ -9,7 +8,6 @@
98
echo('</h1>');
109

1110
if (!is_null($entUser)) {
12-
1311
echo('<h4>This credential is linked to GOCDB user ');
1412
echo('<a href="');
1513
xecho(\GocContextPath::getPath());
@@ -27,46 +25,90 @@
2725
echo("' to '");
2826
xecho($user->getFullname());
2927
echo("'. Click the browser Back button to cancel the edit.</div>");
30-
}
31-
32-
} else {
33-
// This clause should be deleted or replaced with exception after all
34-
// authentication entities are assigned a user.
35-
echo('<div class="input_warning">');
36-
echo("WARNING: editing will link user '");
37-
xecho($user->getFullname());
38-
echo("' to this credential. Click the browser Back button to cancel the edit.</div>");
28+
}
29+
} else {
30+
/**
31+
* This clause should be deleted or replaced with exception after
32+
* all authentication entities are assigned a user.
33+
*/
34+
echo('<div class="input_warning">');
35+
echo("WARNING: editing will link user '");
36+
xecho($user->getFullname());
37+
echo(
38+
"' to this credential. Click the browser Back button "
39+
. "to cancel the edit.</div>"
40+
);
3941
}
4042
?>
41-
<form class="inputForm" method="post" action="index.php?Page_Type=Edit_API_Authentication_Entity&parentid=<?php echo($params['site']->getId())?>&authentityid=<?php xecho($params['authEnt']->getId())?>" name="addAPIAuthenticationEntity">
43+
<form
44+
class="inputForm"
45+
method="post"
46+
action="<?php
47+
echo "index.php?Page_Type=Edit_API_Authentication_Entity",
48+
"&parentid=",
49+
$params['site']->getId(),
50+
"&authentityid=",
51+
xecho($params['authEnt']->getId());
52+
?>"
53+
name="addAPIAuthenticationEntity"
54+
>
4255
<div style="margin-bottom: 0.5em;">
43-
<span class="input_name">Identifier (e.g. Certificate DN or OIDC Subject)*</span>
44-
<input type="text" value="<?php xecho($params['authEnt']->getIdentifier()) ?>" name="IDENTIFIER" class="input_input_text">
56+
<span class="input_name">
57+
Identifier (e.g. Certificate DN or OIDC Subject)*
58+
</span>
59+
60+
<input
61+
type="text"
62+
value="<?php xecho($params['authEnt']->getIdentifier()); ?>"
63+
name="IDENTIFIER"
64+
class="input_input_text"
65+
>
4566
</div>
67+
4668
<div style="margin-bottom: 0.5em;">
4769
<span class="input_name">Credential type*</span>
70+
4871
<select name="TYPE" class="input_input_text">
49-
<?php foreach($params['authTypes'] as $authType) {?>
50-
<option value="<?php xecho($authType) ?>"<?php if ($params['authEnt']->getType() == $authType) {echo " selected=\"selected\"";} ?>>
72+
<?php foreach ($params['authTypes'] as $authType) { ?>
73+
<option
74+
value="<?php xecho($authType); ?>"
75+
<?php
76+
if ($params['authEnt']->getType() == $authType) {
77+
echo " selected=\"selected\"";
78+
}
79+
?>
80+
>
5181
<?php xecho($authType) ?>
5282
</option>
5383
<?php } ?>
5484
</select>
5585
</div>
86+
5687
<div style="margin-bottom: 1em">
5788
<div class="input_warning">
58-
WARNING: it is possible to delete information using the write functionality of the API. Leave Allow API write unchecked if
89+
WARNING: it is possible to delete information using the write
90+
functionality of the API. Leave Allow API write unchecked if
5991
you do not need to write data.
6092
</div>
93+
6194
<div class="input_checkbox">
62-
<input type="checkbox" name="ALLOW_WRITE" id="ALLOW_WRITE" value="checked"
95+
<input
96+
type="checkbox"
97+
name="ALLOW_WRITE"
98+
id="ALLOW_WRITE"
99+
value="checked"
63100
<?php
64-
if ($params['authEnt']->getAllowAPIWrite()) { echo('checked="checked"');}
101+
if ($params['authEnt']->getAllowAPIWrite()) {
102+
echo('checked="checked"');
103+
}
65104
?>
66105
/>
67-
<label class="input_label" for="ALLOW_WRITE">Allow API write</label>
106+
<label class="input_label" for="ALLOW_WRITE">
107+
Allow API write
108+
</label>
68109
</div>
69110
</div>
111+
70112
<input type="submit" value="Edit credential" class="input_button">
71113
</form>
72114
</div>
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
<div class="rightPageContainer">
22
<h1 class="Success">Success</h1><br />
3-
The API authenication credential has now been updated. Type:<?php xecho($params['apiAuthenticationEntity']->getType()) ?>, identifier: <?php xecho($params['apiAuthenticationEntity']->getIdentifier()) ?>.
3+
The API authenication credential has now been updated.
4+
Type: <?php xecho($params['apiAuthenticationEntity']->getType()); ?>,
5+
identifier: <?php
6+
xecho($params['apiAuthenticationEntity']->getIdentifier());
7+
?>.
48
<br />
5-
<a href="index.php?Page_Type=Site&amp;id=<?php echo $params['site']->getID(); ?>">
6-
View site</a>
9+
<a
10+
href="<?php
11+
echo "index.php?Page_Type=Site&amp;id=", $params['site']->getID();
12+
?>"
13+
>View site</a>
714
</div>

‎htdocs/web_portal/views/site/view_site.php

+698-350
Large diffs are not rendered by default.

‎lib/Gocdb_Services/APIAuthenticationService.php

+103-46
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
use Doctrine\ORM\QueryBuilder;
3232
use org\gocdb\services\Validate;
33+
use Exception;
3334

3435
class APIAuthenticationService extends AbstractEntityService
3536
{
@@ -39,20 +40,24 @@ public function __construct()
3940
}
4041

4142
/**
42-
* Returns the APIAuthentication entity associated with the given identifier.
43+
* Returns the APIAuthentication entity
44+
* associated with the given identifier.
4345
*
4446
* @param string $ident Identifier (e.g. X.509 DN as string)
45-
* @return \APIAuthentication[] APIAuthentication associated with this identifier
47+
*
48+
* @return \APIAuthentication[] APIAuthentication associated
49+
* with this identifier
4650
*/
4751
public function getAPIAuthentication($ident)
4852
{
49-
5053
if (!is_string($ident)) {
51-
throw new \LogicException("Expected string APIAuthentication identifier.");
54+
throw new \LogicException(
55+
"Expected string APIAuthentication identifier."
56+
);
5257
}
5358

54-
$dql = "SELECT a FROM APIAuthentication a " .
55-
"WHERE (a.identifier = :ident)" ;
59+
$dql = "SELECT a FROM APIAuthentication a "
60+
. "WHERE (a.identifier = :ident)" ;
5661

5762
/* @var $qry \Doctine\DBAL\query */
5863
$qry = $this->em->createQuery($dql);
@@ -63,35 +68,40 @@ public function getAPIAuthentication($ident)
6368
return $apiAuths;
6469
}
6570

66-
/**
67-
* Update the fields of an APIAuthentication entity and commit the resulting entity
71+
/**
72+
* Update the fields of an APIAuthentication entity
73+
* and commit the resulting entity
6874
*
6975
* @param \Site Parent site
7076
* @param \User Owning user
7177
* @param array Array containing new values
78+
*
7279
* @throws \Exception on error with commit rolled back
7380
* @return \APIAuthentication
7481
*/
7582
public function addAPIAuthentication(\Site $site, \User $user, $newValues)
7683
{
77-
7884
$identifier = $newValues['IDENTIFIER'];
7985
$type = $newValues['TYPE'];
8086
$allowWrite = $newValues['ALLOW_WRITE'];
8187

82-
//Check that an identifier has been provided
88+
// Check that an identifier has been provided
8389
if (empty($identifier)) {
84-
throw new \Exception("A value must be provided for the identifier");
90+
throw new Exception(
91+
"A value must be provided for the identifier"
92+
);
8593
}
8694

87-
//validate the values against the schema
95+
// validate the values against the schema
8896
$this->validate($newValues, $identifier, $type);
89-
90-
//Check there isn't already a credential with that identifier for that Site
97+
/**
98+
* Check there isn't already a credential
99+
* with that identifier for that Site
100+
*/
91101
$this->uniqueAPIAuthEnt($site, $identifier);
92-
93102
//Add the properties
94103
$this->em->getConnection()->beginTransaction();
104+
95105
try {
96106
$authEnt = new \APIAuthentication();
97107
$authEnt->setIdentifier($identifier);
@@ -108,29 +118,35 @@ public function addAPIAuthentication(\Site $site, \User $user, $newValues)
108118
} catch (\Exception $e) {
109119
$this->em->getConnection()->rollback();
110120
$this->em->close();
121+
111122
throw $e;
112123
}
113124

114125
return $authEnt;
115126
}
116127

117128
/**
118-
* Update the fields of an APIAuthentication entity and commit the resulting entity
129+
* Update the fields of an APIAuthentication entity
130+
* and commit the resulting entity
119131
*
120132
* @param \APIAuthentication Entity to delete
133+
*
121134
* @throws \Exception on error with commit rolled back
122135
*/
123136
public function deleteAPIAuthentication(\APIAuthentication $authEntity)
124137
{
125-
126138
$this->em->getConnection()->beginTransaction();
127139

128140
$parentSite = $authEntity->getParentSite();
129141
$user = $authEntity->getUser();
130142

131143
try {
132-
//Remove the authentication entity from the site then remove the entity
133-
$parentSite->getAPIAuthenticationEntities()->removeElement($authEntity);
144+
/**
145+
* Remove the authentication entity from
146+
* the site then remove the entity
147+
*/
148+
$parentSite->getAPIAuthenticationEntities()
149+
->removeElement($authEntity);
134150
$user->getAPIAuthenticationEntities()->removeElement($authEntity);
135151

136152
$this->em->remove($authEntity);
@@ -143,41 +159,53 @@ public function deleteAPIAuthentication(\APIAuthentication $authEntity)
143159
} catch (\Exception $e) {
144160
$this->em->getConnection()->rollback();
145161
$this->em->close();
162+
146163
throw $e;
147164
}
148165
}
149166

150-
/**
151-
* Update the fields of an APIAuthentication entity and commit the resulting entity
167+
/**
168+
* Update the fields of an APIAuthentication entity
169+
* and commit the resulting entity
152170
*
153171
* @param \APIAuthentication Entity to update
154172
* @param \User Owning user
155173
* @param array Array containing new values
156-
* @throws \Exception on error with commit rolled back
174+
*
175+
* @throws Exception on error with commit rolled back
157176
* @return \APIAuthentication
158177
*/
159-
public function editAPIAuthentication(\APIAuthentication $authEntity, \User $user, $newValues)
160-
{
161-
178+
public function editAPIAuthentication(
179+
\APIAuthentication $authEntity,
180+
\User $user,
181+
$newValues
182+
) {
162183
$identifier = $newValues['IDENTIFIER'];
163184
$type = $newValues['TYPE'];
164185
$allowWrite = $newValues['ALLOW_WRITE'];
165186

166-
//Check that an identifier ha been provided
187+
// Check that an identifier ha been provided
167188
if (empty($identifier)) {
168-
throw new \Exception("A value must be provided for the identifier");
189+
throw new Exception(
190+
"A value must be provided for the identifier"
191+
);
169192
}
170193

171-
//validate the values against the schema
194+
// validate the values against the schema
172195
$this->validate($newValues, $identifier, $type);
173196

174-
//Edit the property
197+
// Edit the property
175198
$this->em->getConnection()->beginTransaction();
199+
176200
try {
177-
// This would probably be the place hook for any future policy acceptance tracking
201+
/**
202+
* This would probably be the place hook
203+
* for any future policy acceptance tracking
204+
*/
178205
if ($user->getId() != $authEntity->getUser()) {
179206
$authEntity->setLastRenewTime();
180207
}
208+
181209
$authEntity->setIdentifier($identifier);
182210
$authEntity->setType($type);
183211
$authEntity->setAllowAPIWrite($allowWrite);
@@ -191,13 +219,16 @@ public function editAPIAuthentication(\APIAuthentication $authEntity, \User $use
191219
} catch (\Exception $e) {
192220
$this->em->getConnection()->rollback();
193221
$this->em->close();
222+
194223
throw $e;
195224
}
196225
}
226+
197227
/**
198228
* Set the last use time field to the current UTC time
199229
*
200230
* @param \APIAuthentication[] $authEntities entity to update
231+
*
201232
* @throws \Exception if the update fails
202233
*/
203234
public function updateLastUseTime(array $authEntities)
@@ -216,9 +247,11 @@ public function updateLastUseTime(array $authEntities)
216247
} catch (\Exception $e) {
217248
$this->em->getConnection()->rollback();
218249
$this->em->close();
250+
219251
throw $e;
220252
}
221253
}
254+
222255
/**
223256
* Fail if there is already an API credential with a given identifier
224257
* for a given Site.
@@ -228,55 +261,79 @@ public function updateLastUseTime(array $authEntities)
228261
*
229262
* @param \Site $site field values for an APIAuthentication object
230263
* @param string $identifier to check
231-
* @throws \Exception if the data can't be validated.
264+
*
265+
* @throws Exception if the data can't be validated.
232266
*/
233267
public function uniqueAPIAuthEnt(\Site $site, $identifier)
234268
{
235-
236269
$authEntities = $this->getAPIAuthentication($identifier);
237270

238271
foreach ($authEntities as $authEnt) {
239272
if ($authEnt->getParentSite()->getId() == $site->getId()) {
240-
throw new \Exception(
241-
"An authentication credential with identifier " .
242-
"\"$identifier\" already exists for " . $site->getName()
273+
throw new Exception(
274+
"An authentication credential with identifier "
275+
. "\"$identifier\" already exists for " . $site->getName()
243276
);
244277
}
245278
}
246279
}
280+
247281
/**
248282
* Validates the user inputted site data against the
249283
* checks in the gocdb_schema.xml and applies additional logic checks
250284
* that can't be described in the gocdb_schema.xml.
251285
*
252286
* @param array $data field values for an APIAuthentication object
253287
* @param mixed $type a valid
254-
* @throws \Exception if the data can't be validated.
288+
*
289+
* @throws Exception if the data can't be validated.
255290
* @return null
256291
*/
257292
private function validate($data, $identifier, $type)
258293
{
259-
260294
$serv = new Validate();
295+
261296
foreach ($data as $field => $value) {
262297
$valid = $serv->validate('APIAUTHENTICATION', $field, $value);
298+
263299
if (!$valid) {
264300
$error = "$field contains an invalid value: $value";
265-
throw new \Exception($error);
301+
302+
throw new Exception($error);
266303
}
267304
}
268-
//If the entity is of type X.509, do a more thorough check than the validate service (as we know the type)
269-
//Note that we are allowing ':' as they can appear in robot DN's
270-
if ($type == 'X.509' && !preg_match("/^(\/[A-Za-z]+=[a-zA-Z0-9\/\-\_\s\.,'@:\/]+)*$/", $identifier)) {
271-
throw new \Exception("Invalid X.509 DN");
305+
306+
/**
307+
* If the entity is of type X.509, do a more thorough
308+
* check than the validate service (as we know the type)
309+
*
310+
* Note that we are allowing ':' as they can appear in robot DN's
311+
*/
312+
if (
313+
$type == 'X.509'
314+
&& !preg_match(
315+
"/^(\/[A-Za-z]+=[a-zA-Z0-9\/\-\_\s\.,'@:\/]+)*$/",
316+
$identifier
317+
)
318+
) {
319+
throw new Exception("Invalid X.509 DN");
272320
}
273321

274-
//If the entity is of type OIDC subject, do a more thorough check again
322+
/**
323+
* If the entity is of type OIDC subject,
324+
* do a more thorough check again
325+
*/
275326
if (
276-
$type == 'OIDC Subject' &&
277-
!preg_match("/^([a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12})$/", $identifier)
327+
$type == 'OIDC Subject'
328+
&& !preg_match(
329+
(
330+
"/^([a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}"
331+
. "\-[a-f0-9]{4}\-[a-f0-9]{12})$/"
332+
),
333+
$identifier
334+
)
278335
) {
279-
throw new \Exception("Invalid OIDC Subject");
336+
throw new Exception("Invalid OIDC Subject");
280337
}
281338
}
282339
}

‎lib/Gocdb_Services/Site.php

+805-394
Large diffs are not rendered by default.

‎resources/ManageAPICredentials/ManageAPICredentialsActions.php

+154-79
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<?php
22

33
/**
4-
* Implement API credential management actions such as listing, sending warning emails and
5-
* deleting credentials.
4+
* Implement API credential management actions such as listing,
5+
* sending warning emails and deleting credentials.
66
*/
77

88
namespace org\gocdb\scripts;
99

10-
require_once dirname(__FILE__) . "/../../lib/Gocdb_Services/APIAuthenticationService.php";
11-
require_once dirname(__FILE__) . "/../../lib/Gocdb_Services/Factory.php";
10+
require_once dirname(__FILE__)
11+
. "/../../lib/Gocdb_Services/APIAuthenticationService.php";
12+
require_once dirname(__FILE__)
13+
. "/../../lib/Gocdb_Services/Factory.php";
1214

1315
use APIAuthentication;
1416
use DateInterval;
@@ -22,32 +24,38 @@ class ManageAPICredentialsActions
2224
private $entityManager;
2325
private $baseTime;
2426

27+
/**
28+
* @param \Doctrine\Orm\EntityManager $entitymanager A valid Doctrine
29+
* Entity Manager
30+
* @param bool $dryRun If true no action is taken and
31+
* a report is generated instead
32+
* @param \DateTime $baseTime Time from which interval
33+
* of no-use is measured
34+
*/
2535
public function __construct($dryRun, $entityManager, $baseTime)
2636
{
27-
/**
28-
* @param \Doctrine\Orm\EntityManager $entitymanager A valid Doctrine Entity Manager
29-
* @param bool $dryRun If true no action is taken and a report is generated instead
30-
* @param \DateTime $baseTime Time from which interval of no-use is measured
31-
**/
3237
$this->dryRun = $dryRun;
3338
$this->entityManager = $entityManager;
3439
$this->baseTime = $baseTime;
3540
}
41+
3642
/**
3743
* Find API credentials unused for a number of months.
3844
*
39-
* Find API credentials which have not been used for a number of months prior to a given base time based
40-
* on the credential property lastUseTime.
45+
* Find API credentials which have not been used for a number of months
46+
* prior to a given base time based on the credential property lastUseTime.
4147
*
42-
* @param int $threshold The number of months of no-use prior to $baseTime to use for selection
48+
* @param int $threshold The number of months of no-use
49+
* prior to $baseTime to use for selection
4350
*/
4451
public function getCreds($threshold, $propertyName)
4552
{
4653
$qbl = $this->entityManager->createQueryBuilder();
4754

55+
// `$qbl->expr()->isNotNull("cred.user")` => cope with legacy entities
4856
$qbl->select('cred')
4957
->from('APIAuthentication', 'cred')
50-
->where($qbl->expr()->isNotNull("cred.user")) // cope with legacy entities
58+
->where($qbl->expr()->isNotNull("cred.user"))
5159
->andWhere('cred.' . $propertyName . '< :threshold');
5260

5361
$timeThresh = clone $this->baseTime;
@@ -62,18 +70,23 @@ public function getCreds($threshold, $propertyName)
6270

6371
return $creds;
6472
}
73+
6574
/**
6675
* Select API credentials for deletion.
6776
*
68-
* Find API credentials which have not been used for a given number of months
69-
* and delete them or, if dry-run option is true, generate a summary report
70-
* of the credentils found.
77+
* Find API credentials which have not been used for a given
78+
* number of months and delete them or, if dry-run option is true,
79+
* generate a summary report of the credentils found.
80+
*
81+
* @param array $creds Array of credentials to process.
82+
* @param \Doctrine\Orm\EntityManager $entitymanager A valid Doctrine
83+
* Entity Manager
84+
* @param \DateTime $baseTime Time from which interval of
85+
* no-use is measured
86+
* @param int $deleteThreshold The number of months of no-use
87+
* which will trigger deletion
7188
*
72-
* @param array $creds Array of credentials to process.
73-
* @param \Doctrine\Orm\EntityManager $entitymanager A valid Doctrine Entity Manager
74-
* @param \DateTime $baseTime Time from which interval of no-use is measured
75-
* @param int $deleteThreshold The number of months of no-use which will trigger deletion
76-
* @return array Credentials which were not deleted.
89+
* @return array Credentials which were not deleted.
7790
*/
7891
public function deleteCreds($creds, $deleteThreshold)
7992
{
@@ -82,36 +95,60 @@ public function deleteCreds($creds, $deleteThreshold)
8295
$serv = new APIAuthenticationService();
8396
$serv->setEntityManager($this->entityManager);
8497

85-
/* @var $apiCred APIAuthentication */
8698
foreach ($creds as $apiCred) {
87-
if ($this->isOverThreshold($apiCred, $this->baseTime, $deleteThreshold)) {
99+
if (
100+
$this->isOverThreshold(
101+
$apiCred,
102+
$this->baseTime,
103+
$deleteThreshold
104+
)
105+
) {
88106
$deletedCreds[] = $apiCred;
107+
89108
if (!$this->dryRun) {
90109
$serv->deleteAPIAuthentication($apiCred);
91110
}
92111
}
93112
}
113+
94114
if ($this->dryRun) {
95115
$this->reportDryRun($deletedCreds, "deleting");
96116
}
97117

98-
return array_udiff($creds, $deletedCreds, array($this, 'compareCredIds'));
118+
return array_udiff(
119+
$creds,
120+
$deletedCreds,
121+
array(
122+
$this,
123+
'compareCredIds'
124+
)
125+
);
99126
}
127+
100128
/**
101-
* Send of warning emails where credentials have not been used for a given number of months
129+
* Send of warning emails where credentials have not
130+
* been used for a given number of months
131+
*
132+
* Find API credentials from the input array which have not been used for
133+
* a given number of months and send emails to the owners and site address,
134+
* taken from the credential object, warning of impending deletion if the
135+
* period of no-use reaches a given threshold.
136+
* If dry-run option is true, generate a summary report of the
137+
* credentials found instead of sending emails.
102138
*
103-
* Find API credentials from the input array which have not been used for a given number of months
104-
* and send emails to the owners and site address, taken from the credential object,
105-
* warning of impending deletion if the period of no-use reaches a given threshold.
106-
* If dry-run option is true, generate a summary report of the credentials found
107-
* instead of sending emails.
139+
* @param array $creds Array of credentials
140+
* to process.
141+
* @param int $warningThreshold The number of months of no-use
142+
* which triggers warning emails
143+
* @param int $deleteThreshold The number of months of no-use
144+
* which will trigger deletion
145+
* @param string $fromEmail Email address to use
146+
* as sender's (From:) address
147+
* @param string $replyToEmail Email address for
148+
* replies (Reply-To:)
108149
*
109-
* @param array $creds Array of credentials to process.
110-
* @param int $warningThreshold The number of months of no-use which triggers warning emails
111-
* @param int $deleteThreshold The number of months of no-use which will trigger deletion
112-
* @param string $fromEmail Email address to use as sender's (From:) address
113-
* @param string $replyToEmail Email address for replies (Reply-To:)
114-
* @return array Array of credentials identifed for sending warning emails
150+
* @return array Array of credentials identifed
151+
* for sending warning emails
115152
*/
116153
public function warnUsers(
117154
$creds,
@@ -122,13 +159,21 @@ public function warnUsers(
122159
) {
123160
$warnedCreds = [];
124161

125-
/* @var $api APIAuthentication */
126162
foreach ($creds as $apiCred) {
127-
// The credentials list is pre-selected based on the given threshold in the query
128-
// so this check is probably redundant.
129-
if ($this->isOverThreshold($apiCred, $this->baseTime, $warningThreshold)) {
163+
/**
164+
* The credentials list is pre-selected based on the given
165+
* threshold in the query so this check is probably redundant.
166+
*/
167+
if (
168+
$this->isOverThreshold(
169+
$apiCred,
170+
$this->baseTime,
171+
$warningThreshold
172+
)
173+
) {
130174
$lastUsed = $apiCred->getLastUseTime();
131-
$lastUseMonths = $this->baseTime->diff($lastUsed)->format('%m');
175+
$lastUseMonths = $this->baseTime->diff($lastUsed)
176+
->format('%m');
132177

133178
if (!$this->dryRun) {
134179
$this->sendWarningEmail(
@@ -148,28 +193,43 @@ public function warnUsers(
148193
$this->reportDryRun($warnedCreds, "sending warning emails");
149194
}
150195

151-
return array_udiff($creds, $warnedCreds, array($this, 'compareCredIds'));
196+
return array_udiff(
197+
$creds,
198+
$warnedCreds,
199+
array(
200+
$this,
201+
'compareCredIds'
202+
)
203+
);
152204
}
153-
/**
154-
* @return boolean true if the credential has not been used within $threshold months, else false
155-
*/
156-
private function isOverThreshold(APIAuthentication $cred, DateTime $baseTime, $threshold)
157-
{
205+
206+
/**
207+
* @return boolean true if the credential has not been
208+
* used within $threshold months, else false
209+
*/
210+
private function isOverThreshold(
211+
APIAuthentication $cred,
212+
DateTime $baseTime,
213+
$threshold
214+
) {
158215
$lastUsed = $cred->getLastUseTime();
159216

160217
$diffTime = $baseTime->diff($lastUsed);
161218
$lastUseMonths = ($diffTime->y * 12) + $diffTime->m;
162219

163220
return $lastUseMonths >= $threshold;
164221
}
165-
/**
166-
* Helper function to check if two API credentials have the same id.
167-
*
168-
* @return integer zero if equal, -1 if id1 < id2, 1 if id1 > id2
169-
*
170-
*/
171-
private function compareCredIds(APIAuthentication $cred1, APIAuthentication $cred2)
172-
{
222+
223+
/**
224+
* Helper function to check if two API credentials have the same id.
225+
*
226+
* @return integer zero if equal, -1 if id1 < id2, 1 if id1 > id2
227+
*
228+
*/
229+
private function compareCredIds(
230+
APIAuthentication $cred1,
231+
APIAuthentication $cred2
232+
) {
173233
$id1 = $cred1->getId();
174234
$id2 = $cred2->getId();
175235

@@ -180,19 +240,26 @@ private function compareCredIds(APIAuthentication $cred1, APIAuthentication $cre
180240
return $id1 > $id2 ? 1 : -1;
181241
}
182242

183-
/**
184-
* Format and send warning emails.
185-
*
186-
* Send emails to API credential owner and the registered site address warning of impending credential deletion
187-
* if the credential remains unused until a given threshold of months.
188-
*
189-
* @param string $fromEmail Email address to use as sender's (From:) address
190-
* @param string $replyToEmail Email address for replies (Reply-To:)
191-
* @param \APIAuthentication $api Credential to warn about
192-
* @param int $elapsedMonths The number of months of non-use so far.
193-
* @param int $deleteionThreshold The number of months of no-use which will trigger deletion if reached.
194-
* @return void
195-
*/
243+
/**
244+
* Format and send warning emails.
245+
*
246+
* Send emails to API credential owner and the registered site address
247+
* warning of impending credential deletion if the credential remains
248+
* unused until a given threshold of months.
249+
*
250+
* @param string $fromEmail Email address to use as sender's
251+
* (From:) address
252+
* @param string $replyToEmail Email address for replies
253+
* (Reply-To:)
254+
* @param \APIAuthentication $api Credential to warn about
255+
* @param int $elapsedMonths The number of months of
256+
* non-use so far.
257+
* @param int $deleteionThreshold The number of months of no-use
258+
* which will trigger deletion
259+
* if reached.
260+
*
261+
* @return void
262+
*/
196263
private function sendWarningEmail(
197264
$fromEmail,
198265
$replyToEmail,
@@ -207,9 +274,11 @@ private function sendWarningEmail(
207274

208275
$headersArray = array ("From: $fromEmail",
209276
"Cc: $siteEmail");
277+
210278
if (strlen($replyToEmail) > 0 && $fromEmail !== $replyToEmail) {
211279
$headersArray[] = "Reply-To: $replyToEmail";
212280
}
281+
213282
$headers = join("\r\n", $headersArray);
214283

215284
$subject = "GOCDB: Site API credential deletion notice";
@@ -230,21 +299,27 @@ private function sendWarningEmail(
230299
// Send the email (or not, according to local configuration)
231300
Factory::getEmailService()->send($userEmail, $subject, $body, $headers);
232301
}
233-
/**
234-
* Generate a summary report.
235-
*
236-
* Generate a report to stdout summarising information about each credential in an array when
237-
* a dry-run operation is in progress.
238-
*
239-
* @param array $creds Array of API credential objects to be summarised.
240-
* @param string $text Brief description of the operation which would have been
241-
* performed without dry-run to be included in the report.
242-
* @return void
243-
*/
302+
303+
/**
304+
* Generate a summary report.
305+
*
306+
* Generate a report to stdout summarising information about each
307+
* credential in an array when a dry-run operation is in progress.
308+
*
309+
* @param array $creds Array of API credential objects
310+
* to be summarised.
311+
* @param string $text Brief description of the operation
312+
* which would have been
313+
* performed without dry-run to be
314+
* included in the report.
315+
*
316+
* @return void
317+
*/
244318
private function reportDryRun(array $creds, $text)
245319
{
246320
if (count($creds) == 0) {
247321
print("Dry run: No matching credentials found for $text.\n");
322+
248323
return;
249324
}
250325

‎resources/ManageAPICredentials/ManageUnusedAPICredentials.php

+18-12
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@
33
/**
44
* Maintenance utility for API credential management.
55
*
6-
* Finds API credentials that have not been used for a given number of months and
7-
* deletes them, then finds credentials that have not been used for a different,
8-
* smaller number of months and sends warning email messages to the owners telling
9-
* them the credentials will be deleted if not used before the larger month
10-
* deadline is reached. A dry-run option is provided to report what would have been
11-
* done but without taking any actions.
6+
* Finds API credentials that have not been used for a given
7+
* number of months and deletes them, then finds credentials that have not
8+
* been used for a different, smaller number of months and sends warning email
9+
* messages to the owners telling them the credentials will be deleted if not
10+
* used before the larger month deadline is reached.
11+
* A dry-run option is provided to report what would have been done but without
12+
* taking any actions.
1213
*
13-
* Sender and reply-to email addresses are taken from the local_info.xml config file.
14+
* Sender and reply-to email addresses are taken from
15+
* the local_info.xml config file.
1416
*
1517
* Usage: php ManageUnusedAPICredentials.php [ --help | -h ] [ --dry-run ] \\
1618
* [[ --warning_threshold | -w ] LITTLE_MONTHS ] \\
1719
* [[ --deletion_threshold | -d ] BIG_MONTHS ]
1820
*
19-
* Warning and deletion thresholds are optional. If missing the respective action is not taken.
20-
* If both are specified, the deletion threshold MUST be greater than the warning threshold.
21+
* Warning and deletion thresholds are optional.
22+
* If missing the respective action is not taken.
23+
* If both are specified, the deletion threshold MUST be
24+
* greater than the warning threshold.
2125
*/
2226

2327
namespace org\gocdb\scripts;
@@ -34,8 +38,6 @@
3438

3539
// Fetch sender and replyTo email addresses from the local config file.
3640
// There is no capability to override based on URL as the webserver does with -
37-
// \Factory::getConfigService()->setLocalInfoOverride($_SERVER['SERVER_NAME']);
38-
3941
$configService = \Factory::getConfigService();
4042
$fromEmail = (string) $configService->getEmailFrom();
4143
$replyToEmail = (string) $configService->getEmailTo();
@@ -49,7 +51,11 @@
4951

5052
$baseTime = new DateTime("now", new DateTimeZone('UTC'));
5153

52-
$actions = new ManageAPICredentialsActions($options->isDryRun(), $entityManager, $baseTime);
54+
$actions = new ManageAPICredentialsActions(
55+
$options->isDryRun(),
56+
$entityManager,
57+
$baseTime
58+
);
5359

5460
$creds = $actions->getCreds($options->getThreshold(), 'lastUseTime');
5561

‎resources/ManageAPICredentials/ManageUnusedAPICredentialsOptions.php

+32-9
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ public function __construct()
2121
{
2222
$this->getOptions();
2323
}
24+
2425
/**
25-
* @throws \InvInvalidArgumentException If errors found in argument processing
26+
* @throws \InvalidArgumentException If errors found in
27+
* argument processing
2628
*/
2729
public function getOptions()
2830
{
@@ -35,11 +37,16 @@ public function getOptions()
3537
'deletion_threshold:'
3638
];
3739

38-
// Beware that getopt is not clever at spotting invalid/misspelled arguments
40+
/**
41+
* Beware that getopt is not clever at
42+
* spotting invalid/misspelled arguments
43+
*/
3944
$given = getopt($shortOptions, $longOptions);
4045

4146
if ($given === false) {
42-
throw new InvalidArgumentException('failed to parse command line arguments');
47+
throw new InvalidArgumentException(
48+
'failed to parse command line arguments'
49+
);
4350
}
4451

4552
if ($this->getBoolOption($given, 'help', 'h')) {
@@ -60,37 +67,45 @@ public function getOptions()
6067
);
6168
}
6269
}
70+
6371
return;
6472
}
73+
6574
private function getValOption($given, $long, $short)
6675
{
6776
if (isset($given[$long]) || isset($given[$short])) {
68-
$tValGiven = isset($given[$short]) ? $given[$short] : $given[$long];
77+
$tValGiven = isset($given[$short])
78+
? $given[$short]
79+
: $given[$long];
80+
6981
return $this->positiveInteger($tValGiven, $long);
7082
}
83+
7184
return;
7285
}
86+
7387
private function getBoolOption($given, $long, $short)
7488
{
7589
return isset($given[$long]) || isset($given[$short]);
7690
}
91+
7792
private function positiveInteger($val, $txt)
7893
{
7994
if ((string)abs((int)$val) != $val) {
8095
throw new InvalidArgumentException(
8196
"$txt must be integer and greater than zero . Received: $val"
8297
);
8398
}
99+
84100
return (int)$val;
85101
}
102+
86103
public static function usage($message = '')
87104
{
88105
if ($message != '') {
89-
print
90-
(
91-
"Error: $message\n"
92-
);
106+
print("Error: $message\n");
93107
}
108+
94109
print
95110
(
96111
"Usage: php ManageAPICredentials.php [--help | -h] [--dry-run] \\\ \n" .
@@ -105,32 +120,40 @@ public static function usage($message = '')
105120
" for MONTHS months.\n"
106121
);
107122
}
123+
108124
public function isShowHelp()
109125
{
110126
return $this->showHelp;
111127
}
128+
112129
public function isDryRun()
113130
{
114131
return $this->dryRun;
115132
}
133+
116134
public function isDeleteEnabled()
117135
{
118136
return !is_null($this->getDelete());
119137
}
138+
120139
public function isWarnEnabled()
121140
{
122141
return !is_null($this->getWarn());
123142
}
143+
124144
public function getWarn()
125145
{
126146
return $this->warn;
127147
}
148+
128149
public function getDelete()
129150
{
130151
return $this->delete;
131152
}
153+
132154
/**
133-
* The delete threshold may not be given in which case the warning threshold should be used.
155+
* The delete threshold may not be given in which case
156+
* the warning threshold should be used.
134157
* Note that it is an error is delete is greater than warning.
135158
*/
136159
public function getThreshold()

‎tests/resourcesTests/ManageUnrenewedAPICredentialsTest.php

+46-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
require_once __DIR__ . '/ManageAPICredentialsTestUtils.php';
2323
require_once __DIR__ . '/../unit/lib/Gocdb_Services/ServiceTestUtil.php';
24-
require_once __DIR__ . '/../../resources/ManageAPICredentials/ManageAPICredentialsActions.php';
24+
require_once __DIR__
25+
. '/../../resources/ManageAPICredentials/ManageAPICredentialsActions.php';
2526

2627
class ManageUnrenewedAPICredentialsTest extends PHPUnit_Extensions_Database_TestCase
2728
{
@@ -31,10 +32,13 @@ class ManageUnrenewedAPICredentialsTest extends PHPUnit_Extensions_Database_Test
3132
public function __construct()
3233
{
3334
parent::__construct();
34-
// Use a local instance to avoid Mess Detector's whinging about avoiding
35-
// static access.
35+
/**
36+
* Use a local instance to avoid Mess Detector's
37+
* whinging about avoiding static access.
38+
*/
3639
$this->dbOpsFactory = new PHPUnit_Extensions_Database_Operation_Factory();
3740
}
41+
3842
/**
3943
* Overridden.
4044
*/
@@ -44,27 +48,40 @@ public static function setUpBeforeClass()
4448
echo "\n\n-------------------------------------------------\n";
4549
echo "Executing ManageUnrenewedAPICredentialsTest. . .\n";
4650
}
51+
4752
/**
4853
* Overridden. Returns the test database connection.
54+
*
4955
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
5056
*/
5157
protected function getConnection()
5258
{
5359
require_once __DIR__ . '/../doctrine/bootstrap_pdo.php';
60+
5461
return getConnectionToTestDB();
5562
}
63+
5664
/**
5765
* Overridden. Returns the test dataset.
58-
* Defines how the initial state of the database should look before each test is executed.
66+
* Defines how the initial state of the database
67+
* should look before each test is executed.
68+
*
5969
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
6070
*/
6171
protected function getDataSet()
6272
{
63-
$dataset = $this->createFlatXMLDataSet(__DIR__ . '/../doctrine/truncateDataTables.xml');
73+
$dataset = $this->createFlatXMLDataSet(
74+
__DIR__ . '/../doctrine/truncateDataTables.xml'
75+
);
76+
6477
return $dataset;
65-
// Use below to return an empty data set if we don't want to truncate and seed
66-
//return new PHPUnit_Extensions_Database_DataSet_DefaultDataSet();
78+
/**
79+
* Use below to return an empty data set
80+
* if we don't want to truncate and seed.
81+
*/
82+
// return new PHPUnit_Extensions_Database_DataSet_DefaultDataSet();
6783
}
84+
6885
/**
6986
* Overridden.
7087
*/
@@ -75,11 +92,16 @@ protected function getSetUpOperation()
7592
//return PHPUnit_Extensions_Database_Operation_Factory::UPDATE();
7693
//return PHPUnit_Extensions_Database_Operation_Factory::NONE();
7794
//
78-
// Issue a DELETE from <table> which is more portable than a
79-
// TRUNCATE table <table> (some DBs require high privileges for truncate statements
80-
// and also do not allow truncates across tables with FK contstraints e.g. Oracle)
95+
/**
96+
* Issue a DELETE from <table> which is more portable than a
97+
* TRUNCATE table <table> (some DBs require high privileges
98+
* for truncate statements and also do not allow truncates across
99+
* tables with FK contstraints e.g. Oracle)
100+
*/
101+
81102
return $this->dbOpsFactory->DELETE_ALL();
82103
}
104+
83105
/**
84106
* Overridden.
85107
*/
@@ -88,6 +110,7 @@ protected function getTearDownOperation()
88110
// NONE is default
89111
return $this->dbOpsFactory->NONE();
90112
}
113+
91114
/**
92115
* Sets up the fixture, e.g create a new entityManager for each test run
93116
* This method is called before each test method is executed.
@@ -102,25 +125,32 @@ protected function setUp()
102125

103126
date_default_timezone_set("UTC");
104127
}
128+
105129
/**
106130
* Run after each test function to prevent pile-up of database connections.
107131
*/
108132
protected function tearDown()
109133
{
110134
parent::tearDown();
135+
111136
if (!is_null($this->entityManager)) {
112137
$this->entityManager->getConnection()->close();
113138
}
114139
}
140+
115141
/**
116142
* @return EntityManager
117143
*/
118144
private function createEntityManager()
119145
{
120-
$entityManager = null; // Initialise in local scope to avoid unused variable warnings
146+
// Initialise in local scope to avoid unused variable warnings
147+
$entityManager = null;
148+
121149
require __DIR__ . '/../doctrine/bootstrap_doctrine.php';
150+
122151
return $entityManager;
123152
}
153+
124154
public function testLastRenewTime()
125155
{
126156
print __METHOD__ . "\n";
@@ -132,7 +162,11 @@ public function testLastRenewTime()
132162

133163
$entityManager = $this->createEntityManager();
134164

135-
$actions = new ManageAPICredentialsActions(false, $entityManager, $baseTime);
165+
$actions = new ManageAPICredentialsActions(
166+
false,
167+
$entityManager,
168+
$baseTime
169+
);
136170

137171
// Fetch credentials not renewed in the last 5 months - should be 6.
138172
$creds = $actions->getCreds(5, 'lastRenewTime');

‎tests/resourcesTests/ManageUnusedAPICredentialsTest.php

+52-16
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121

2222
require_once __DIR__ . '/ManageAPICredentialsTestUtils.php';
2323
require_once __DIR__ . '/../unit/lib/Gocdb_Services/ServiceTestUtil.php';
24-
require_once __DIR__ . '/../../resources/ManageAPICredentials/ManageAPICredentialsActions.php';
24+
require_once __DIR__
25+
. '/../../resources/ManageAPICredentials/ManageAPICredentialsActions.php';
2526

2627
class ManageUnusedAPICredentialsTest extends PHPUnit_Extensions_Database_TestCase
2728
{
@@ -31,10 +32,14 @@ class ManageUnusedAPICredentialsTest extends PHPUnit_Extensions_Database_TestCas
3132
public function __construct()
3233
{
3334
parent::__construct();
34-
// Use a local instance to avoid Mess Detector's whinging about avoiding
35-
// static access.
36-
$this->dbOpsFactory = new PHPUnit_Extensions_Database_Operation_Factory();
35+
/**
36+
* Use a local instance to avoid Mess Detector's
37+
* whinging about avoiding static access.
38+
*/
39+
$this->dbOpsFactory =
40+
new PHPUnit_Extensions_Database_Operation_Factory();
3741
}
42+
3843
/**
3944
* Overridden.
4045
*/
@@ -44,27 +49,39 @@ public static function setUpBeforeClass()
4449
echo "\n\n-------------------------------------------------\n";
4550
echo "Executing ManageUnusedAPICredentialsTest. . .\n";
4651
}
52+
4753
/**
4854
* Overridden. Returns the test database connection.
4955
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
5056
*/
5157
protected function getConnection()
5258
{
5359
require_once __DIR__ . '/../doctrine/bootstrap_pdo.php';
60+
5461
return getConnectionToTestDB();
5562
}
63+
5664
/**
5765
* Overridden. Returns the test dataset.
58-
* Defines how the initial state of the database should look before each test is executed.
66+
* Defines how the initial state of the database should
67+
* look before each test is executed.
68+
*
5969
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
6070
*/
6171
protected function getDataSet()
6272
{
63-
$dataset = $this->createFlatXMLDataSet(__DIR__ . '/../doctrine/truncateDataTables.xml');
73+
$dataset = $this->createFlatXMLDataSet(
74+
__DIR__ . '/../doctrine/truncateDataTables.xml'
75+
);
76+
6477
return $dataset;
65-
// Use below to return an empty data set if we don't want to truncate and seed
78+
/**
79+
* Use below to return an empty data set
80+
* if we don't want to truncate and seed
81+
*/
6682
//return new PHPUnit_Extensions_Database_DataSet_DefaultDataSet();
6783
}
84+
6885
/**
6986
* Overridden.
7087
*/
@@ -75,19 +92,24 @@ protected function getSetUpOperation()
7592
//return PHPUnit_Extensions_Database_Operation_Factory::UPDATE();
7693
//return PHPUnit_Extensions_Database_Operation_Factory::NONE();
7794
//
78-
// Issue a DELETE from <table> which is more portable than a
79-
// TRUNCATE table <table> (some DBs require high privileges for truncate statements
80-
// and also do not allow truncates across tables with FK contstraints e.g. Oracle)
95+
/**
96+
* Issue a DELETE from <table> which is more portable than a
97+
* TRUNCATE table <table> (some DBs require high privileges
98+
* for truncate statements and also do not allow truncates
99+
* across tables with FK contstraints e.g. Oracle)
100+
*/
81101
return $this->dbOpsFactory->DELETE_ALL();
82102
}
103+
83104
/**
84105
* Overridden.
85106
*/
86107
protected function getTearDownOperation()
87108
{
88-
// NONE is default
109+
// NONE is default
89110
return $this->dbOpsFactory->NONE();
90111
}
112+
91113
/**
92114
* Sets up the fixture, e.g create a new entityManager for each test run
93115
* This method is called before each test method is executed.
@@ -96,31 +118,38 @@ protected function setUp()
96118
{
97119
parent::setUp();
98120
$this->entityManager = $this->createEntityManager();
99-
// Pass the Entity Manager into the Factory to allow Gocdb_Services
100-
// to use other Gocdb_Services.
121+
// Pass the Entity Manager into the Factory to allow Gocdb_Services
122+
// to use other Gocdb_Services.
101123
\Factory::setEntityManager($this->entityManager);
102124

103125
date_default_timezone_set("UTC");
104126
}
127+
105128
/**
106129
* Run after each test function to prevent pile-up of database connections.
107130
*/
108131
protected function tearDown()
109132
{
110133
parent::tearDown();
134+
111135
if (!is_null($this->entityManager)) {
112136
$this->entityManager->getConnection()->close();
113137
}
114138
}
139+
115140
/**
116141
* @return EntityManager
117142
*/
118143
private function createEntityManager()
119144
{
120-
$entityManager = null; // Initialise in local scope to avoid unused variable warnings
145+
// Initialise in local scope to avoid unused variable warnings
146+
$entityManager = null;
147+
121148
require __DIR__ . '/../doctrine/bootstrap_doctrine.php';
149+
122150
return $entityManager;
123151
}
152+
124153
public function testLastUseTime()
125154
{
126155
print __METHOD__ . "\n";
@@ -130,7 +159,11 @@ public function testLastUseTime()
130159

131160
$entityManager = $this->createEntityManager();
132161

133-
$actions = new ManageAPICredentialsActions(false, $entityManager, $baseTime);
162+
$actions = new ManageAPICredentialsActions(
163+
false,
164+
$entityManager,
165+
$baseTime
166+
);
134167

135168
// Fetch credentials not used in the last 7 months - should be 2
136169
$creds = $actions->getCreds(7, 'lastUseTime');
@@ -158,7 +191,10 @@ public function testLastUseTime()
158191
$this->assertCount(
159192
1,
160193
$creds,
161-
'Unexpected credential count following deletion based on last use time.'
194+
(
195+
"Unexpected credential count following "
196+
. "deletion based on last use time."
197+
)
162198
);
163199
}
164200
}

0 commit comments

Comments
 (0)
Please sign in to comment.