Skip to content

Commit

Permalink
DRY out JSON encoding code in repositories
Browse files Browse the repository at this point in the history
Summary:
- Create JsonTranscodeRepository trait and use it
- Fix up layers test: don't expect an array unless JSON value is populated

Test Plan: bin/behat

Reviewers: lkamau, will, jasonmule

Reviewed By: jasonmule

Subscribers: jasonmule, will, rjmackay, lkamau

Differential Revision: https://phabricator.ushahidi.com/D927
  • Loading branch information
rjmackay committed Aug 4, 2015
1 parent d05f721 commit d17fd43
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 108 deletions.
18 changes: 7 additions & 11 deletions application/classes/Ushahidi/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,25 +270,21 @@ public static function init()
$di->setter['Ushahidi_Repository_User']['setHasher'] = $di->lazyGet('tool.hasher.password');

// Repository parameters
foreach ([
'form_attribute',
'post',
'layer',
'tag',
'set',
] as $name)
{
$di->setter['Ushahidi_Repository_' . Text::ucfirst($name, '_')]['setTranscoder'] =
$di->lazyGet('tool.jsontranscode');
}

// Abstract repository parameters
$di->params['Ushahidi_Repository'] = [
'db' => $di->lazyGet('kohana.db'),
];

// Set up Json Transcode Repository Trait
$di->setter['Ushahidi_JsonTranscodeRepository']['setTranscoder'] = $di->lazyGet('tool.jsontranscode');

// Media repository parameters
$di->params['Ushahidi_Repository_Media'] = [
'upload' => $di->lazyGet('tool.uploader'),
];

// Post repository parameters
$di->params['Ushahidi_Repository_Post'] = [
'form_attribute_repo' => $di->lazyGet('repository.form_attribute'),
'form_stage_repo' => $di->lazyGet('repository.form_stage'),
Expand Down
56 changes: 56 additions & 0 deletions application/classes/Ushahidi/JsonTranscodeRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php defined('SYSPATH') OR die('No direct script access.');

/**
* Ushahidi Json Transcoding Repo Trait
*
* JSON encodes properties defined `json_properties`
* during `executeUpdate` and `executeInsert`
*
* @author Ushahidi Team <[email protected]>
* @package Ushahidi\Application\Controllers
* @copyright 2013 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/

use Ushahidi\Core\Tool\JsonTranscode;

trait Ushahidi_JsonTranscodeRepository {

protected $json_transcoder;

/**
* Return an array of properties to be json encoded
* @return Array
*/
abstract protected function getJsonProperties();

public function setTranscoder(JsonTranscode $transcoder)
{
$this->json_transcoder = $transcoder;
}

// Ushahidi_Repository
public function executeInsert(Array $input)
{
// JSON Encode defined properties
$input = array_filter($this->json_transcoder->encode(
$input,
$this->getJsonProperties()
));

return parent::executeInsert($input);
}

// Ushahidi_Repository
public function executeUpdate(Array $where, Array $input)
{
// JSON Encode defined properties
$input = $this->json_transcoder->encode(
$input,
$this->getJsonProperties()
);

return parent::executeUpdate($where, $input);
}

}
25 changes: 6 additions & 19 deletions application/classes/Ushahidi/Repository/Form/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,27 @@
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Entity\FormAttribute;
use Ushahidi\Core\Entity\FormAttributeRepository;
use Ushahidi\Core\Tool\JsonTranscode;

