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

Feature: VIP #6

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
26 changes: 26 additions & 0 deletions app/Console/Commands/VipActivate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Laravel\Pennant\FeatureManager;

class VipActivate extends Command
{
protected $signature = 'app:vip:activate {emails*}';

protected $description = 'Activate VIP permission';

public function handle(User $userModel, FeatureManager $feature): int
{
$users = $userModel->newQuery()
->select('id', 'email')
->whereIn('email', $this->argument('emails'))
->get();

$feature->for($users)->activate('vip');

return self::SUCCESS;
}
}
26 changes: 26 additions & 0 deletions app/Console/Commands/VipDeactivate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Laravel\Pennant\FeatureManager;

class VipDeactivate extends Command
{
protected $signature = 'app:vip:deactivate {emails*}';

protected $description = 'Deactivate VIP permission';

public function handle(User $userModel, FeatureManager $feature): int
{
$users = $userModel->newQuery()
->select('id', 'email')
->whereIn('email', $this->argument('emails'))
->get();

$feature->for($users)->deactivate('vip');

return self::SUCCESS;
}
}
36 changes: 36 additions & 0 deletions app/Console/Commands/VipStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Laravel\Pennant\Feature;

class VipStatus extends Command
{
protected $signature = 'app:vip:status {emails*}';

protected $description = 'Inspect VIP status';

public function handle(User $userModel): int
{
$emails = $this->argument('emails');

$users = $userModel->newQuery()
->select('id', 'email')
->whereIn('email', $emails)
->get();

Feature::for($users)->load('vip');

$status = $users->map(function (User $user) {
$user['vip'] = $user->features()->active('vip') ? 'ON' : 'OFF';

return $user->toArray();
});

$this->table(['ID', 'Email', 'status'], $status->toArray());

return self::SUCCESS;
}
}
20 changes: 20 additions & 0 deletions app/Features/Vip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Features;

use Illuminate\Support\Lottery;

class Vip
{
public readonly string $name;

public function __construct()
{
$this->name = 'vip';
}

public function resolve(mixed $scope): bool
{
return false;
}
}
16 changes: 15 additions & 1 deletion app/Http/Controllers/User/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;
use Laravel\Pennant\Feature;
use RealRashid\SweetAlert\Facades\Alert;

class Order
Expand Down Expand Up @@ -38,7 +40,19 @@ public function __invoke(OrderRequest $request, Cart $cartModel): RedirectRespon
} else {
$totalPrice = $cart->product->price;
}
$order->price = $totalPrice;

$isVip = Feature::for($user)->active('vip');

if ($isVip) {
$order->price = (int)$totalPrice * 0.9;
} else {
$order->price = $totalPrice;
}

Log::info('User ordered', [
'user' => $user->email,
'is_vip' => $isVip,
]);

if ($order->save()) {
//cart delete
Expand Down
4 changes: 3 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Pennant\Concerns\HasFeatures;
use Spatie\Permission\Traits\HasRoles;

/**
Expand All @@ -27,8 +28,9 @@
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
use HasFeatures;
use HasRoles;
use Notifiable;

/**
* The attributes that are mass assignable.
Expand Down
Loading