Skip to content

Commit

Permalink
Merge pull request #19 from ShoppinPal/add-version-compare-helper
Browse files Browse the repository at this point in the history
Add helper method to compare versions returned from the api to already retrieved versions
  • Loading branch information
szeber authored Oct 24, 2019
2 parents 48fa57f + 65fc8b3 commit ff2f314
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/ShoppinPal/Vend/DataObject/Entity/V2/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace ShoppinPal\Vend\DataObject\Entity\V2;

use ShoppinPal\Vend\DataObject\Entity\EntityDoAbstract;
use YapepBase\Exception\ParameterException;

class Version extends EntityDoAbstract
{
Expand Down Expand Up @@ -45,4 +46,36 @@ class Version extends EntityDoAbstract
public $customers;

public $consignments;

/**
* Returns TRUE if there is a newer version of the given entity.
*
* Since Vend recently changed this endpoint's behaviour and now returns a version of 1 if the retailer has no
* entities for an entity type instead of 0, we will not consider any version less than 10 by default as a new
* version (treat it as it has no entities for that type). Since the versions are typically much higher numbers
* than 10 this should be safe to be left at 10, but can be overridden by the minimumVersion param.
*
* @param string $entityType The entity type. Must match one of the properties of this entity.
* @param int|null $lastRetrievedVersion The last retrieved version
* @param int $minimumVersion The minimum version number to be considered a new version
*
* @return bool
*
* @throws ParameterException If the entity type is invalid
*/
public function hasNewerVersion(string $entityType, ?int $lastRetrievedVersion, int $minimumVersion = 10): bool
{
$invalidEntityTypes = [
'subEntities',
'ignoredProperties',
];

$validEntityTypes = array_diff(array_keys(get_object_vars($this)), $invalidEntityTypes);

if (!in_array($entityType, $invalidEntityTypes)) {
throw new ParameterException('Invalid entity type: ' . $entityType);
}

return $lastRetrievedVersion > max($minimumVersion - 1, (int)$lastRetrievedVersion);
}
}

0 comments on commit ff2f314

Please sign in to comment.