-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring Core and added 100% CodeCoverage
For SMTP CodeCoverage support, it was neccesary to communicate with MailTrapi API to confirm if mail was acutally send.
- Loading branch information
vagrant
committed
Nov 24, 2019
1 parent
9a14de8
commit 1fa041b
Showing
19 changed files
with
999 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ vendor | |
composer.lock | ||
.phpunit.result.cache | ||
tests/_report | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
/* | ||
* This document has been generated with | ||
* https://mlocati.github.io/php-cs-fixer-configurator/#version:2.15.3|configurator | ||
* you can change this configuration by importing this file. | ||
*/ | ||
return PhpCsFixer\Config::create() | ||
->setRules([ | ||
'@PSR2' => true, | ||
'blank_line_after_namespace' => true, | ||
'class_definition' => true, | ||
'elseif' => true, | ||
'encoding' => true, | ||
'full_opening_tag' => true, | ||
'function_declaration' => true, | ||
'indentation_type' => true, | ||
'lowercase_constants' => true, | ||
'lowercase_keywords' => true, | ||
'method_argument_space' => true, | ||
'no_break_comment' => true, | ||
'no_closing_tag' => true, | ||
'no_spaces_after_function_name' => true, | ||
'no_trailing_whitespace' => true, | ||
'no_trailing_whitespace_in_comment' => true, | ||
'no_extra_blank_lines' => true, | ||
'align_multiline_comment' => true, | ||
'array_syntax' => ['syntax' => 'short'], | ||
'array_indentation' => true, | ||
//'binary_operator_spaces' => true, ('=' is binary operator..) | ||
'blank_line_after_opening_tag' => true, | ||
'cast_spaces' => true, | ||
'combine_consecutive_issets' => true, | ||
'combine_consecutive_unsets' => true, | ||
'concat_space' => ['spacing' => 'one'], | ||
'explicit_indirect_variable' => true, | ||
'fully_qualified_strict_types' => true, | ||
'function_typehint_space' => true, | ||
'linebreak_after_opening_tag' => true, | ||
'lowercase_static_reference' => true, | ||
'no_blank_lines_after_class_opening' => true, | ||
'no_empty_comment' => true, | ||
'no_empty_phpdoc' => true, | ||
'no_empty_statement' => true, | ||
'no_multiline_whitespace_around_double_arrow' => true, | ||
'no_null_property_initialization' => true, | ||
'no_trailing_comma_in_singleline_array' => true, | ||
'no_useless_return' => true, | ||
'no_useless_else' => true, | ||
'no_unused_imports' => true, | ||
'ordered_imports' => true, | ||
'ordered_class_elements' => true, | ||
'single_quote' => true, | ||
'whitespace_after_comma_in_array' => true, | ||
'backtick_to_shell_exec' => true, | ||
'class_attributes_separation' => true, | ||
'no_superfluous_elseif' => true, | ||
'ternary_operator_spaces' => true, | ||
'ternary_to_null_coalescing' => true, | ||
'trailing_comma_in_multiline_array' => true, | ||
'trim_array_spaces' => true, | ||
'space_after_semicolon' => true, | ||
]) | ||
->setFinder(PhpCsFixer\Finder::create() | ||
->exclude('vendor') | ||
->in(__DIR__) | ||
) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
<?php | ||
|
||
namespace IWasHereFirst2\LaravelMultiMail; | ||
|
||
class Config | ||
{ | ||
/** | ||
* Name from mail sender. | ||
* | ||
* @var string | ||
*/ | ||
protected $name; | ||
|
||
/** | ||
* Email from mail sender. | ||
* Has to be set in `config/multimail.php` | ||
* | ||
* @var string | ||
*/ | ||
protected $email; | ||
|
||
/** | ||
* Driver, Host, Port & Encryption. | ||
* | ||
* @var array | ||
*/ | ||
protected $provider; | ||
|
||
/** | ||
* Email settings. | ||
* This may include credentials, name, provider. | ||
* | ||
* @var [type] | ||
*/ | ||
protected $settings; | ||
|
||
/** | ||
* Load config settings by key | ||
* | ||
* @param mixed Either string of email, or array of form ['email' => .., 'name' => .. ] | ||
*/ | ||
public function __construct($key) | ||
{ | ||
// Retreive email | ||
$this->parseEmail($key); | ||
|
||
$this->loadConfiguration(); | ||
|
||
// If credentials are empty, load default values. | ||
// This makes local testing for many emails | ||
// very convenient. | ||
if ($this->isEmpty()) { | ||
$this->loadDefault(); | ||
} | ||
} | ||
|
||
/** | ||
* Check if log driver is used. | ||
* | ||
* @return boolean | ||
*/ | ||
public function isLogDriver() | ||
{ | ||
return (isset($this->provider['driver']) && $this->provider['driver'] == 'log'); | ||
} | ||
|
||
/** | ||
* Get provider. | ||
* | ||
* @return array | ||
*/ | ||
public function getProvider() | ||
{ | ||
return $this->provider; | ||
} | ||
|
||
/** | ||
* Get setting. | ||
* | ||
* @return array | ||
*/ | ||
public function getSetting() | ||
{ | ||
return $this->settings; | ||
} | ||
|
||
/** | ||
* Return email of sender. | ||
* | ||
* @return string | ||
*/ | ||
public function getFromEmail() | ||
{ | ||
return $this->settings['from_mail'] ?? $this->email; | ||
} | ||
|
||
/** | ||
* Return name of sender. | ||
* | ||
* @return string | ||
*/ | ||
public function getFromName() | ||
{ | ||
return $this->name; | ||
} | ||
|
||
/** | ||
* Return email of sender. | ||
* | ||
* @return string | ||
*/ | ||
public function getReplyEmail() | ||
{ | ||
return $this->settings['reply_to_mail'] ?? null; | ||
} | ||
|
||
/** | ||
* Return name of sender. | ||
* | ||
* @return string | ||
*/ | ||
public function getReplyName() | ||
{ | ||
return $this->settings['reply_to_name'] ?? null; | ||
} | ||
|
||
/** | ||
* Parse $key into email and possible from name | ||
* | ||
* @param mixed string/array | ||
* @return void | ||
*/ | ||
protected function parseEmail($key) | ||
{ | ||
if (is_array($key)) { | ||
$this->name = $key['name'] ?? null; | ||
|
||
if (empty($key['email'])) { | ||
throw new Exceptions\InvalidConfigKeyException; | ||
} | ||
$this->email = $key['email']; | ||
} else { | ||
$this->email = $key; | ||
} | ||
} | ||
|
||
/** | ||
* Load config settings and provder from email | ||
* | ||
* @return void | ||
*/ | ||
protected function loadConfiguration() | ||
{ | ||
try { | ||
$this->settings = config('multimail.emails')[$this->email]; | ||
} catch (\Exception $e) { | ||
throw new Exceptions\EmailNotInConfigException($this->email); | ||
} | ||
|
||
if (empty($this->name)) { | ||
$this->name = $this->settings['from_name'] ?? null; | ||
} | ||
|
||
$this->loadProvider(); | ||
} | ||
|
||
protected function loadProvider() | ||
{ | ||
if (isset($this->settings['provider']) && !empty($provider = $this->settings['provider'])) { | ||
$this->provider = config('multimail.provider.' . $provider); | ||
} | ||
|
||
if (empty($this->provider)) { | ||
$this->provider = config('multimail.provider.default'); | ||
} | ||
} | ||
|
||
/** | ||
* Check if email, pass and username are not empty | ||
* | ||
* @return boolean | ||
*/ | ||
protected function isEmpty() | ||
{ | ||
return (empty($this->email) || empty($this->settings) || empty($this->settings['pass']) || empty($this->settings['username'])); | ||
} | ||
|
||
/** | ||
* Load default setting. If default setting is | ||
* invalid throw exception | ||
* | ||
* @return void | ||
*/ | ||
protected function loadDefault() | ||
{ | ||
$this->settings = config('multimail.emails.default'); | ||
|
||
$this->loadProvider(); | ||
|
||
if ($this->provider['driver'] != 'log' && (empty($this->settings) || empty($this->settings['pass']) || empty($this->settings['username']))) { | ||
throw new Exceptions\NoDefaultException($this->email); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace IWasHereFirst2\LaravelMultiMail\Exceptions; | ||
|
||
class EmailNotInConfigException extends \Exception | ||
{ | ||
public function __construct($mail) | ||
{ | ||
$this->message = 'Email ' . $mail . ' not found in config/multimail.php!'; | ||
parent::__construct(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace IWasHereFirst2\LaravelMultiMail\Exceptions; | ||
|
||
class InvalidConfigKeyException extends \Exception | ||
{ | ||
public function __construct() | ||
{ | ||
$this->message = "Mailer name has to be provided in array as column 'email'"; | ||
parent::__construct(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace IWasHereFirst2\LaravelMultiMail\Exceptions; | ||
|
||
class NoDefaultException extends \Exception | ||
{ | ||
public function __construct($mail) | ||
{ | ||
$this->message = 'Username or password for ' . $mail . ' is missing in config/multimail.php and no default specified!'; | ||
parent::__construct(); | ||
} | ||
} |
Oops, something went wrong.