Skip to content
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
7 changes: 7 additions & 0 deletions src/Helpers/FilterHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public static function recordNotification($notifiable, $notification): bool
});
}

public static function recordFeatureFlag($flag, $scope): bool
{
return static::recordEvent(function () use ($flag, $scope) {
return app(LaritorOverride::class)->recordFeatureFlag($flag, $scope);
});
}

public static function isBot($request): bool
{
return static::recordEvent(function () use ($request) {
Expand Down
1 change: 1 addition & 0 deletions src/LaritorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public function registerRecorders()
\BinaryBuilds\LaritorClient\Recorders\LogRecorder::class,
\BinaryBuilds\LaritorClient\Recorders\MailRecorder::class,
\BinaryBuilds\LaritorClient\Recorders\NotificationRecorder::class,
\BinaryBuilds\LaritorClient\Recorders\FeatureFlagRecorder::class,
];

foreach ($recorders as $recorder) {
Expand Down
10 changes: 10 additions & 0 deletions src/Override/DefaultOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ public function recordNotification($notifiable, $notification): bool
return true;
}

/**
* @param string $flag
* @param mixed $scope
* @return bool
*/
public function recordFeatureFlag($flag, $scope): bool
{
return true;
}

/**
* @param Request $request
* @return bool
Expand Down
7 changes: 7 additions & 0 deletions src/Override/LaritorOverride.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ public function recordMail($message): bool;
*/
public function recordNotification($notifiable, $notification): bool;

/**
* @param string $flag
* @param mixed $scope
* @return bool
*/
public function recordFeatureFlag($flag, $scope): bool;

/**
* @param Request $request
* @return bool
Expand Down
62 changes: 62 additions & 0 deletions src/Recorders/FeatureFlagRecorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace BinaryBuilds\LaritorClient\Recorders;

use BinaryBuilds\LaritorClient\Helpers\FilterHelper;
use BinaryBuilds\LaritorClient\Laritor;
use Illuminate\Support\Facades\Event;

class FeatureFlagRecorder extends Recorder
{
/**
* @var string
*/
public static $eventType = 'feature_flags';

/**
* @param mixed $event
* @return void
*/
public function trackEvent($event)
{
if(!FilterHelper::recordFeatureFlag($event->feature, $event->scope)) {
return;
}

self::recordFeatureCheck($event->feature, $event->value !== false);
}

public static function registerRecorder()
{
if (class_exists(\Laravel\Pennant\Events\FeatureRetrieved::class)) {
Event::listen( \Laravel\Pennant\Events\FeatureRetrieved::class, [static::class, 'handle'] );
}
}

public static function recordFeatureCheck(string $feature, bool $active = true)
{
$laritor = app(Laritor::class);

$flags = $laritor->getEvents(self::$eventType);
$addedFlags = [];

$flagAdded = false;
foreach ($flags as $flag) {
if ($flag['flag'] === $feature) {
$flagAdded = true;
$flag['active'] = $active;
$addedFlags[] = $flag;
break;
}
}

if (!$flagAdded) {
$addedFlags[] = [
'flag' => $feature,
'active' => $active
];
}

$laritor->addevents(self::$eventType, $addedFlags);
}
}
1 change: 0 additions & 1 deletion src/Recorders/MailRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use BinaryBuilds\LaritorClient\Helpers\DataHelper;
use BinaryBuilds\LaritorClient\Helpers\FilterHelper;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Log\Events\MessageLogged;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;
use Illuminate\Mail\Mailable;
Expand Down
10 changes: 10 additions & 0 deletions stubs/LaritorDataFilter.stub
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ class LaritorDataFilter extends DefaultOverride
return parent::recordNotification($notifiable, $notification);
}

/**
* @param string $flag
* @param mixed $scope
* @return bool
*/
public function recordFeatureFlag($flag, $scope): bool
{
return true;
}

/**
* @param Request $request
* @return bool
Expand Down