Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2.x: Add new activities filters #55

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Model/Activities/HasActivitiesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,18 @@ public function getActivity($id);
/**
* Get a list of activities.
*
* @param int $limit
* Limit the number of activities to return.
* @param string $type
* @param int $limit
* Limit the number of activities to return. Zero for no limit.
* @param string|null $type
* Filter activities by type.
* @param int $startsAt
* @param int|null $startsAt
* A UNIX timestamp for the maximum created date of activities to return.
* @param string|string[]|null $state
* Filter activities by state ("pending", "in_progress", "complete").
* @param string|string[]|null $result
* Filter activities by result ("success" or "failure").
*
* @return Activity[]
*/
public function getActivities($limit = 0, $type = null, $startsAt = null);
public function getActivities($limit = 0, $type = null, $startsAt = null, $state = null, $result = null);
}
33 changes: 20 additions & 13 deletions src/Model/Activities/HasActivitiesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,32 @@ public function getActivity($id)
/**
* {@inheritDoc}
*/
public function getActivities($limit = 0, $type = null, $startsAt = null)
public function getActivities($limit = 0, $type = null, $startsAt = null, $state = null, $result = null)
{
$options = [];
$query = '';
if ($type !== null) {
$options['query']['type'] = $type;
$query .= '&type=' . \rawurlencode($type);
}
if ($startsAt !== null) {
$options['query']['starts_at'] = Activity::formatStartsAt($startsAt);
$query .= '&starts_at=' . Activity::formatStartsAt($startsAt);
}

$activities = Activity::getCollection($this->getUri() . '/activities', $limit, $options, $this->client);

// Guarantee the type filter (works around a temporary bug).
if ($type !== null) {
$activities = array_filter($activities, function (Activity $activity) use ($type) {
return $activity->type === $type;
});
if (!empty($limit)) {
$query .= '&count=' . $limit;
}
if ($result !== null) {
foreach ((array) $result as $resultItem) {
$query .= '&result=' . \rawurlencode($resultItem);
}
}
if ($state !== null) {
foreach ((array) $state as $stateItem) {
$query .= '&state=' . \rawurlencode($stateItem);
}
}
if ($query !== '') {
$query = '?' . \substr($query, 1);
}

return $activities;
return Activity::getCollection($this->getUri() . '/activities' . $query, $limit, [], $this->client);
}
}
8 changes: 2 additions & 6 deletions src/Model/ApiResourceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ protected static function checkProperty($property, $value)
*
* @param string $url The collection URL.
* @param int $limit A limit on the number of resources to
* return. Use 0 for no limit.
* return. Use 0 for no limit. Deprecated.
* @param array $options An array of additional Guzzle request
* options.
* @param ClientInterface $client A suitably configured Guzzle client.
Expand All @@ -312,15 +312,11 @@ protected static function checkProperty($property, $value)
*/
public static function getCollection($url, $limit, array $options, ClientInterface $client)
{
// @todo uncomment this when the API implements a 'count' parameter
// if ($limit) {
// $options['query']['count'] = $limit;
// }
$request = new Request('get', $url);
$data = self::send($request, $client, $options);

// @todo remove this when the API implements a 'count' parameter
if ($limit) {
if (!empty($limit) && count($data) > $limit) {
$data = array_slice($data, 0, $limit);
}

Expand Down