forked from ntidev/SyncBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for limit, pagination, and changed the request and resp…
…onse structure to a more standarized way. Also. created a base sync repository that can be extended
- Loading branch information
Showing
6 changed files
with
332 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
namespace NTI\SyncBundle\Models; | ||
|
||
use JMS\Serializer\Annotation as JMS; | ||
|
||
/** | ||
* Class SyncPullRequestData | ||
* @package NTI\SyncBundle\Models | ||
*/ | ||
class SyncPullRequestData { | ||
|
||
/** | ||
* @var string | ||
* @JMS\Type("string") | ||
* | ||
* The name of the SyncMapping | ||
*/ | ||
private $mapping; | ||
|
||
/** | ||
* @var int | ||
* @JMS\Type("integer") | ||
* | ||
* The timestamp that the client wants to pull from | ||
*/ | ||
private $timestamp; | ||
|
||
/** | ||
* @var int | ||
* @JMS\Type("integer") | ||
* | ||
* The amount of results that should be returned | ||
*/ | ||
private $limit; | ||
|
||
/** | ||
* @var int | ||
* @JMS\Type("integer") | ||
* | ||
* The page for the current timestamp. | ||
* | ||
* Explanation: Let's say that the server is going to send results in batch of 50s | ||
* If there are 300 results whose last_timestamp is > than the timestamp provided | ||
* and all of those 300 results have the same timestamp (for example, it is normal to | ||
* set the inital timestamp to 0 when first installing this bundle) this would cause | ||
* a loop and the client would always sync the same 50 results over and over again. | ||
* | ||
* For this, the client can send the `page` parameter, which then can be used in the repository to offset the results. | ||
* | ||
*/ | ||
private $page = 1; | ||
|
||
/** | ||
* @var array|string | ||
* @JMS\Type("array") | ||
* | ||
* The serialization groups that the process should use when returning the results | ||
*/ | ||
private $serializationGroups = array("sync_basic"); | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getMapping() | ||
{ | ||
return $this->mapping; | ||
} | ||
|
||
/** | ||
* @param string $mapping | ||
* @return SyncPullRequestData | ||
*/ | ||
public function setMapping($mapping) | ||
{ | ||
$this->mapping = $mapping; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getTimestamp() | ||
{ | ||
return $this->timestamp; | ||
} | ||
|
||
/** | ||
* @param int $timestamp | ||
* @return SyncPullRequestData | ||
*/ | ||
public function setTimestamp($timestamp) | ||
{ | ||
$this->timestamp = $timestamp; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getLimit() | ||
{ | ||
return $this->limit; | ||
} | ||
|
||
/** | ||
* @param int $limit | ||
* @return SyncPullRequestData | ||
*/ | ||
public function setLimit($limit) | ||
{ | ||
$this->limit = $limit; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getPage() | ||
{ | ||
return $this->page; | ||
} | ||
|
||
/** | ||
* @param int $page | ||
* @return SyncPullRequestData | ||
*/ | ||
public function setPage($page) | ||
{ | ||
$this->page = $page; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return array|string | ||
*/ | ||
public function getSerializationGroups() | ||
{ | ||
return $this->serializationGroups; | ||
} | ||
|
||
/** | ||
* @param array|string $serializationGroups | ||
* @return SyncPullRequestData | ||
*/ | ||
public function setSerializationGroups($serializationGroups) | ||
{ | ||
$this->serializationGroups = $serializationGroups; | ||
return $this; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace NTI\SyncBundle\Models; | ||
|
||
use JMS\Serializer\Annotation as JMS; | ||
|
||
/** | ||
* Class SyncPullResponseData | ||
* @package NTI\SyncBundle\Models | ||
* | ||
*/ | ||
class SyncPullResponseData { | ||
|
||
/** | ||
* @var int | ||
* @JMS\SerializedName("real_last_timestamp") | ||
* | ||
* This is the last timestamp of the batch of results provided. Basically, it's the timestamp of the last element | ||
* in the array of provided elements in data. This is useful for the client so it can continue synching from this | ||
* timestamp. | ||
*/ | ||
private $realLastTimestamp; | ||
|
||
/** | ||
* @var array | ||
* | ||
* The list of elements that changed. | ||
*/ | ||
private $data; | ||
|
||
/** | ||
* @var int | ||
* @JMS\SerializedName("total_count") | ||
* | ||
* This is the total count of results from the timestamp provided. Useful for the client to know if there are more results and how many more. | ||
*/ | ||
private $totalCount; | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getRealLastTimestamp(): int | ||
{ | ||
return $this->realLastTimestamp; | ||
} | ||
|
||
/** | ||
* @param int $realLastTimestamp | ||
* @return SyncPullResponseData | ||
*/ | ||
public function setRealLastTimestamp(int $realLastTimestamp): SyncPullResponseData | ||
{ | ||
$this->realLastTimestamp = $realLastTimestamp; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getData(): array | ||
{ | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* @return SyncPullResponseData | ||
*/ | ||
public function setData(array $data): SyncPullResponseData | ||
{ | ||
$this->data = $data; | ||
return $this; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getTotalCount(): int | ||
{ | ||
return $this->totalCount; | ||
} | ||
|
||
/** | ||
* @param int $totalCount | ||
* @return SyncPullResponseData | ||
*/ | ||
public function setTotalCount(int $totalCount): SyncPullResponseData | ||
{ | ||
$this->totalCount = $totalCount; | ||
return $this; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace NTI\SyncBundle\Repository; | ||
|
||
use Doctrine\ORM\EntityRepository; | ||
use JMS\Serializer\SerializationContext; | ||
use NTI\SyncBundle\Models\SyncPullRequestData; | ||
use NTI\SyncBundle\Models\SyncPullResponseData; | ||
use NTI\SyncBundle\Entity\SyncState; | ||
use NTI\SyncBundle\Interfaces\SyncRepositoryInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
class SyncRepository extends EntityRepository implements SyncRepositoryInterface { | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function findFromTimestamp(ContainerInterface $container, SyncPullRequestData $requestData) | ||
{ | ||
$timestamp = $requestData->getTimestamp(); | ||
$serializationGroups = $requestData->getSerializationGroups(); | ||
$page = $requestData->getPage() > 0 ? $requestData->getPage() - 1 : 0; | ||
$limit = $requestData->getLimit(); | ||
|
||
// Joins | ||
$qb = $this->createQueryBuilder('i'); | ||
$qb->andWhere($qb->expr()->gte('i.lastTimestamp', $timestamp)); | ||
$qb->orderBy('i.lastTimestamp', 'asc'); | ||
|
||
/** | ||
* This should be set BEFORE getting the total count, that way the client will receive | ||
* the actual items that are left for it to sync, not the total amount from a timestamp. | ||
* @Ref the "page"parameter in SyncPulLRequestData | ||
*/ | ||
$qb->setFirstResult($page * $limit); | ||
|
||
// Total records | ||
$totalCountQb = clone $qb; | ||
$totalCountQb->select('COUNT(i.id)'); | ||
$totalCountQuery = $totalCountQb->getQuery(); | ||
|
||
try { | ||
$totalCount = intval($totalCountQuery->getSingleScalarResult()); | ||
} catch (\Exception $e) { | ||
$totalCount = 0; | ||
} | ||
|
||
$qb->setMaxResults($limit); | ||
|
||
$items = $qb->getQuery()->getResult(); | ||
|
||
$realLastTimestamp = count($items) <= 0 ? $timestamp : $items[count($items) - 1]->getLastTimestamp(); | ||
|
||
$itemsArray = json_decode($container->get('jms_serializer')->serialize($items, 'json', SerializationContext::create()->setGroups($serializationGroups)), true); | ||
|
||
$result = new SyncPullResponseData(); | ||
$result->setData($itemsArray); | ||
$result->setRealLastTimestamp($realLastTimestamp); | ||
$result->setTotalCount($totalCount); | ||
|
||
return $result; | ||
} | ||
|
||
} |
Oops, something went wrong.