Skip to content

Commit

Permalink
Refactoring Core and added 100% CodeCoverage
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 19 changed files with 999 additions and 184 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ vendor
composer.lock
.phpunit.result.cache
tests/_report
.env
67 changes: 67 additions & 0 deletions .php_cs.dist
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__)
)
;
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"laravel/framework": "^5.6.0|^6.0.0"
},
"require-dev": {
"orchestra/testbench": "^4.0"
"orchestra/testbench": "^4.0",
"guzzlehttp/guzzle": "^6.4"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>

<testsuite name="Integration">
<directory suffix="Test.php">./tests/Integration</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
Expand Down
204 changes: 204 additions & 0 deletions src/Config.php
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);
}
}
}
12 changes: 12 additions & 0 deletions src/Exceptions/EmailNotInConfigException.php
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();
}
}
12 changes: 12 additions & 0 deletions src/Exceptions/InvalidConfigKeyException.php
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();
}
}
12 changes: 12 additions & 0 deletions src/Exceptions/NoDefaultException.php
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();
}
}
Loading

0 comments on commit 1fa041b

Please sign in to comment.