Skip to content

Commit

Permalink
MDL-63658 core_favourites: properly define interface methods and cleanup
Browse files Browse the repository at this point in the history
This gets rid of specific repo functions which were unused, and makes
sure the following methods are defined on the interface, implemented
and tested:
- exists_by($criteria)
- find_by($criteria)
- delete_by($crtieria)
Also, added missing tests for find_favourite() repo method.
  • Loading branch information
snake committed Oct 18, 2018
1 parent 1f3c76f commit 68eaa31
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 101 deletions.
157 changes: 67 additions & 90 deletions favourites/classes/local/repository/favourite_repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,44 @@ protected function get_list_of_favourites_from_records(array $records) {
return $list;
}

/**
* Basic validation, confirming we have the minimum field set needed to save a record to the store.
*
* @param favourite $favourite the favourite record to validate.
* @throws \moodle_exception if the supplied favourite has missing or unsupported fields.
*/
protected function validate(favourite $favourite) {

$favourite = (array)$favourite;

// The allowed fields, and whether or not each is required to create a record.
// The timecreated, timemodified and id fields are generated during create/update.
$allowedfields = [
'userid' => true,
'component' => true,
'itemtype' => true,
'itemid' => true,
'contextid' => true,
'ordering' => false,
'timecreated' => false,
'timemodified' => false,
'id' => false
];

$requiredfields = array_filter($allowedfields, function($field) {
return $field;
});

if ($missingfields = array_keys(array_diff_key($requiredfields, $favourite))) {
throw new \moodle_exception("Missing object property(s) '" . join(', ', $missingfields) . "'.");
}

// If the record contains fields we don't allow, throw an exception.
if ($unsupportedfields = array_keys(array_diff_key($favourite, $allowedfields))) {
throw new \moodle_exception("Unexpected object property(s) '" . join(', ', $unsupportedfields) . "'.");
}
}

