From 1f8c118233aef705ace22b53afe3f1072e45f1ce Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Mon, 1 May 2023 19:24:41 +0200 Subject: [PATCH 1/5] refactor: delete single pattern in class Subscribe --- .gitignore | 1 + .../src/Application/Subscribe.php | 16 ++++++------ .../src/SubscribeController.php | 26 ++++++++++++++----- 3 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..485dee64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php b/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php index 37944157..82d908da 100644 --- a/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php +++ b/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php @@ -12,21 +12,21 @@ final class Subscribe { + public function __construct(private MySqlConnection $connection, private FeatureFlags $featureFlags, private EmailNotifier $emailNotifier) { + + } + public function __invoke(string $email, ?string $name = null): void { - if (Debug::instance()->isDebugModeEnabled()) { - FeatureFlags::instance()->deactivateAll(); - } - - $flag = FeatureFlags::instance()->get(Flags::NEW_SUBSCRIPTION_PAGE_NAME); + $flag = $this->featureFlags->get(Flags::NEW_SUBSCRIPTION_PAGE_NAME); if ($flag) { // The new subscription added a "name" field - MySqlConnection::instance()->persist($email, $name); + $this->connection->persist($email, $name); } else { - MySqlConnection::instance()->persist($email); + $this->connection->persist($email); } - EmailNotifier::instance()->sendSubscriptionEmail($email); + $this->emailNotifier->sendSubscriptionEmail($email); } } diff --git a/examples/php/php-feature_flags-01_base/src/SubscribeController.php b/examples/php/php-feature_flags-01_base/src/SubscribeController.php index 7847e235..aed3d8c1 100644 --- a/examples/php/php-feature_flags-01_base/src/SubscribeController.php +++ b/examples/php/php-feature_flags-01_base/src/SubscribeController.php @@ -5,22 +5,36 @@ namespace CodelyTv; use CodelyTv\Application\Subscribe; +use CodelyTv\Email\EmailNotifier; +use CodelyTv\Persistence\MySqlConnection; use Symfony\Component\HttpFoundation\Request; final class SubscribeController { public function __invoke(Request $request) { - $flagHeader = $request->headers->get('X-FLAG'); - - if ($flagHeader === Flags::NEW_SUBSCRIPTION_PAGE_TOKEN) { - FeatureFlags::instance()->set('new_subscription_page', true); - } + $subscribeUseCase = new Subscribe( + MySqlConnection::instance(), + $this->getFeatures($request), + EmailNotifier::instance() + ); - $subscribeUseCase = new Subscribe(); $subscribeUseCase->__invoke( $request->request->get('email'), $request->request->get('name'), ); } + + private function getFeatures(Request $request): FeatureFlags + { + $flagHeader = $request->headers->get('X-FLAG'); + $features = FeatureFlags::instance(); + if ($flagHeader === Flags::NEW_SUBSCRIPTION_PAGE_TOKEN) { + $features->set('new_subscription_page', true); + } + if (Debug::instance()->isDebugModeEnabled()) { + $features->deactivateAll(); + } + return $features; + } } From eedd5ac731344a5193ebd9f1c7139df604d6e4f4 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Mon, 1 May 2023 19:30:46 +0200 Subject: [PATCH 2/5] refactor: remove singleton pattern in MysqlConnection + --- .../src/Persistence/MySqlConnection.php | 14 ++------------ .../src/SubscribeController.php | 5 +++-- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php b/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php index 44545f6d..0b4332e5 100644 --- a/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php +++ b/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php @@ -9,25 +9,15 @@ final class MySqlConnection { - private static ?MySqlConnection $instance = null; - - private function __construct() + public function __construct(private FeatureFlags $flags) { } - public static function instance(): MySqlConnection - { - if (!self::$instance) { - self::$instance = new self(); - } - return self::$instance; - } - public function persist(string $email, ?string $name = null): void { $subscription = ['email' => $email]; - $flag = FeatureFlags::instance()->get(Flags::NEW_SUBSCRIPTION_PAGE_NAME); + $flag = $this->flags->get(Flags::NEW_SUBSCRIPTION_PAGE_NAME); if ($flag) { $subscription['name'] = $name; } diff --git a/examples/php/php-feature_flags-01_base/src/SubscribeController.php b/examples/php/php-feature_flags-01_base/src/SubscribeController.php index aed3d8c1..5f6e7197 100644 --- a/examples/php/php-feature_flags-01_base/src/SubscribeController.php +++ b/examples/php/php-feature_flags-01_base/src/SubscribeController.php @@ -13,9 +13,10 @@ final class SubscribeController { public function __invoke(Request $request) { + $flags = $this->getFeatures($request); $subscribeUseCase = new Subscribe( - MySqlConnection::instance(), - $this->getFeatures($request), + new MySqlConnection($flags), + $flags, EmailNotifier::instance() ); From 27c2948a55ed72cb35789bfb53d186cb2fc703a9 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Mon, 1 May 2023 19:43:36 +0200 Subject: [PATCH 3/5] refactor: create FeatureFlagsInmutable --- .../src/Application/Subscribe.php | 5 ++--- .../src/Email/EmailNotifier.php | 15 ++++----------- .../src/Email/subscription-email.php | 5 +++-- .../src/FeatureFlagsInmutable.php | 19 +++++++++++++++++++ .../src/Persistence/MySqlConnection.php | 4 ++-- .../src/SubscribeController.php | 17 +++++++++-------- 6 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php diff --git a/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php b/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php index 82d908da..0382b7d0 100644 --- a/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php +++ b/examples/php/php-feature_flags-01_base/src/Application/Subscribe.php @@ -4,15 +4,14 @@ namespace CodelyTv\Application; -use CodelyTv\Debug; use CodelyTv\Email\EmailNotifier; -use CodelyTv\FeatureFlags; +use CodelyTv\FeatureFlagsInmutable; use CodelyTv\Flags; use CodelyTv\Persistence\MySqlConnection; final class Subscribe { - public function __construct(private MySqlConnection $connection, private FeatureFlags $featureFlags, private EmailNotifier $emailNotifier) { + public function __construct(private MySqlConnection $connection, private FeatureFlagsInmutable $featureFlags, private EmailNotifier $emailNotifier) { } diff --git a/examples/php/php-feature_flags-01_base/src/Email/EmailNotifier.php b/examples/php/php-feature_flags-01_base/src/Email/EmailNotifier.php index 85d11c92..6bfe5604 100644 --- a/examples/php/php-feature_flags-01_base/src/Email/EmailNotifier.php +++ b/examples/php/php-feature_flags-01_base/src/Email/EmailNotifier.php @@ -4,24 +4,17 @@ namespace CodelyTv\Email; +use CodelyTv\FeatureFlagsInmutable; + final class EmailNotifier { - private static ?EmailNotifier $instance = null; - - private function __construct() - { - } - - public static function instance(): EmailNotifier + public function __construct(private FeatureFlagsInmutable $flags) { - if (!self::$instance) { - self::$instance = new self(); - } - return self::$instance; } public function sendSubscriptionEmail(string $to) { + $flags = $this->flags; echo "Email sent to $to"; require __DIR__ . '/subscription-email.php'; } diff --git a/examples/php/php-feature_flags-01_base/src/Email/subscription-email.php b/examples/php/php-feature_flags-01_base/src/Email/subscription-email.php index 17ea3406..12b08ac9 100644 --- a/examples/php/php-feature_flags-01_base/src/Email/subscription-email.php +++ b/examples/php/php-feature_flags-01_base/src/Email/subscription-email.php @@ -1,5 +1,6 @@

Message for all subscribers

- -get(\CodelyTv\Flags::NEW_SUBSCRIPTION_PAGE_NAME)): ?> +get(\CodelyTv\Flags::NEW_SUBSCRIPTION_PAGE_NAME)): ?>

Additional message for subscribers with active flag

diff --git a/examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php b/examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php new file mode 100644 index 00000000..8322b0f5 --- /dev/null +++ b/examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php @@ -0,0 +1,19 @@ +flags)) { + return false; + } + + return $this->flags[$flagName]; + } +} diff --git a/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php b/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php index 0b4332e5..5b481de5 100644 --- a/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php +++ b/examples/php/php-feature_flags-01_base/src/Persistence/MySqlConnection.php @@ -4,12 +4,12 @@ namespace CodelyTv\Persistence; -use CodelyTv\FeatureFlags; +use CodelyTv\FeatureFlagsInmutable; use CodelyTv\Flags; final class MySqlConnection { - public function __construct(private FeatureFlags $flags) + public function __construct(private FeatureFlagsInmutable $flags) { } diff --git a/examples/php/php-feature_flags-01_base/src/SubscribeController.php b/examples/php/php-feature_flags-01_base/src/SubscribeController.php index 5f6e7197..a2da0280 100644 --- a/examples/php/php-feature_flags-01_base/src/SubscribeController.php +++ b/examples/php/php-feature_flags-01_base/src/SubscribeController.php @@ -17,7 +17,7 @@ public function __invoke(Request $request) $subscribeUseCase = new Subscribe( new MySqlConnection($flags), $flags, - EmailNotifier::instance() + new EmailNotifier($flags) ); $subscribeUseCase->__invoke( @@ -26,16 +26,17 @@ public function __invoke(Request $request) ); } - private function getFeatures(Request $request): FeatureFlags + private function getFeatures(Request $request): FeatureFlagsInmutable { + $result = new FeatureFlagsInmutable(); + if (Debug::instance()->isDebugModeEnabled()) { + return $result; + } + $flagHeader = $request->headers->get('X-FLAG'); - $features = FeatureFlags::instance(); if ($flagHeader === Flags::NEW_SUBSCRIPTION_PAGE_TOKEN) { - $features->set('new_subscription_page', true); - } - if (Debug::instance()->isDebugModeEnabled()) { - $features->deactivateAll(); + return new FeatureFlagsInmutable(['new_subscription_page' => true]); } - return $features; + return $result; } } From 30921a1979317ec1824be5d64cc880da00659604 Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Mon, 1 May 2023 19:45:45 +0200 Subject: [PATCH 4/5] refactor: add initial value in FeatureFlagsInmutable --- .../php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php b/examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php index 8322b0f5..1b235b38 100644 --- a/examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php +++ b/examples/php/php-feature_flags-01_base/src/FeatureFlagsInmutable.php @@ -4,7 +4,7 @@ class FeatureFlagsInmutable { - public function __construct(private array $flags) + public function __construct(private array $flags = []) { } From 47582674b868d4a63bbb0e247a3f07daff61516a Mon Sep 17 00:00:00 2001 From: Marcos Antonio Barreche Salguero Date: Mon, 1 May 2023 19:55:37 +0200 Subject: [PATCH 5/5] refactor: delete singleton patter in Debug class --- .../php-feature_flags-01_base/src/Debug.php | 21 ++----------------- .../src/SubscribeController.php | 11 +++++++--- .../php-feature_flags-01_base/src/index.php | 4 ++-- 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/examples/php/php-feature_flags-01_base/src/Debug.php b/examples/php/php-feature_flags-01_base/src/Debug.php index 564bcb0b..9d29dabe 100644 --- a/examples/php/php-feature_flags-01_base/src/Debug.php +++ b/examples/php/php-feature_flags-01_base/src/Debug.php @@ -6,29 +6,12 @@ final class Debug { - private static ?Debug $instance = null; - - private bool $debugMode = false; - - private function __construct() - { - } - - public static function instance(): Debug - { - if (!self::$instance) { - self::$instance = new self(); - } - return self::$instance; - } - - public function enableDebugMode(): void + public function __construct(private bool $debugMode) { - $this->debugMode = true; } public function isDebugModeEnabled(): bool { - return $this->debugMode; + return $this->debugMode; } } diff --git a/examples/php/php-feature_flags-01_base/src/SubscribeController.php b/examples/php/php-feature_flags-01_base/src/SubscribeController.php index a2da0280..398378b5 100644 --- a/examples/php/php-feature_flags-01_base/src/SubscribeController.php +++ b/examples/php/php-feature_flags-01_base/src/SubscribeController.php @@ -11,9 +11,14 @@ final class SubscribeController { + public function __construct(private Debug $debug) + { + + } + public function __invoke(Request $request) { - $flags = $this->getFeatures($request); + $flags = $this->getFeatures($request, $this->debug); $subscribeUseCase = new Subscribe( new MySqlConnection($flags), $flags, @@ -26,10 +31,10 @@ public function __invoke(Request $request) ); } - private function getFeatures(Request $request): FeatureFlagsInmutable + private function getFeatures(Request $request, Debug $debug): FeatureFlagsInmutable { $result = new FeatureFlagsInmutable(); - if (Debug::instance()->isDebugModeEnabled()) { + if ($debug->isDebugModeEnabled()) { return $result; } diff --git a/examples/php/php-feature_flags-01_base/src/index.php b/examples/php/php-feature_flags-01_base/src/index.php index 21708413..4c81c5a3 100644 --- a/examples/php/php-feature_flags-01_base/src/index.php +++ b/examples/php/php-feature_flags-01_base/src/index.php @@ -8,12 +8,12 @@ use CodelyTv\SubscribeController; use Symfony\Component\HttpFoundation\Request; -Debug::instance()->enableDebugMode(); +$debug = new Debug(true); $request = Request::createFromGlobals(); $request->headers->set('X-FLAG', Flags::NEW_SUBSCRIPTION_PAGE_TOKEN); $request->request->set('email', 'email@example.com'); $request->request->set('name', 'Codely'); -$controller = new SubscribeController(); +$controller = new SubscribeController($debug); $controller->__invoke($request);