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

Support return the increment or decrement result #42

Merged
merged 1 commit into from
Feb 22, 2020
Merged
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: 11 additions & 3 deletions src/Visits.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ protected function isCrawler()
* @param int $inc value to increment
* @param bool $force force increment, skip time limit
* @param array $ignore to ignore recording visits of periods, country, refer, language and operatingSystem. pass them on this array.
* @return bool
*/
public function increment($inc = 1, $force = false, $ignore = [])
{
Expand All @@ -212,36 +213,43 @@ public function increment($inc = 1, $force = false, $ignore = [])
$this->{'record'.Str::studly($method)}($inc);
}
}

return true;
}

return false;
}

/**
* @param int $inc
* @param array $ignore to ignore recording visits like country, periods ...
* @return bool
*/
public function forceIncrement($inc = 1, $ignore = [])
{
$this->increment($inc, true, $ignore);
return $this->increment($inc, true, $ignore);
}

/**
* Decrement a new/old subject to the cache cache.
*
* @param int $dec
* @param array $ignore to ignore recording visits like country, periods ...
* @return bool
*/
public function decrement($dec = 1, $force = false, $ignore = [])
{
$this->increment(-$dec, $force, $ignore);
return $this->increment(-$dec, $force, $ignore);
}

/**
* @param int $dec
* @param array $ignore to ignore recording visits like country, periods ...
* @return bool
*/
public function forceDecrement($dec = 1, $ignore = [])
{
$this->decrement($dec, true, $ignore);
return $this->decrement($dec, true, $ignore);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/VisitsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ public function by_can_accept_array()
$this->assertEquals(0, visits($post)->count());
}

/** @test * */
public function by_cant_repeat_accept_array()
{
User::create();
$post = Post::create();

$firstResult = visits($post)->increment();
$secondResult = visits($post)->increment();

$this->assertTrue($firstResult);
$this->assertFalse($secondResult);
$this->assertEquals(1, visits($post)->count());
}

/** @test * */
public function visits_by_user_lists()
{
Expand Down