Skip to content

Commit

Permalink
Merge pull request #9 from elbgoods/laravel-guzzle-v2
Browse files Browse the repository at this point in the history
upgrade laravel-guzzle:2.0.0
  • Loading branch information
Gummibeer authored Mar 2, 2020
2 parents 2a22d51 + 02182c4 commit 0e34850
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ jobs:
- name: phpunit
run: vendor/bin/phpunit
env:
VERIFIER_API_KEY: ${{ secrets.VERIFIER_API_KEY }}

- name: php-cs-test
run: vendor/bin/php-cs-test
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this package will be documented in this file.

## v0.5.0

* upgrade `astrotomic/laravel-guzzle` to v2.0.0

## v0.4.0

* add https://verifier.meetchopra.com provider `\Elbgoods\TrashmailRule\Providers\VerifierProvider`
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
],
"require": {
"php": "^7.4",
"astrotomic/laravel-guzzle": "^1.0.1",
"astrotomic/laravel-guzzle": "^2.0",
"illuminate/cache": "^6.0",
"illuminate/support": "^6.0"
},
Expand Down
13 changes: 1 addition & 12 deletions config/trashmail.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use GuzzleHttp\RequestOptions;

return [
/*
* The list of providers that should run to decide whether an email is disposable or not.
Expand All @@ -25,30 +23,21 @@
'key' => 'elbgoods.trashmail.dead_letter',
'ttl' => 60 * 60 * 24, // one day
],
'guzzle' => [
RequestOptions::TIMEOUT => 10,
],
],

/*
* This package can do a request to https://www.disposable-email-detector.com
*/
'disposable_email_detector' => [
'enabled' => false,
'guzzle' => [
RequestOptions::TIMEOUT => 5,
],
],

/*
* This package can do a request to https://verifier.meetchopra.com
*/
'verifier' => [
'enabled' => false,
'api_key' => null,
'guzzle' => [
RequestOptions::TIMEOUT => 5,
],
'api_key' => env('VERIFIER_API_KEY'),
],

/*
Expand Down
9 changes: 3 additions & 6 deletions src/Providers/DeadLetterProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

namespace Elbgoods\TrashmailRule\Providers;

use Astrotomic\LaravelGuzzle\Facades\Guzzle;
use Elbgoods\TrashmailRule\Contracts\ProviderContract;
use Illuminate\Contracts\Cache\Factory as CacheFactory;
use Illuminate\Contracts\Cache\Repository as CacheRepository;

class DeadLetterProvider implements ProviderContract
{
protected const BLACKLIST_URL = 'https://www.dead-letter.email/blacklist_flat.json';

protected array $config;
protected CacheFactory $cache;

Expand Down Expand Up @@ -59,10 +58,8 @@ protected function getBlacklist(): array

protected function loadDeadLetter(): array
{
$response = guzzle(
self::BLACKLIST_URL,
$this->config['guzzle']
)->request('GET', '');
$response = Guzzle::client('dead-letter.email')
->request('GET', 'blacklist_flat.json');

$body = $response->getBody()->getContents();

Expand Down
9 changes: 3 additions & 6 deletions src/Providers/DisposableEmailDetectorProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Elbgoods\TrashmailRule\Providers;

use Astrotomic\LaravelGuzzle\Facades\Guzzle;
use Elbgoods\TrashmailRule\Contracts\ProviderContract;

class DisposableEmailDetectorProvider implements ProviderContract
{
protected const BASE_URL = 'https://api.disposable-email-detector.com/api/dea/v1/check/';

protected array $config;

public function __construct(array $config)
Expand All @@ -21,10 +20,8 @@ public function isDisposable(string $domain): ?bool
return null;
}

$response = guzzle(
self::BASE_URL,
$this->config['guzzle']
)->request('GET', $domain);
$response = Guzzle::client('api.disposable-email-detector.com')
->request('GET', 'api/dea/v1/check/'.urlencode($domain));

$body = $response->getBody()->getContents();

Expand Down
17 changes: 7 additions & 10 deletions src/Providers/VerifierProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

namespace Elbgoods\TrashmailRule\Providers;

use Astrotomic\LaravelGuzzle\Facades\Guzzle;
use Elbgoods\TrashmailRule\Contracts\ProviderContract;

class VerifierProvider implements ProviderContract
{
protected const BASE_URL = 'https://verifier.meetchopra.com/verify/';

protected array $config;

public function __construct(array $config)
Expand All @@ -25,14 +24,12 @@ public function isDisposable(string $domain): ?bool
return null;
}

$response = guzzle(
self::BASE_URL,
$this->config['guzzle']
)->request('GET', $domain, [
'query' => [
'token' => $this->config['api_key'],
],
]);
$response = Guzzle::client('verifier.meetchopra.com')
->request('GET', 'verify/'.urlencode($domain), [
'query' => [
'token' => $this->config['api_key'],
],
]);

$body = $response->getBody()->getContents();

Expand Down
23 changes: 23 additions & 0 deletions src/TrashmailRuleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Elbgoods\TrashmailRule;

use Astrotomic\LaravelGuzzle\Facades\Guzzle;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\ServiceProvider;

class TrashmailRuleServiceProvider extends ServiceProvider
Expand All @@ -12,6 +14,27 @@ public function boot(): void
$this->bootConfig();
$this->bootLang();
}

Guzzle::register('dead-letter.email', [
'base_uri' => 'https://www.dead-letter.email',
RequestOptions::TIMEOUT => 10,
RequestOptions::ALLOW_REDIRECTS => true,
RequestOptions::HTTP_ERRORS => true,
]);

Guzzle::register('api.disposable-email-detector.com', [
'base_uri' => 'https://api.disposable-email-detector.com',
RequestOptions::TIMEOUT => 5,
RequestOptions::ALLOW_REDIRECTS => true,
RequestOptions::HTTP_ERRORS => true,
]);

Guzzle::register('verifier.meetchopra.com', [
'base_uri' => 'https://verifier.meetchopra.com',
RequestOptions::TIMEOUT => 5,
RequestOptions::ALLOW_REDIRECTS => true,
RequestOptions::HTTP_ERRORS => true,
]);
}

public function register(): void
Expand Down
36 changes: 36 additions & 0 deletions tests/Rules/TrashmailRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,42 @@ public function it_passes_whitelist_addresses(): void
$this->assertTrue($rule->passes('email', 'example@'.$domain));
}

/** @test */
public function dead_letter_fails_with_disposable_email(): void
{
$this->app['config']->set('trashmail.dead_letter.enabled', true);

$rule = new TrashmailRule();

$this->assertFalse($rule->passes('email', '[email protected]'));
}

/** @test */
public function disposable_email_fails_with_disposable_email(): void
{
$this->app['config']->set('trashmail.disposable_email_detector.enabled', true);

$rule = new TrashmailRule();

$this->assertFalse($rule->passes('email', '[email protected]'));
}

/** @test */
public function verifier_fails_with_disposable_email(): void
{
if ($this->app['config']->get('trashmail.verifier.api_key') === null) {
$this->markTestSkipped('Verifier requires an API-Key');

return;
}

$this->app['config']->set('trashmail.verifier.enabled', true);

$rule = new TrashmailRule();

$this->assertFalse($rule->passes('email', '[email protected]'));
}

public function provideTrashMailDomain(): array
{
return array_map(static function (string $domain): array {
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Elbgoods\TrashmailRule\Tests;

use Astrotomic\LaravelGuzzle\LaravelGuzzleServiceProvider;
use Astrotomic\LaravelGuzzle\GuzzleServiceProvider;
use Elbgoods\TrashmailRule\TrashmailRuleServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

Expand All @@ -11,7 +11,7 @@ abstract class TestCase extends Orchestra
protected function getPackageProviders($app)
{
return [
LaravelGuzzleServiceProvider::class,
GuzzleServiceProvider::class,
TrashmailRuleServiceProvider::class,
];
}
Expand Down

0 comments on commit 0e34850

Please sign in to comment.