From 34188d20594cc994d65a496c8cf2f666a837491f Mon Sep 17 00:00:00 2001 From: dietercoopman Date: Fri, 5 Aug 2022 07:01:51 +0000 Subject: [PATCH] Fix styling --- src/Mailspfchecker.php | 55 +++++++++++++-------------- src/MailspfcheckerServiceProvider.php | 2 +- tests/TestCase.php | 2 +- 3 files changed, 29 insertions(+), 30 deletions(-) diff --git a/src/Mailspfchecker.php b/src/Mailspfchecker.php index f4e4cef..1367f3b 100755 --- a/src/Mailspfchecker.php +++ b/src/Mailspfchecker.php @@ -6,12 +6,12 @@ use SPFLib\Decoder; use SPFLib\Term\Mechanism; - class Mailspfchecker { - private array $spfRecords = []; - private string $server = ""; + + private string $server = ''; + private string $sendingMailserver; public function __construct() @@ -21,13 +21,14 @@ public function __construct() public function canISendAs(string $emailOrDomain): bool { - return ($this->check($emailOrDomain, true) === "pass"); + return $this->check($emailOrDomain, true) === 'pass'; } public function using(string $server): self { $this->server = $server; $this->setSendingMailServer(); + return $this; } @@ -38,76 +39,72 @@ private function getDomain(string $emailOrDomain): string } else { $domain = $emailOrDomain; } + return $domain; } public function check(string $emailOrDomain, $returnCode = false): mixed { - $domain = $this->getDomain($emailOrDomain); + $domain = $this->getDomain($emailOrDomain); $decoder = new \SPFLib\Decoder(); - $code = "error"; + $code = 'error'; $codes = []; - foreach ($this->spfRecords as $spfValueToCheck) { $record = $decoder->getRecordFromDomain($domain); if ($record) { foreach ($record->getTerms() as $term) { - $codes[$spfValueToCheck] = "error"; + $codes[$spfValueToCheck] = 'error'; if (strstr(strtolower($term), strtolower($spfValueToCheck))) { - $codes[$spfValueToCheck] = "pass"; + $codes[$spfValueToCheck] = 'pass'; break; } } } else { - $codes[$spfValueToCheck] = "error"; + $codes[$spfValueToCheck] = 'error'; } } - if (in_array("pass", $codes)) { - $code = "pass"; + if (in_array('pass', $codes)) { + $code = 'pass'; } return ($returnCode) ? $code : $codes; - } private function setSendingMailServer(): void { - if (!blank($this->server)) { + if (! blank($this->server)) { $sendingMailserver = $this->server; } else { $sendingMailserver = config('mail.mailers.smtp.host'); } //if the address is localhost, then check wan address via icanhazip - if ($sendingMailserver == "127.0.0.1" || $sendingMailserver == "localhost") { - $sendingMailserver = trim(file_get_contents("https://icanhazip.com/")); + if ($sendingMailserver == '127.0.0.1' || $sendingMailserver == 'localhost') { + $sendingMailserver = trim(file_get_contents('https://icanhazip.com/')); } $this->sendingMailserver = $sendingMailserver; - $this->spfRecords = $this->retreiveSpfRecordsFromSendingServer(); - - + $this->spfRecords = $this->retreiveSpfRecordsFromSendingServer(); } private function retreiveSpfRecordsFromSendingServer(): array { $spfRecords = []; - $server = $this->sendingMailserver; + $server = $this->sendingMailserver; - if (!filter_var($server, FILTER_VALIDATE_IP)) { - - $checker = new Decoder(); + if (! filter_var($server, FILTER_VALIDATE_IP)) { + $checker = new Decoder(); $explodedServerUrl = explode('.', $server); array_shift($explodedServerUrl); - $domain = implode(".", $explodedServerUrl); + $domain = implode('.', $explodedServerUrl); if ($domain) { $record = $checker->getRecordFromDomain($domain); foreach ($record->getTerms() as $term) { if ($term instanceof Mechanism\IncludeMechanism || $term instanceof Mechanism\AMechanism) { - $domainSpec = (string)$term->getDomainSpec(); + $domainSpec = (string) $term->getDomainSpec(); if (strstr($domainSpec, $domain)) { $spfRecords[] = $domainSpec; } @@ -115,15 +112,17 @@ private function retreiveSpfRecordsFromSendingServer(): array } } } + return array_unique($spfRecords); } public function howCanISendAs(string $emailOrDomain, string $overRuleMessage = null): string { - list($name, $value) = array_values($this->buildDnsString($emailOrDomain)); + [$name, $value] = array_values($this->buildDnsString($emailOrDomain)); if ($overRuleMessage) { return str_replace([':name', ':value'], [$name, $value], $overRuleMessage); } + return "Generate a txt-record with a name of {$name} and the value {$value}"; } @@ -131,7 +130,7 @@ public function buildDnsString(string $emailOrDomain): array { $domain = $this->getDomain($emailOrDomain); $record = new \SPFLib\Record(); - if (!empty($this->spfRecords)) { + if (! empty($this->spfRecords)) { foreach ($this->spfRecords as $server) { if (filter_var($server, FILTER_VALIDATE_IP)) { $record->addTerm(new Mechanism\Ip4Mechanism(Mechanism::QUALIFIER_PASS, IPv4::parseString($server))); @@ -145,6 +144,6 @@ public function buildDnsString(string $emailOrDomain): array $record->addTerm(new Mechanism\AllMechanism(Mechanism::QUALIFIER_FAIL)); - return ["name" => $domain, "value" => (string)$record]; + return ['name' => $domain, 'value' => (string) $record]; } } diff --git a/src/MailspfcheckerServiceProvider.php b/src/MailspfcheckerServiceProvider.php index f9bfd34..bb74f9c 100644 --- a/src/MailspfcheckerServiceProvider.php +++ b/src/MailspfcheckerServiceProvider.php @@ -2,9 +2,9 @@ namespace Dietercoopman\Mailspfchecker; +use Dietercoopman\Mailspfchecker\Commands\MailspfcheckerCommand; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; -use Dietercoopman\Mailspfchecker\Commands\MailspfcheckerCommand; class MailspfcheckerServiceProvider extends PackageServiceProvider { diff --git a/tests/TestCase.php b/tests/TestCase.php index b301af4..740f159 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,9 +2,9 @@ namespace Dietercoopman\Mailspfchecker\Tests; +use Dietercoopman\Mailspfchecker\MailspfcheckerServiceProvider; use Illuminate\Database\Eloquent\Factories\Factory; use Orchestra\Testbench\TestCase as Orchestra; -use Dietercoopman\Mailspfchecker\MailspfcheckerServiceProvider; class TestCase extends Orchestra {