Skip to content

Commit

Permalink
#211 - reduced the amount of code in the TagController::doDELETE() me…
Browse files Browse the repository at this point in the history
…thod to reduce duplication
  • Loading branch information
alphadevx committed Sep 15, 2015
1 parent 4567932 commit b71b026
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 73 deletions.
2 changes: 0 additions & 2 deletions Alpha/Controller/ActiveRecordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,6 @@ public function doPUT($request)
* @return Alpha\Util\Http\Response
*
* @since 2.0
*
* @todo implement
*/
public function doDELETE($request)
{
Expand Down
75 changes: 6 additions & 69 deletions Alpha/Controller/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function doGET($request)
label: 'Okay',
cssClass: 'btn btn-default btn-xs',
action: function(dialogItself) {
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('deleteOID')) : 'deleteOID')."\"]').attr('value', '".$tag->getID()."');
$('[id=\"".($config->get('security.encrypt.http.fieldnames') ? base64_encode(SecurityUtils::encrypt('ActiveRecordOID')) : 'ActiveRecordOID')."\"]').attr('value', '".$tag->getID()."');
$('#deleteForm').submit();
dialogItself.close();
}
Expand Down Expand Up @@ -416,78 +416,15 @@ public function doDELETE($request)
{
self::$logger->debug('>>doDELETE($request=['.var_export($request, true).'])');

$params = $request->getParams();

try {
// check the hidden security fields before accepting the form POST data
if (!$this->checkSecurityFields()) {
throw new SecurityException('This page cannot accept post data from remote servers!');
}

// ensure that a bo is provided
if (isset($params['ActiveRecordType'])) {
$ActiveRecordType = urldecode($params['ActiveRecordType']);
} else {
throw new IllegalArguementException('Could not load the tag objects as an ActiveRecordType was not supplied!');
}

// ensure that a OID is provided
if (isset($params['ActiveRecordOID'])) {
$ActiveRecordOID = $params['ActiveRecordOID'];
} else {
throw new IllegalArguementException('Could not load the tag objects as an ActiveRecordOID was not supplied!');
}

if (class_exists($ActiveRecordType)) {
$record = new $ActiveRecordType();
} else {
throw new IllegalArguementException('No ActiveRecord available to display tags for!');
}

if (!empty($params['deleteOID'])) {
try {
$record = new $ActiveRecordType();
$record->load($ActiveRecordOID);

$tag = new Tag();
$tag->load($params['deleteOID']);
$content = $tag->get('content');

ActiveRecord::begin();

$tag->delete();

self::$logger->action('Deleted tag '.$content.' on '.$ActiveRecordType.' instance with OID '.$ActiveRecordOID);

ActiveRecord::commit();

$this->setStatusMessage(View::displayUpdateMessage('Tag <em>'.$content.'</em> on '.get_class($record).' '.$record->getID().' deleted successfully.'));

return $this->doGET($request);
} catch (AlphaException $e) {
self::$logger->error('Unable to delete the tag of id ['.$params['deleteOID'].'], error was ['.$e->getMessage().']');
ActiveRecord::rollback();

$this->setStatusMessage(View::displayErrorMessage('Tag <em>'.$content.'</em> on '.get_class($record).' '.$record->getID().' not deleted, please check the application logs.'));

return $this->doGET($request);
}
$config = ConfigProvider::getInstance();

ActiveRecord::disconnect();
}
} catch (SecurityException $e) {
$this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
$this->setName($config->get('app.url').$this->request->getURI());
$this->setUnitOfWork(array($config->get('app.url').$this->request->getURI(), $config->get('app.url').$this->request->getURI()));

self::$logger->warn($e->getMessage());
} catch (IllegalArguementException $e) {
self::$logger->error($e->getMessage());
} catch (RecordNotFoundException $e) {
self::$logger->warn($e->getMessage());

$this->setStatusMessage(View::displayErrorMessage('Failed to load the requested item from the database!'));
}
$request->addParams(array('ActiveRecordType' => 'Alpha\Model\Tag'));

self::$logger->debug('<<doDELETE');
return parent::doDELETE($request);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions test/Alpha/Test/Controller/TagControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,14 @@ public function testDoDELETE()

$this->assertTrue($found, 'Checking that the new tag added was actually saved');

$params = array('deleteOID' => $tagOID, 'var1' => $securityParams[0], 'var2' => $securityParams[1]);
$params = array('ActiveRecordOID' => $tagOID, 'var1' => $securityParams[0], 'var2' => $securityParams[1]);

$request = new Request(array('method' => 'DELETE', 'URI' => '/tag/'.urlencode('Alpha\Model\Article').'/'.$article->getOID(), 'params' => $params));

$response = $front->process($request);

$this->assertEquals(200, $response->getStatus(), 'Testing the doDELETE method');
$this->assertEquals(301, $response->getStatus(), 'Testing the doDELETE method');
$this->assertTrue(strpos($response->getHeader('Location'), '/tag/'.urlencode('Alpha\Model\Article').'/'.$article->getOID()) !== false, 'Testing the doDELETE method');

$tags = $article->getPropObject('tags')->getRelatedObjects();

Expand Down

0 comments on commit b71b026

Please sign in to comment.