Skip to content

Commit

Permalink
Some code refactoring and changelog updates
Browse files Browse the repository at this point in the history
  • Loading branch information
papnoisanjeev committed Dec 19, 2024
1 parent efede9c commit bf98efc
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 43 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG-1.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ CHANGELOG for 1.1.x

This changelog references any relevant changes introduced in 1.1 minor versions.

* 1.1.4
* 1.1.4 (2024-12-19)
* Microsoft Modern App related updates.
* License and support email address updates.
* Code refactoring.

* 1.1.3 (2023-06-12)
* Update: Dropped dependency on uvdesk/composer-plugin in support of symfony/flex
Expand Down
28 changes: 17 additions & 11 deletions Controller/MailboxChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function createMailboxConfiguration(Request $request, EntityManagerInterf
$imapConfiguration = null;

// IMAP Configuration
if (!empty($params['imap']['transport'])) {
if (! empty($params['imap']['transport'])) {
$imapConfiguration = IMAP\Configuration::createTransportDefinition($params['imap']['transport'], !empty($params['imap']['host']) ? trim($params['imap']['host'], '"') : null);

if ($imapConfiguration instanceof IMAP\Transport\AppTransportConfigurationInterface) {
Expand Down Expand Up @@ -107,7 +107,10 @@ public function createMailboxConfiguration(Request $request, EntityManagerInterf
}

// SMTP Configuration
if (!empty($params['smtp']['transport']) && 'swiftmailer_id' !== $params['smtp']['transport']) {
if (
! empty($params['smtp']['transport'])
&& 'swiftmailer_id' !== $params['smtp']['transport']
) {
$smtpConfiguration = SMTP\Configuration::createTransportDefinition($params['smtp']['transport'], !empty($params['smtp']['host']) ? trim($params['smtp']['host'], '"') : null);

if ($smtpConfiguration instanceof SMTP\Transport\AppTransportConfigurationInterface) {
Expand Down Expand Up @@ -164,8 +167,8 @@ public function createMailboxConfiguration(Request $request, EntityManagerInterf
} else {
$mailboxConfiguration = $mailboxService->parseMailboxConfigurations();

// Swiftmailer Configuration
if (!empty($params['swiftmailer_id'])) {
// SwiftMailer Configuration
if (! empty($params['swiftmailer_id'])) {
foreach ($swiftMailerConfigurationCollection as $configuration) {
if ($configuration->getId() == $params['swiftmailer_id']) {
$swiftmailerConfiguration = $configuration;
Expand Down Expand Up @@ -224,7 +227,7 @@ public function createMailboxConfiguration(Request $request, EntityManagerInterf

public function updateMailboxConfiguration($id, Request $request, EntityManagerInterface $entityManager, UserService $userService, MailboxService $mailboxService, TranslatorInterface $translator, SwiftMailerService $swiftMailer)
{
if (!$userService->isAccessAuthorized('ROLE_ADMIN')) {
if (! $userService->isAccessAuthorized('ROLE_ADMIN')) {
return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
}

Expand Down Expand Up @@ -310,7 +313,10 @@ public function updateMailboxConfiguration($id, Request $request, EntityManagerI
}

// SMTP Configuration
if (!empty($params['smtp']['transport']) && 'swiftmailer_id' !== $params['smtp']['transport']) {
if (
! empty($params['smtp']['transport'])
&& 'swiftmailer_id' !== $params['smtp']['transport']
) {
$smtpConfiguration = SMTP\Configuration::createTransportDefinition($params['smtp']['transport'], !empty($params['smtp']['host']) ? trim($params['smtp']['host'], '"') : null);

if ($smtpConfiguration instanceof SMTP\Transport\AppTransportConfigurationInterface) {
Expand Down Expand Up @@ -366,7 +372,7 @@ public function updateMailboxConfiguration($id, Request $request, EntityManagerI

$mailbox = new Mailbox($params['id']);

// Swiftmailer Configuration
// SwiftMailer Configuration
if (! empty($params['swiftmailer_id'])) {
foreach ($swiftMailerConfigurationCollection as $configuration) {
if ($configuration->getId() == $params['swiftmailer_id']) {
Expand All @@ -380,27 +386,27 @@ public function updateMailboxConfiguration($id, Request $request, EntityManagerI
->setName($params['name'])
->setIsEnabled(!empty($params['isEnabled']) && 'on' == $params['isEnabled'] ? true : false);

if (!empty($imapConfiguration)) {
if (! empty($imapConfiguration)) {
$mailbox
->setImapConfiguration($imapConfiguration)
;
}

if (!empty($smtpConfiguration)) {
if (! empty($smtpConfiguration)) {
$mailbox
->setSmtpConfiguration($smtpConfiguration)
;
}

if (!empty($swiftmailerConfiguration) && empty($smtpConfiguration)) {
if (! empty($swiftmailerConfiguration) && empty($smtpConfiguration)) {
$mailbox
->setSwiftMailerConfiguration($swiftmailerConfiguration);
;
}

$mailboxConfiguration->addMailbox($mailbox);

if (!empty($params['isDefault']) && 'on' == $params['isDefault']) {
if (! empty($params['isDefault']) && 'on' == $params['isDefault']) {
$mailboxConfiguration
->setDefaultMailbox($mailbox)
;
Expand Down
2 changes: 1 addition & 1 deletion Controller/MailboxChannelXHR.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function processOutlookMailXHR(Request $request, MailboxService $mailboxS
return new JsonResponse([
'success' => false,
'message' => $e->getMessage(),
'params' => $request->get('email')
'params' => $request->get('email')
], 500);
}

Expand Down
59 changes: 35 additions & 24 deletions Services/MailboxService.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ public function parseMailboxConfigurations(bool $ignoreInvalidAttributes = false
// SMTP Configuration
$smtpConfiguration = null;

if (!empty($params['smtp_server']) && !isset($params['smtp_server']['mailer_id'])) {
if (
! empty($params['smtp_server'])
&& !isset($params['smtp_server']['mailer_id'])
) {
$smtpConfiguration = SMTP\Configuration::guessTransportDefinition($params['smtp_server']);

if ($smtpConfiguration instanceof SMTP\Transport\AppTransportConfigurationInterface) {
Expand All @@ -127,7 +130,7 @@ public function parseMailboxConfigurations(bool $ignoreInvalidAttributes = false
->setPassword($params['smtp_server']['password'])
;

if (!empty($params['smtp_server']['sender_address'])) {
if (! empty($params['smtp_server']['sender_address'])) {
$smtpConfiguration
->setSenderAddress($params['smtp_server']['sender_address'])
;
Expand All @@ -140,13 +143,13 @@ public function parseMailboxConfigurations(bool $ignoreInvalidAttributes = false
->setName($params['name'])
->setIsEnabled($params['enabled']);

if (!empty($imapConfiguration)) {
if (! empty($imapConfiguration)) {
$mailbox
->setImapConfiguration($imapConfiguration)
;
}

if (!empty($smtpConfiguration)) {
if (! empty($smtpConfiguration)) {
$mailbox
->setSmtpConfiguration($smtpConfiguration)
;
Expand Down Expand Up @@ -280,8 +283,8 @@ private function searchTicketSubjectReference($senderEmail, $messageSubject) {

// Search Criteria: Find ticket based on subject
if (
!empty($senderEmail)
&& !empty($messageSubject)
! empty($senderEmail)
&& ! empty($messageSubject)
) {
$threadRepository = $this->entityManager->getRepository(Thread::class);
$ticket = $threadRepository->findTicketBySubject($senderEmail, $messageSubject);
Expand Down Expand Up @@ -318,7 +321,7 @@ private function searchExistingTickets(array $criterias = [])
} else {
$thread = $threadRepository->findOneByMessageId($criteriaValue);

if (!empty($thread)) {
if (! empty($thread)) {
return $thread->getTicket();
}
}
Expand Down Expand Up @@ -377,10 +380,10 @@ public function processMail($rawEmail)

$from = $this->parseAddress('from') ?: $this->parseAddress('sender');
$addresses = [
'from' => $this->getEmailAddress($from),
'to' => empty($this->parseAddress('X-Forwarded-To')) ? $this->parseAddress('to') : $this->parseAddress('X-Forwarded-To'),
'cc' => $this->parseAddress('cc'),
'delivered-to' => $this->parseAddress('delivered-to'),
'from' => $this->getEmailAddress($from),
'to' => empty($this->parseAddress('X-Forwarded-To')) ? $this->parseAddress('to') : $this->parseAddress('X-Forwarded-To'),
'cc' => $this->parseAddress('cc'),
'delivered-to' => $this->parseAddress('delivered-to'),
];

if (empty($addresses['from'])) {
Expand Down Expand Up @@ -468,7 +471,7 @@ public function processMail($rawEmail)
$mailData['subject'] = $parser->getHeader('subject');
$mailData['message'] = autolink($htmlFilter->addClassEmailReplyQuote($parser->getMessageBody('htmlEmbedded')));
$mailData['attachments'] = $parser->getAttachments();
}catch(\Exception $e){
} catch(\Exception $e) {
return [
'error' => true,
'message' => $e->getMessage(),
Expand All @@ -481,7 +484,7 @@ public function processMail($rawEmail)

$website = $this->entityManager->getRepository(Website::class)->findOneByCode('knowledgebase');

if (!empty($mailData['from']) && $this->container->get('ticket.service')->isEmailBlocked($mailData['from'], $website)) {
if (! empty($mailData['from']) && $this->container->get('ticket.service')->isEmailBlocked($mailData['from'], $website)) {
return [
'message' => "Received email where the sender email address is present in the block list. Skipping this email from further processing.",
'content' => [
Expand Down Expand Up @@ -530,7 +533,7 @@ public function processMail($rawEmail)
return [
'message' => "The contents of this email has already been processed.",
'content' => [
'from' => !empty($mailData['from']) ? $mailData['from'] : null,
'from' => ! empty($mailData['from']) ? $mailData['from'] : null,
'thread' => $thread->getId(),
'ticket' => $ticket->getId(),
],
Expand All @@ -547,13 +550,16 @@ public function processMail($rawEmail)
];
}

if ($ticket->getCustomer() && $ticket->getCustomer()->getEmail() == $mailData['from']) {
if (
$ticket->getCustomer()
&& $ticket->getCustomer()->getEmail() == $mailData['from']
) {
// Reply from customer
$user = $ticket->getCustomer();

$mailData['user'] = $user;
$userDetails = $user->getCustomerInstance()->getPartialDetails();
} else if ($this->entityManager->getRepository(Ticket::class)->isTicketCollaborator($ticket, $mailData['from'])){
} else if ($this->entityManager->getRepository(Ticket::class)->isTicketCollaborator($ticket, $mailData['from'])) {
// Reply from collaborator
$user = $this->entityManager->getRepository(User::class)->findOneByEmail($mailData['from']);

Expand All @@ -563,7 +569,10 @@ public function processMail($rawEmail)
} else {
$user = $this->entityManager->getRepository(User::class)->findOneByEmail($mailData['from']);

if (!empty($user) && null != $user->getAgentInstance()) {
if (
! empty($user)
&& null != $user->getAgentInstance()
) {
$mailData['user'] = $user;
$mailData['createdBy'] = 'agent';
$userDetails = $user->getAgentInstance()->getPartialDetails();
Expand All @@ -589,7 +598,6 @@ public function processMail($rawEmail)
$this->entityManager->flush();

$ticket->lastCollaborator = $user;


$event = new CoreWorkflowEvents\Ticket\Collaborator();
$event
Expand Down Expand Up @@ -780,18 +788,22 @@ public function processOutlookMail(array $outlookEmail)
;

$this->container->get('event_dispatcher')->dispatch($event, 'uvdesk.automation.workflow.execute');
} else if (false === $ticket->getIsTrashed() && strtolower($ticket->getStatus()->getCode()) != 'spam' && !empty($mailData['inReplyTo'])) {
} else if (
false === $ticket->getIsTrashed()
&& strtolower($ticket->getStatus()->getCode()) != 'spam'
&& ! empty($mailData['inReplyTo'])
) {
$mailData['threadType'] = 'reply';
$thread = $this->entityManager->getRepository(Thread::class)->findOneByMessageId($mailData['messageId']);
$ticketRef = $this->entityManager->getRepository(Ticket::class)->findById($ticket->getId());
$referenceIds = explode(' ', $ticketRef[0]->getReferenceIds());

if (!empty($thread)) {
if (! empty($thread)) {
// Thread with the same message id exists skip process.
return [
'message' => "The contents of this email has already been processed 1.",
'content' => [
'from' => !empty($mailData['from']) ? $mailData['from'] : null,
'from' => ! empty($mailData['from']) ? $mailData['from'] : null,
'thread' => $thread->getId(),
'ticket' => $ticket->getId(),
],
Expand Down Expand Up @@ -823,8 +835,7 @@ public function processOutlookMail(array $outlookEmail)
$userDetails = $user->getCustomerInstance()->getPartialDetails();
} else {
$user = $this->entityManager->getRepository(User::class)->findOneByEmail($mailData['from']);

if (!empty($user) && null != $user->getAgentInstance()) {
if (! empty($user) && null != $user->getAgentInstance()) {
$mailData['user'] = $user;
$mailData['createdBy'] = 'agent';
$userDetails = $user->getAgentInstance()->getPartialDetails();
Expand Down Expand Up @@ -865,7 +876,7 @@ public function processOutlookMail(array $outlookEmail)

$thread = $this->container->get('ticket.service')->createThread($ticket, $mailData);

if($thread->getThreadType() == 'reply') {
if ($thread->getThreadType() == 'reply') {
if ($thread->getCreatedBy() == 'customer') {
$event = new CoreWorkflowEvents\Ticket\CustomerReply();
$event
Expand Down
15 changes: 9 additions & 6 deletions Utils/MailboxConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function addMailbox(Mailbox $mailbox)
$imapConfiguration = $mailbox->getImapConfiguration();

if (
!empty($imapConfiguration)
! empty($imapConfiguration)
&& !$imapConfiguration instanceof IMAP\Transport\AppTransportConfigurationInterface
&& !$imapConfiguration instanceof IMAP\Transport\SimpleTransportConfigurationInterface
) {
Expand Down Expand Up @@ -59,7 +59,10 @@ public function getMailboxes(): array

public function getDefaultMailbox(): ?Mailbox
{
if (!empty($this->defaultMailbox) && !empty($this->collection[$this->defaultMailbox])) {
if (
! empty($this->defaultMailbox)
&& !empty($this->collection[$this->defaultMailbox])
) {
return $this->collection[$this->defaultMailbox];
}

Expand All @@ -81,7 +84,7 @@ public function setDefaultMailbox(Mailbox $mailbox): self

public function getMailboxById($mailboxId): ?Mailbox
{
if (!empty($this->collection[$mailboxId])) {
if (! empty($this->collection[$mailboxId])) {
return $this->collection[$mailboxId];
}

Expand All @@ -93,7 +96,7 @@ public function getMailboxByEmailAddress($mailboxEmail): ?Mailbox
foreach ($this->collection as $mailbox) {
$smtpConfiguration = $mailbox->getSmtpConfiguration();

if (!empty($smtpConfiguration) && $smtpConfiguration->getUsername() == $mailboxEmail) {
if (! empty($smtpConfiguration) && $smtpConfiguration->getUsername() == $mailboxEmail) {
return $mailbox;
}
}
Expand All @@ -106,7 +109,7 @@ public function getIncomingMailboxByEmailAddress($mailboxEmail): ?Mailbox
foreach ($this->collection as $mailbox) {
$imapConfiguration = $mailbox->getImapConfiguration();

if (!empty($imapConfiguration) && $imapConfiguration->getUsername() == $mailboxEmail) {
if (! empty($imapConfiguration) && $imapConfiguration->getUsername() == $mailboxEmail) {
return $mailbox;
}
}
Expand All @@ -121,7 +124,7 @@ public function getOutgoingMailboxByEmailAddress($mailboxEmail): ?Mailbox

public function __toString()
{
if (!empty($this->collection)) {
if (! empty($this->collection)) {
$mailboxes = array_reduce($this->collection, function($mailboxes, $mailbox) {
return $mailboxes . (string) $mailbox;
}, '');
Expand Down

0 comments on commit bf98efc

Please sign in to comment.