Skip to content

Commit

Permalink
Removing dependency to the Time class
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Nov 12, 2015
1 parent 3216125 commit 6981569
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/LockableTrait.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace RowLocker;

use Cake\I18n\Time;
use Cake\Database\Type;

/**
* Default implementation for LockableInterface
Expand All @@ -22,7 +22,7 @@ public function lock($by = null, $session = null)
throw new LockingException('This entity is already locked');
}

$this->set('locked_time', Time::now());
$this->set('locked_time', Type::build('datetime')->marshal(time()));
if ($by !== null) {
$this->set('locked_by', $by);
}
Expand Down Expand Up @@ -53,9 +53,10 @@ public function unlock()
*/
public function isLocked()
{
$now = Time::now();
$locked = $this->get('locked_time');
return $locked && $now->diffInSeconds($locked) < static::getLockTimeout();
$now = time();
$locked = $this->get('locked_time')->format('U');

return $locked && abs($now - $locked) < static::getLockTimeout();
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Behavior/RowLockerBehavior.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php
namespace RowLocker\Model\Behavior;

use Cake\I18n\Time;
use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\ORM\Table;
use RowLocker\LockableInterface;
use Cake\Utility\Hash;
use DateTimeImmutable;
use RowLocker\LockableInterface;

/**
* Contains custom finders
Expand Down Expand Up @@ -52,7 +52,7 @@ public function findUnlocked(Query $query, $options)
$entityClass = $this->_table->entityClass();

$nullExp = clone $exp;
$edge = Time::now()->subSeconds($entityClass::getLockTimeout());
$edge = new DateTimeImmutable('@'. time() - $entityClass::getLockTimeout());
$or = $exp->or_([
$nullExp->isNull($timeCol),
$exp->lte($timeCol, $edge, 'datetime')
Expand Down

0 comments on commit 6981569

Please sign in to comment.