/**
* Add a favourite to the repository.
*
Expand Down Expand Up @@ -130,31 +168,31 @@ public function find(int $id) : favourite {
}

/**
* Return all items matching the supplied criteria (a [key => value,..] list).
* Return all items in this repository, as an array, indexed by id.
*
* @param array $criteria the list of key/value criteria pairs.
* @param int $limitfrom optional pagination control for returning a subset of records, starting at this point.
* @param int $limitnum optional pagination control for returning a subset comprising this many records.
* @return array the list of favourites matching the criteria.
* @return array the list of all favourites stored within this repository.
* @throws \dml_exception if any database errors are encountered.
*/
public function find_by(array $criteria, int $limitfrom = 0, int $limitnum = 0) : array {
public function find_all(int $limitfrom = 0, int $limitnum = 0) : array {
global $DB;
$records = $DB->get_records($this->favouritetable, $criteria, '', '*', $limitfrom, $limitnum);
$records = $DB->get_records($this->favouritetable, null, '', '*', $limitfrom, $limitnum);
return $this->get_list_of_favourites_from_records($records);
}

/**
* Return all items in this repository, as an array, indexed by id.
* Return all items matching the supplied criteria (a [key => value,..] list).
*
* @param array $criteria the list of key/value criteria pairs.
* @param int $limitfrom optional pagination control for returning a subset of records, starting at this point.
* @param int $limitnum optional pagination control for returning a subset comprising this many records.
* @return array the list of all favourites stored within this repository.
* @return array the list of favourites matching the criteria.
* @throws \dml_exception if any database errors are encountered.
*/
public function find_all(int $limitfrom = 0, int $limitnum = 0) : array {
public function find_by(array $criteria, int $limitfrom = 0, int $limitnum = 0) : array {
global $DB;
$records = $DB->get_records($this->favouritetable, null, '', '*', $limitfrom, $limitnum);
$records = $DB->get_records($this->favouritetable, $criteria, '', '*', $limitfrom, $limitnum);
return $this->get_list_of_favourites_from_records($records);
}

Expand Down Expand Up @@ -196,6 +234,18 @@ public function exists(int $id) : bool {
return $DB->record_exists($this->favouritetable, ['id' => $id]);
}

/**
* Check whether an item exists in this repository, based on the specified criteria.
*
* @param array $criteria the list of key/value criteria pairs.
* @return bool true if the favourite exists, false otherwise.
* @throws \dml_exception if any database errors are encountered.
*/
public function exists_by(array $criteria) : bool {
global $DB;
return $DB->record_exists($this->favouritetable, $criteria);
}

/**
* Update a favourite.
*
Expand Down Expand Up @@ -223,60 +273,25 @@ public function delete(int $id) {
}

/**
* Return the total number of favourites in this repository.
*
* @return int the total number of items.
* @throws \dml_exception if any database errors are encountered.
*/
public function count() : int {
global $DB;
return $DB->count_records($this->favouritetable);
}

/**
* Check for the existence of a favourite item in the specified area.
* Delete all favourites matching the specified criteria.
*
* A favourite item is identified by the itemid/contextid pair.
* An area is identified by the component/itemtype pair.
*
* @param int $userid the id of user to whom the favourite belongs.
* @param string $component the frankenstyle component name.
* @param string $itemtype the type of the favourited item.
* @param int $itemid the id of the item which was favourited (not the favourite's id).
* @param int $contextid the contextid of the item which was favourited.
* @return bool true if the favourited item exists, false otherwise.
* @param array $criteria the list of key/value criteria pairs.
* @throws \dml_exception if any database errors are encountered.
*/
public function exists_by_area(int $userid, string $component, string $itemtype, int $itemid, int $contextid) : bool {
public function delete_by(array $criteria) {
global $DB;
return $DB->record_exists($this->favouritetable,
[
'userid' => $userid,
'component' => $component,
'itemtype' => $itemtype,
'itemid' => $itemid,
'contextid' => $contextid
]
);
$DB->delete_records($this->favouritetable, $criteria);
}

/**
* Delete all favourites within the component/itemtype.
* Return the total number of favourites in this repository.
*
* @param int $userid the id of the user to whom the favourite belongs.
* @param string $component the frankenstyle component name.
* @param string $itemtype the type of the favourited item.
* @return int the total number of items.
* @throws \dml_exception if any database errors are encountered.
*/
public function delete_by_area(int $userid, string $component, string $itemtype) {
public function count() : int {
global $DB;
$DB->delete_records($this->favouritetable,
[
'userid' => $userid,
'component' => $component,
'itemtype' => $itemtype
]
);
return $DB->count_records($this->favouritetable);
}

/**
Expand All @@ -290,42 +305,4 @@ public function count_by(array $criteria) : int {
global $DB;
return $DB->count_records($this->favouritetable, $criteria);
}

/**
* Basic validation, confirming we have the minimum field set needed to save a record to the store.
*
* @param favourite $favourite the favourite record to validate.
* @throws \moodle_exception if the supplied favourite has missing or unsupported fields.
*/
protected function validate(favourite $favourite) {

$favourite = (array)$favourite;

// The allowed fields, and whether or not each is required to create a record.
// The timecreated, timemodified and id fields are generated during create/update.
$allowedfields = [
'userid' => true,
'component' => true,
'itemtype' => true,
'itemid' => true,
'contextid' => true,
'ordering' => false,
'timecreated' => false,
'timemodified' => false,
'id' => false
];

$requiredfields = array_filter($allowedfields, function($field) {
return $field;
});

if ($missingfields = array_keys(array_diff_key($requiredfields, $favourite))) {
throw new \moodle_exception("Missing object property(s) '" . join(', ', $missingfields) . "'.");
}

// If the record contains fields we don't allow, throw an exception.
if ($unsupportedfields = array_keys(array_diff_key($favourite, $allowedfields))) {
throw new \moodle_exception("Unexpected object property(s) '" . join(', ', $unsupportedfields) . "'.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,29 @@ public function find_by(array $criteria, int $limitfrom = 0, int $limitnum = 0)
*/
public function exists(int $id) : bool;

/**
* Check whether an item exists in this repository, based on the specified criteria.
*
* @param array $criteria the list of key/value criteria pairs.
* @return bool true if the favourite exists, false otherwise.
*/
public function exists_by(array $criteria) : bool;

/**
* Return the total number of items in this repository.
*
* @return int the total number of items.
*/
public function count() : int;

/**
* Return the number of favourites matching the specified criteria.
*
* @param array $criteria the list of key/value criteria pairs.
* @return int the number of favourites matching the criteria.
*/
public function count_by(array $criteria) : int;

/**
* Update an item within this repository.
*
Expand All @@ -103,6 +119,14 @@ public function update(favourite $item) : favourite;
*/
public function delete(int $id);

/**
* Delete all favourites matching the specified criteria.
*
* @param array $criteria the list of key/value criteria pairs.
* @return void.
*/
public function delete_by(array $criteria);

/**
* Find a single favourite, based on it's unique identifiers.
*
Expand Down
Loading

0 comments on commit 68eaa31

Please sign in to comment.