<?php
 namespace App\ClientCollection\Corporate; class EmailData { private $idCorporate; private $messages; private $emailData; public function __construct(int $idCorporate) { $this->idCorporate = $idCorporate; } public function getIdCorporate() :int { return $this->idCorporate; } public function getData() : array { if ($this->emailData) { return $this->emailData; } $this->messages = new \App\MessagesCollector(); $return = []; $corporateCollectionProfileEnt = \Entity\CorporateCollection::select($this->idCorporate); $corporateEnt = \Entity\Corporate::select($this->idCorporate); if ($corporateCollectionProfileEnt->recordExists()) { switch ($corporateCollectionProfileEnt->toAuto) { case 'none': $return['toAuto'] = []; break; case 'corporateEmail': if ($corporateEnt->email) { $return['toAuto'] = [ $corporateEnt->email ]; } else { $return['toAuto'] = []; } break; case 'corporateMainOfficeEmail': if ($corporateEnt->mainOffice->email) { $return['toAuto'] = [ $corporateEnt->mainOffice->email ]; } else { $return['toAuto'] = []; } break; case '': $return['toAuto'] = null; break; default: throw new \Exception('Invalid toAuto (1) - '.$corporateCollectionProfileEnt->toAuto); } if ($corporateCollectionProfileEnt->toList) { $return['toManual'] = $corporateCollectionProfileEnt->toList; } else { $return['toManual'] = null; } if ($corporateCollectionProfileEnt->bccList) { $return['bccManual'] = $corporateCollectionProfileEnt->bccList; } else { $return['bccManual'] = null; } $return['dueInDaysPeriod'] = $corporateCollectionProfileEnt->dueInDaysPeriod; } if ($corporateCollectionProfileEnt->profileCode) { $profileCode = $corporateCollectionProfileEnt->profileCode; $return['profileType'] = 'custom'; $return['active'] = $corporateCollectionProfileEnt->active; } else { $profileCode = \App\Config\CorporateCollectionProfiles::getInstance()->getDefaultItem(); if ($profileCode) { $return['profileType'] = 'default'; $return['active'] = true; } else { $return['profileType'] = 'none'; $return['active'] = false; } } $return['profileCode'] = $profileCode; if ($profileCode) { $profileDetails = \App\Config\CorporateCollectionProfiles::getInstance()->get($profileCode); if ($return['toAuto']===null) { switch ($profileDetails['recipientToAuto']) { case 'corporateEmail': if ($corporateEnt->email) { $return['toAuto'] = [ $corporateEnt->email ]; } else { $return['toAuto'] = []; } break; case 'corporateMainOfficeEmail': if ($corporateEnt->mainOffice->email) { $return['toAuto'] = [ $corporateEnt->mainOffice->email ]; } else { $return['toAuto'] = []; } break; case '': $return['toAuto'] = []; break; default: throw new \Exception('Invalid toAuto (2) - '.$profileDetails['recipientToAuto']); } } if ($return['toManual']===null) { $return['toManual'] = $profileDetails['recipientToManual']; } if ($return['bccManual']===null) { $return['bccManual'] = $profileDetails['recipientBccManual']; } if ($return['dueInDaysPeriod']===null) { $return['dueInDaysPeriod'] = $profileDetails['dueInDaysPeriod']; } } if ($return['profileType']!='none') { $return['toFull'] = array_unique( array_merge((array)$return['toAuto'], (array)$return['toManual']) ); $return['bccFull'] = array_unique( (array)$return['bccManual'] ); } if ($profileCode) { $return['attachment'] = $profileDetails['attachment']; $return['subject'] = $profileDetails['emailSubject']; $return['body'] = $profileDetails['emailBody']; } $this->checkOutput($return); $this->emailData = $return; return $return; } public function getMessages() :\App\MessagesCollector { if (!$this->messages) { throw new \Exception('You must call getData() before calling '.__METHOD__); } return $this->messages; } private function checkOutput(array $output) { if ($output['profileType']=='custom' and !$output['active']) { $this->messages->addWarning('The profile for this company is not active.'); } if ($output['profileType']=='none') { $this->messages->addWarning('There is no default collection profile to be applied on this company.'); } if (!$output['toFull']) { $this->messages->addWarning('No email adddress(es) (auto or manual) for this company - please check the company profile.'); } if ($output['dueInDaysPeriod']===null) { $this->messages->addWarning('The field `Days until due date reached` is not filled in neither in company details nor in collection profile.'); } } }