class Ushahidi_Repository_Form_Attribute extends Ushahidi_Repository implements
FormAttributeRepository
{
protected $json_transcoder;
protected $json_properties = ['options', 'config'];
// Use the JSON transcoder to encode properties
use Ushahidi_JsonTranscodeRepository;

public function setTranscoder(JsonTranscode $transcoder)
// Ushahidi_JsonTranscodeRepository
protected function getJsonProperties()
{
$this->json_transcoder = $transcoder;
return ['options', 'config'];
}

// CreateRepository
public function create(Entity $entity)
{
$record = $this->json_transcoder->encode(
$entity->asArray(),
$this->json_properties
);
$record = $entity->asArray();
unset($record['form_id']);
return $this->executeInsert($this->removeNullValues($record));
}

// UpdateRepository
public function update(Entity $entity)
{
$record = $this->json_transcoder->encode(
$entity->getChanged(),
$this->json_properties
);
return $this->executeUpdate(['id' => $entity->getId()], $record);
}

// SearchRepository
protected function setSearchConditions(SearchData $search)
{
Expand Down
24 changes: 10 additions & 14 deletions application/classes/Ushahidi/Repository/Layer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@
use Ushahidi\Core\Entity;
use Ushahidi\Core\Entity\Layer;
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Tool\JsonTranscode;

class Ushahidi_Repository_Layer extends Ushahidi_Repository
{
protected $json_transcoder;
protected $json_properties = ['options'];

public function setTranscoder(JsonTranscode $transcoder)
{
$this->json_transcoder = $transcoder;
}
// Use the JSON transcoder to encode properties
use Ushahidi_JsonTranscodeRepository;

// Ushahidi_Repository
protected function getTable()
Expand All @@ -36,6 +30,12 @@ public function getEntity(Array $data = null)
return new Layer($data);
}

// Ushahidi_JsonTranscodeRepository
protected function getJsonProperties()
{
return ['options'];
}

// SearchRepository
public function getSearchFields()
{
Expand All @@ -59,9 +59,7 @@ protected function setSearchConditions(SearchData $search)
// CreateRepository
public function create(Entity $entity)
{
$record = $this->json_transcoder->encode(
$entity->asArray(), $this->json_properties
);
$record = array_filter($entity->asArray());
$record['created'] = time();

return $this->executeInsert($this->removeNullValues($record));
Expand All @@ -70,9 +68,7 @@ public function create(Entity $entity)
// UpdateRepository
public function update(Entity $entity)
{
$update = $this->json_transcoder->encode(
$entity->getChanged(), $this->json_properties
);
$update = $entity->getChanged();
$update['updated'] = time();

return $this->executeUpdate(['id' => $entity->id], $update);
Expand Down
28 changes: 11 additions & 17 deletions application/classes/Ushahidi/Repository/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Ushahidi\Core\Usecase\Post\UpdatePostRepository;
use Ushahidi\Core\Usecase\Post\UpdatePostTagRepository;
use Ushahidi\Core\Usecase\Set\SetPostRepository;
use Ushahidi\Core\Tool\JsonTranscode;
use Ushahidi\Core\Traits\UserContext;

use Aura\DI\InstanceFactory;
Expand All @@ -34,6 +33,9 @@ class Ushahidi_Repository_Post extends Ushahidi_Repository implements
{
use UserContext;

// Use the JSON transcoder to encode properties
use Ushahidi_JsonTranscodeRepository;

protected $form_attribute_repo;
protected $form_stage_repo;
protected $post_value_factory;
Expand All @@ -43,14 +45,6 @@ class Ushahidi_Repository_Post extends Ushahidi_Repository implements
protected $include_value_types = [];
protected $include_attributes = [];

protected $json_transcoder;
protected $json_properties = ['published_to'];

public function setTranscoder(JsonTranscode $transcoder)
{
$this->json_transcoder = $transcoder;
}

/**
* Construct
* @param Database $db
Expand Down Expand Up @@ -98,6 +92,12 @@ public function getEntity(Array $data = null)
return new Post($data);
}

// Ushahidi_JsonTranscodeRepository
protected function getJsonProperties()
{
return ['published_to'];
}

// Override selectQuery to fetch 'value' from db as text
protected function selectQuery(Array $where = [])
{
Expand Down Expand Up @@ -737,10 +737,7 @@ public function doesTranslationExist($locale, $parent_id, $type)
public function create(Entity $entity)
{

$post = array_filter($this->json_transcoder->encode(
$entity->asArray(),
$this->json_properties
));
$post = $entity->asArray();
$post['created'] = time();

// Remove attribute values and tags
Expand Down Expand Up @@ -773,10 +770,7 @@ public function create(Entity $entity)
// UpdateRepository
public function update(Entity $entity)
{
$post = $this->json_transcoder->encode(
$entity->getChanged(),
$this->json_properties
);
$post = $entity->getChanged();
$post['updated'] = time();

// Remove attribute values and tags
Expand Down
35 changes: 13 additions & 22 deletions application/classes/Ushahidi/Repository/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@
use Ushahidi\Core\Entity\SavedSearch;
use Ushahidi\Core\Entity\SetRepository;
use Ushahidi\Core\SearchData;
use Ushahidi\Core\Tool\JsonTranscode;

class Ushahidi_Repository_Set extends Ushahidi_Repository implements SetRepository
{
protected $post_repo;
protected $json_transcoder;
protected $json_properties = ['filter', 'view_options', 'visible_to'];

public function setTranscoder(JsonTranscode $transcoder)
{
$this->json_transcoder = $transcoder;
}
// Use the JSON transcoder to encode properties
use Ushahidi_JsonTranscodeRepository;

/**
* @var Boolean Return SavedSearches (when true) or vanilla Sets
Expand All @@ -49,6 +42,12 @@ public function getEntity(Array $data = null)
return $this->savedSearch ? new SavedSearch($data) : new Set($data);
}

// Ushahidi_JsonTranscodeRepository
protected function getJsonProperties()
{
return ['filter', 'view_options', 'visible_to'];
}

/**
* Override selectQuery to enforce filtering by search=0/1
*/
Expand All @@ -68,17 +67,15 @@ protected function selectQuery(Array $where = [])

// CreateRepository
public function create(Entity $entity) {
// Get record as an array
$record = $entity->asArray();
// .. then filter empty values and JSON encode properties
$record = array_filter($this->json_transcoder->encode(
$record,
$this->json_properties
));
// Get record and filter empty values
$record = array_filter($entity->asArray());

// Set the created time
$record['created'] = time();

// And save if this is a saved search or collection
$record['search'] = (int)$this->savedSearch;

// Finally, save the record to the DB
return $this->executeInsert($this->removeNullValues($record));

Expand All @@ -90,12 +87,6 @@ public function update(Entity $entity)
// Get changed values
$record = $entity->getChanged();

// .. then JSON encode json properties
$record = $this->json_transcoder->encode(
$record,
$this->json_properties
);

// Set the updated time
$record['updated'] = time();

Expand Down
Loading

0 comments on commit d17fd43

Please sign in to comment.