Skip to content

Commit

Permalink
Added trait for raising exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Oct 4, 2024
1 parent 9e9ce05 commit 99ef8cd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 28 deletions.
42 changes: 14 additions & 28 deletions src/ZugferdMailReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@
namespace horstoeko\zugferdmail;

use Throwable;
use horstoeko\zugferd\ZugferdDocument;
use horstoeko\zugferd\ZugferdDocumentPdfReader;
use Webklex\PHPIMAP\Folder;
use Webklex\PHPIMAP\Message;
use Webklex\PHPIMAP\Attachment;
use Webklex\PHPIMAP\ClientManager;
use horstoeko\zugferd\ZugferdDocumentReader;
use horstoeko\zugferdmail\concerns\ZugferdMailClearsMessageBag;
use horstoeko\zugferdmail\concerns\ZugferdMailReceivesMessagesFromMessageBag;
use horstoeko\zugferdmail\concerns\ZugferdMailSendsMessagesToMessageBag;
use horstoeko\zugferdmail\config\ZugferdMailAccount;
use horstoeko\zugferd\ZugferdDocumentPdfReader;
use horstoeko\zugferdmail\config\ZugferdMailConfig;
use horstoeko\zugferdmail\consts\ZugferdMailReaderRecognitionType;
use horstoeko\zugferdmail\config\ZugferdMailAccount;
use horstoeko\zugferdublbridge\XmlConverterUblToCii;
use RuntimeException;
use Webklex\PHPIMAP\Attachment;
use Webklex\PHPIMAP\ClientManager;
use Webklex\PHPIMAP\Folder;
use Webklex\PHPIMAP\Message;
use horstoeko\zugferdmail\concerns\ZugferdMailClearsMessageBag;
use horstoeko\zugferdmail\concerns\ZugferdMailRaisesExceptions;
use horstoeko\zugferdmail\consts\ZugferdMailReaderRecognitionType;
use horstoeko\zugferdmail\concerns\ZugferdMailSendsMessagesToMessageBag;
use horstoeko\zugferdmail\concerns\ZugferdMailReceivesMessagesFromMessageBag;

/**
* Class representing the mail reader
Expand All @@ -39,7 +38,8 @@ class ZugferdMailReader
{
use ZugferdMailSendsMessagesToMessageBag,
ZugferdMailReceivesMessagesFromMessageBag,
ZugferdMailClearsMessageBag;
ZugferdMailClearsMessageBag,
ZugferdMailRaisesExceptions;

/**
* The config
Expand Down Expand Up @@ -191,7 +191,7 @@ protected function checkSingleMessageAttachment(ZugferdMailAccount $account, Fol
try {
$this->addLogMessageToMessageBag('Checking for ZUGFeRD compatible PDF', $messageAdditionalData);
$document = ZugferdDocumentPdfReader::readAndGuessFromContent($attachment->getContent());
$this->runtimeExceptionIf(is_null($document), "No document returned");
$this->raiseRuntimeExceptionIf(is_null($document), "No document returned");
$this->addSuccessMessageToMessageBag('Mail contains a ZUGFeRD compatible PDF', $messageAdditionalData);
$this->triggerHandlers($account, $folder, $message, $attachment, $document, ZugferdMailReaderRecognitionType::ZFMAIL_RECOGNITION_TYPE_PDF_CII);
} catch (Throwable $e) {
Expand Down Expand Up @@ -249,18 +249,4 @@ protected function triggerHandlers(ZugferdMailAccount $account, Folder $folder,
}
}
}

/**
* Raise exception if $condition is evaludated to trze
*
* @param boolean $condidition
* @param string $message
* @return void
*/
protected function runtimeExceptionIf(bool $condidition, string $message = ""): void
{
if ($condidition === true) {
throw new RuntimeException($message);
}
}
}
54 changes: 54 additions & 0 deletions src/concerns/ZugferdMailRaisesExceptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* This file is a part of horstoeko/zugferdmail.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace horstoeko\zugferdmail\concerns;

use RuntimeException;

/**
* Trait representing general facillities to raise
* exceptions of several types
*
* @category ZugferdMail
* @package ZugferdMail
* @author D. Erling <horstoeko@erling.com.de>
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/horstoeko/zugferdmail
*/
trait ZugferdMailRaisesExceptions
{
/**
* Raise exception defined by $class if $condition is evaludated to trze
*
* @param boolean $condidition
* @param string $exceptionClass
* @param string $message
* @return void
*/
protected function raiseExceptionClassIf(bool $condidition, string $exceptionClass, string $message = "")
{
if ($condidition !== true) {
return;
}

throw new $exceptionClass($message);
}

/**
* Raise Runtime Exception if $condition is evaludated to trze
*
* @param boolean $condidition
* @param string $message
* @return void
*/
protected function raiseRuntimeExceptionIf(bool $condidition, string $message = ""): void
{
$this->raiseExceptionClassIf($condidition, RuntimeException::class, $message);
}
}

0 comments on commit 99ef8cd

Please sign in to comment.