Last active
August 3, 2024 17:30
-
-
Save acabreragnz/4ab2291f18b665dc149d33d63893a171 to your computer and use it in GitHub Desktop.
Acapedia - Laravel using the FeatureFlagProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// JsonResource | |
<?php | |
namespace App\Http\Resources; | |
use Illuminate\Http\Resources\Json\JsonResource; | |
use Business\FeatureFlagProvider\FeatureFlagProvider; | |
use Business\FeatureFlagProvider\FeatureFlags; | |
class UserResource extends JsonResource | |
{ | |
private $featureFlagProvider; | |
public function __construct($resource, FeatureFlagProvider $featureFlagProvider) | |
{ | |
// Ensure you call the parent constructor | |
parent::__construct($resource); | |
$this->featureFlagProvider = $featureFlagProvider; | |
} | |
public function toArray($request) | |
{ | |
if ($this->featureFlagProvider->isFeatureEnabled(FeatureFlags::MILESTONE_NEW_SIGNUP_ENABLED)) { | |
// If the feature flag is enabled, include additional data | |
return [ | |
'id' => $this->id, | |
'name' => $this->name, | |
'email' => $this->email, | |
'special_field' => $this->special_field, | |
]; | |
} else { | |
// If the feature flag is not enabled, return the basic user data | |
return [ | |
'id' => $this->id, | |
'name' => $this->name, | |
'email' => $this->email, | |
]; | |
} | |
} | |
} | |
// Controller | |
<?php | |
namespace App\Http\Controllers; | |
use Business\FeatureFlagProvider\FeatureFlagProvider; | |
use Business\FeatureFlagProvider\FeatureFlags; | |
use Illuminate\Http\Request; | |
use Illuminate\Validation\ValidationException; | |
class SomeController extends Controller | |
{ | |
private $featureFlagProvider; | |
public function __construct(FeatureFlagProvider $featureFlagProvider) | |
{ | |
$this->featureFlagProvider = $featureFlagProvider; | |
} | |
public function store(Request $request) | |
{ | |
if ($this->featureFlagProvider->isFeatureEnabled(FeatureFlags::MILESTONE_NEW_SIGNUP_ENABLED)) { | |
// Si la característica está habilitada, valida los campos adicionales | |
$validatedData = $request->validate([ | |
'name' => 'required|max:255', | |
'email' => 'required|email', | |
'special_field' => 'required|string' | |
]); | |
// Realizar acciones adicionales... | |
} else { | |
// Si la característica no está habilitada, valida los campos básicos | |
$validatedData = $request->validate([ | |
'name' => 'required|max:255', | |
'email' => 'required|email' | |
]); | |
// Realizar acciones básicas... | |
} | |
// Insertar en la base de datos, enviar respuesta, etc. | |
return response()->json($validatedData, 201); | |
} | |
} | |
// Interactor | |
<?php | |
namespace App\Interactors; | |
use Business\FeatureFlagProvider\FeatureFlagProvider; | |
use Business\FeatureFlagProvider\FeatureFlags; | |
use App\Services\NotificationService; | |
class SomeInteractor | |
{ | |
private $featureFlagProvider; | |
private $notificationService; | |
public function __construct(FeatureFlagProvider $featureFlagProvider, NotificationService $notificationService) | |
{ | |
$this->featureFlagProvider = $featureFlagProvider; | |
$this->notificationService = $notificationService; | |
} | |
public function execute(array $data) | |
{ | |
if ($this->featureFlagProvider->isFeatureEnabled(FeatureFlags::MILESTONE_NEW_SIGNUP_ENABLED)) { | |
// Send a special notification if this feature is enabled | |
$this->notificationService->sendSpecialSignupNotification($data['userId']); | |
} else { | |
// Send a regular notification if the feature is not enabled | |
$this->notificationService->sendRegularSignupNotification($data['userId']); | |
} | |
} | |
} | |
// Repository | |
namespace App\Repositories; | |
use Business\FeatureFlagProvider\FeatureFlagProvider; | |
use Business\FeatureFlagProvider\FeatureFlags; | |
use App\Models\User; | |
class SomeRepository | |
{ | |
private $featureFlagProvider; | |
public function __construct(FeatureFlagProvider $featureFlagProvider) | |
{ | |
$this->featureFlagProvider = $featureFlagProvider; | |
} | |
public function createNewUser(array $data) | |
{ | |
if ($this->featureFlagProvider->isFeatureEnabled(FeatureFlags::MILESTONE_NEW_SIGNUP_ENABLED)) { | |
// Create a user with additional fields if this feature is enabled | |
return User::create([ | |
'name' => $data['name'], | |
'email' => $data['email'], | |
'special_field' => $data['special_field'], | |
]); | |
} else { | |
// Create a regular user if the feature is not enabled | |
return User::create([ | |
'name' => $data['name'], | |
'email' => $data['email'], | |
]); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment