Skip to content

Commit

Permalink
Reintroduce NullDriver From v4 (#323)
Browse files Browse the repository at this point in the history
* Reintroduce NullDriver From v4

* Update composer.json

---------

Co-authored-by: Keller Martin <[email protected]>
Co-authored-by: Freek Van der Herten <[email protected]>
  • Loading branch information
3 people authored Jul 18, 2024
1 parent da64b53 commit 0507ee5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 25 additions & 0 deletions src/Drivers/NullDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Spatie\Newsletter\Drivers;

use Illuminate\Support\Facades\Log;

class NullDriver
{
/**
* @var bool
*/
private $logCalls;

public function __construct(bool $logCalls = false)
{
$this->logCalls = $logCalls;
}

public function __call($name, $arguments)
{
if ($this->logCalls) {
Log::debug('Called Spatie Newsletter facade method: '.$name.' with:', $arguments);
}
}
}
9 changes: 9 additions & 0 deletions src/NewsletterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +23,14 @@ public function bootingPackage()
/** @var class-string<Driver> $driverClass */
$driverClass = config('newsletter.driver');

if (
is_null($driverClass)

Check failure on line 27 in src/NewsletterServiceProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to function is_null() with class-string<Spatie\Newsletter\Drivers\Driver> will always evaluate to false.
|| $driverClass === 'log'
|| $driverClass === NullDriver::class
) {
return new NullDriver($driverClass === 'log');
}

$arguments = config('newsletter.driver_arguments');
$lists = Lists::createFromConfig(config('newsletter'));

Expand Down
27 changes: 27 additions & 0 deletions tests/Drivers/NullDriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Spatie\Newsletter\Drivers\NullDriver;

it('uses the NullDriver with null config value', function () {
config()->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('[email protected]'))->toBeNull();
});

0 comments on commit 0507ee5

Please sign in to comment.