diff --git a/composer.json b/composer.json index 0472033..9a76834 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ ], "require": { "php": "^8.1", - "spatie/laravel-package-tools": "^1.13" + "spatie/laravel-package-tools": "^1.13", + "illuminate/log": "^9|^10|^11" }, "suggest": { "spatie/mailcoach-sdk-php": "For working with Mailcoach", diff --git a/src/Drivers/NullDriver.php b/src/Drivers/NullDriver.php new file mode 100644 index 0000000..6fb5ac6 --- /dev/null +++ b/src/Drivers/NullDriver.php @@ -0,0 +1,25 @@ +logCalls = $logCalls; + } + + public function __call($name, $arguments) + { + if ($this->logCalls) { + Log::debug('Called Spatie Newsletter facade method: '.$name.' with:', $arguments); + } + } +} diff --git a/src/NewsletterServiceProvider.php b/src/NewsletterServiceProvider.php index 6b1c1e2..6e95cf8 100644 --- a/src/NewsletterServiceProvider.php +++ b/src/NewsletterServiceProvider.php @@ -5,6 +5,7 @@ use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; use Spatie\Newsletter\Drivers\Driver; +use Spatie\Newsletter\Drivers\NullDriver; use Spatie\Newsletter\Support\Lists; class NewsletterServiceProvider extends PackageServiceProvider @@ -22,6 +23,14 @@ public function bootingPackage() /** @var class-string $driverClass */ $driverClass = config('newsletter.driver'); + if ( + is_null($driverClass) + || $driverClass === 'log' + || $driverClass === NullDriver::class + ) { + return new NullDriver($driverClass === 'log'); + } + $arguments = config('newsletter.driver_arguments'); $lists = Lists::createFromConfig(config('newsletter')); diff --git a/tests/Drivers/NullDriverTest.php b/tests/Drivers/NullDriverTest.php new file mode 100644 index 0000000..c55c214 --- /dev/null +++ b/tests/Drivers/NullDriverTest.php @@ -0,0 +1,27 @@ +set('newsletter.driver', null); + + expect(app('newsletter'))->toBeInstanceOf(NullDriver::class); +}); + +it('uses the NullDriver with log config value', function () { + config()->set('newsletter.driver', 'log'); + + expect(app('newsletter'))->toBeInstanceOf(NullDriver::class); +}); + +it('uses the NullDriver with NullDriver::class config', function () { + config()->set('newsletter.driver', NullDriver::class); + + expect(app('newsletter'))->toBeInstanceOf(NullDriver::class); +}); + +it('accepts calls to the NullDriver', function () { + config()->set('newsletter.driver', null); + + expect(app('newsletter')->subscribe('example@example.org'))->toBeNull(); +});