Skip to content

Commit

Permalink
Add isPermanent & isTemporary ban checks (#27)
Browse files Browse the repository at this point in the history
* Add `isPermanent` & `isTemporary` ban checks
* Fix setting null expired_at value
  • Loading branch information
antonkomarev authored Sep 15, 2018
1 parent b75def6 commit 64801ee
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 10 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

All notable changes to `laravel-ban` will be documented in this file.

## [3.3.0] - 2018-09-16

### Added

- ([#27](https://github.com/cybercog/laravel-ban/pull/27)) Add `isPermanent` & `isTemporary` ban checks

### Fixed

- ([#27](https://github.com/cybercog/laravel-ban/pull/27)) Stop trying to parse `null` value for `expired_at` as Carbon value

## [3.2.0] - 2018-09-09

### Added
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,34 @@ $user->isNotBanned();
app(\Cog\Contracts\Ban\BanService::class)->deleteExpiredBans();
```

#### Determine if ban is permanent

```php
$ban = $user->ban();

$ban->isPermanent(); // true
```

Or pass `null` value.

```php
$ban = $user->ban([
'expired_at' => null,
]);

$ban->isPermanent(); // true
```

#### Determine if ban is temporary

```php
$ban = $user->ban([
'expired_at' => '2086-03-28 00:00:00',
]);

$ban->isTemporary(); // true
```

### Scopes

#### Get all models which are not banned
Expand Down
18 changes: 10 additions & 8 deletions contracts/Ban.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

namespace Cog\Contracts\Ban;

use Cog\Contracts\Ban\Bannable as BannableContract;
use Illuminate\Database\Eloquent\Builder;

/**
* Interface Ban.
*
Expand All @@ -36,11 +33,16 @@ public function createdBy();
public function bannable();

/**
* Scope a query to only include bans by bannable model.
* Determine if Ban is permanent.
*
* @return bool
*/
public function isPermanent();

/**
* Determine if Ban is temporary.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Cog\Contracts\Ban\Bannable $bannable
* @return \Illuminate\Database\Eloquent\Builder
* @return bool
*/
public function scopeWhereBannable(Builder $query, BannableContract $bannable);
public function isTemporary();
}
25 changes: 23 additions & 2 deletions src/Models/Ban.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,37 @@ class Ban extends Model implements BanContract
* Expired timestamp mutator.
*
* @param \Carbon\Carbon|string $value
* @return void
*/
public function setExpiredAtAttribute($value)
{
if (!$value instanceof Carbon) {
if (!is_null($value) && !$value instanceof Carbon) {
$value = Carbon::parse($value);
}

$this->attributes['expired_at'] = $value;
}

/**
* Determine if Ban is permanent.
*
* @return bool
*/
public function isPermanent()
{
return !isset($this->attributes['expired_at']) || is_null($this->attributes['expired_at']);
}

/**
* Determine if Ban is temporary.
*
* @return bool
*/
public function isTemporary()
{
return !$this->isPermanent();
}

/**
* Entity responsible for ban.
*
Expand Down Expand Up @@ -97,8 +118,8 @@ public function bannable()
public function scopeWhereBannable(Builder $query, BannableContract $bannable)
{
return $query->where([
'bannable_id' => $bannable->getKey(),
'bannable_type' => $bannable->getMorphClass(),
'bannable_id' => $bannable->getKey(),
]);
}
}
45 changes: 45 additions & 0 deletions tests/Unit/Models/BanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ public function it_can_cast_expired_at()
$this->assertInstanceOf(Carbon::class, $ban->expired_at);
}

/** @test */
public function it_not_modify_null_expired_at()
{
$ban = new Ban([
'expired_at' => null,
]);

$this->assertNull($ban->expired_at);
}

/** @test */
public function it_can_has_ban_creator()
{
Expand Down Expand Up @@ -134,4 +144,39 @@ public function it_can_scope_bannable_models()

$this->assertCount(4, $bannableModels);
}

/** @test */
public function it_can_check_if_ban_is_permanent()
{
$permanentBan = new Ban();
$temporaryBan = new Ban([
'expired_at' => '2086-03-28 00:00:00',
]);

$this->assertTrue($permanentBan->isPermanent());
$this->assertFalse($temporaryBan->isPermanent());
}

/** @test */
public function it_can_check_if_ban_is_temporary()
{
$permanentBan = new Ban();
$temporaryBan = new Ban([
'expired_at' => '2086-03-28 00:00:00',
]);

$this->assertFalse($permanentBan->isTemporary());
$this->assertTrue($temporaryBan->isTemporary());
}

/** @test */
public function it_can_check_if_ban_with_null_expired_at_is_permanent()
{
$permanentBan = new Ban([
'expired_at' => null,
]);

$this->assertTrue($permanentBan->isPermanent());
$this->assertFalse($permanentBan->isTemporary());
}
}

0 comments on commit 64801ee

Please sign in to comment.