src/Service/InquiryService.php line 49

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Service;
  4. use PHPUnit\Exception;
  5. use Pimcore\Log\ApplicationLogger;
  6. use Pimcore\Model\DataObject\DealerLocation;
  7. use Pimcore\Model\DataObject\Inquiry;
  8. use Pimcore\Mail;
  9. use Pimcore\Model\DataObject\Objectbrick\Data\VehicleBaseData;
  10. use Pimcore\Model\DataObject\Vehicle;
  11. use Spatie\ArrayToXml\ArrayToXml;
  12. use Symfony\Component\Uid\Uuid;
  13. class InquiryService
  14. {
  15.     /**
  16.      * @var ApplicationLogger
  17.      */
  18.     private ApplicationLogger $logger;
  19.     /**
  20.      * @var Vehicle|null
  21.      */
  22.     private ?Vehicle $vehicle;
  23.     /**
  24.      * @var Inquiry
  25.      */
  26.     private Inquiry $inquiry;
  27.     /**
  28.      * @var Array
  29.      */
  30.     private array $inquiryFile;
  31.     /**
  32.      * @var Array
  33.      */
  34.     private array $message;
  35.     public function __construct(ApplicationLogger $logger)
  36.     {
  37.         $this->logger = $logger;
  38.         $this->vehicle = null;
  39.         $this->inquiryFile = [];
  40.         $this->messageString[] = 'Neue Anfrage über die Homepage';
  41.     }
  42.     /**
  43.      * @return void
  44.      * @throws \Exception
  45.      */
  46.     public function sendCRMInquiryMailNotification()
  47.     {
  48.         $this->logger->debug('Created Inquiry');
  49.         // create generic xml
  50.         $this->inquiryFile['generalInformation'] = [
  51.             'channel' => 'HOMEPAGE',
  52.             'urgency' => 'NORMAL',
  53.             'subject' => $this->inquiry->getMessage(),
  54.         ];
  55. // ????        $this->inquiryFile['leadid'] = $leadId ?? Uuid::v4();
  56.         $this->inquiryFile['contact'] = [
  57.             'status' => '',
  58.             'type' => $this->inquiry->getDealerdeskType() ?? 'CASE',
  59.             'salutation' => $this->inquiry->getSalutation() ??  '',
  60.             'fullName' => $this->inquiry->getFirstname() . ' ' . $this->inquiry->getLastname(),
  61.             'givenName' => $this->inquiry->getFirstname(),
  62.             'familyName' => $this->inquiry->getLastname(),
  63.             'address1' => '',
  64.             'address2' => '',
  65.             'zip' => '',
  66.             'city' => '',
  67.             'country' => '',
  68.             'state' => $this->inquiry->getSourceContext() ?? '',
  69.             'phone' => $this->inquiry->getPhone() ?? '',
  70.             'email' => $this->inquiry->getEmail() ?? '',
  71.             ];
  72.         // - is vehicle included
  73.         if ($this->getVehicle() instanceof Vehicle) {
  74.             //  - attach vehicle information to generic message
  75.             /* @var VehicleBaseData $vehicleBaseData */
  76.             $vehicleBaseData = $this->getVehicle()->getStammdaten()->getVehicleBaseData();
  77.             //  - attach vehicle information to xml
  78.             $this->inquiryFile['requestedVehicle'] = [
  79.                 'internalId' => $vehicleBaseData->getInterneNummer(),
  80.                 'link' => 'https://timmermanns.de' . '/fahrzeugboerse/fahrzeuge?i=' . $vehicleBaseData->getInterneNummer(),
  81.                 'make' => $vehicleBaseData->getMarke(),
  82.                 'model' => $vehicleBaseData->getModellbasis(),
  83.                 'modelDescription' => $vehicleBaseData->getModell(),
  84.                 'mileage' => $vehicleBaseData->getKilometer(),
  85.                 'price' => $this->getVehicle()->getKosten()->getCosts()->getPreis(),
  86.                 'firstRegistration' => $vehicleBaseData->getEz(),
  87.                 'power' => round($this->getVehicle()->getTechnischeDaten()->getTechnicalData()->getLeistung() * 1.35962, 0), // convert from kW to PS
  88.                 'exteriorColor' => $this->getVehicle()->getAussen()->getExterieur()->getFarbe(),
  89.             ];
  90.         }
  91.         // set mail recipient
  92.         $mailRecipient = $this->getInquiryMailRecipient();
  93.         $mail = new Mail();
  94.         $mail->to($mailRecipient);
  95.         $mail->subject("Neue Anfrage über die Webseite");
  96.         $mail->text($this->composeMessage());
  97.         $mail->attach($this->getInquiryXML(), 'dealerdeskLead.xml', 'text/xml');
  98.         $mail->sendWithoutRendering();
  99.     }
  100.     /**
  101.      * @return Vehicle|null
  102.      * @throws \Exception
  103.      */
  104.     protected function getVehicle()
  105.     {
  106.         if (!$this->vehicle instanceof Vehicle) {
  107.             $this->logger->debug('Vehicle not yet set ... ');
  108.             try {
  109.                 if ($this->inquiry->getInternalVehicleId() !== null) {
  110.                     $this->vehicle = Vehicle::getList()->filterByInternalVehicleId($this->inquiry->getInternalVehicleId())->current();
  111.                 }
  112.             } catch (Exception $exception) {
  113.                 $this->logger->error($exception->getMessage());
  114.                 $this->vehicle = null;
  115.             }
  116.         }
  117.         return $this->vehicle;
  118.     }
  119.     /**
  120.      * @return string|null
  121.      * @throws \Exception
  122.      */
  123.     protected function getInquiryMailRecipient(): ?string
  124.     {
  125.         $parentFolder = $this->inquiry->getParent();
  126.         if ($parentFolder instanceof \Pimcore\Model\DataObject\Folder) {
  127.             $sourceContextKey = $this->inquiry->getSourceContext();
  128.             if ($sourceContextKey) {
  129.                 $recipientFromProperty = $parentFolder->getProperty($sourceContextKey);
  130.                 if ($recipientFromProperty) {
  131.                     return $recipientFromProperty;
  132.                 }
  133.             }
  134.         }
  135.         try {
  136.             $vehicle = $this->getVehicle();
  137.             if ($vehicle instanceof Vehicle) {
  138.                 $dealerLocation = DealerLocation::getList()->filterByLocationId(
  139.                     $vehicle->getStammdaten()->getVehicleBaseData()->getFilialId()
  140.                 )->current();
  141.                 if ($dealerLocation && $dealerLocation->getAutocrmCarInquiryEmail()) {
  142.                     return $dealerLocation->getAutocrmCarInquiryEmail();
  143.                 }
  144.             }
  145.         } catch (Exception $exception) {
  146.             $this->logger->error($exception->getMessage());
  147.         }
  148.         $inquiryLocation = $this->inquiry->getLocation();
  149.         try  {
  150.             $dealerLocation = DealerLocation::getList()->filterByName($inquiryLocation)->current();
  151.             if ($dealerLocation && $dealerLocation->getAutocrmGenericInquiryEmail()) {
  152.                 return $dealerLocation->getAutocrmGenericInquiryEmail();
  153.             }
  154.         } catch (Exception $exception) {
  155.             $this->logger->error($exception->getMessage());
  156.         }
  157.         return 'homepage-neuss@timmermanns.dealerdesk.de';
  158.     }
  159.     /**
  160.      * @return bool|string
  161.      * @throws \Exception
  162.      */
  163.     protected function getInquiryXML()
  164.     {
  165.         return ArrayToXml::convert($this->inquiryFile, 'dealerdeskLead', true, 'utf-8');
  166.     }
  167.     /**
  168.      * @return string
  169.      * @throws \Exception
  170.      */
  171.     protected function composeMessage(): string
  172.     {
  173.         $messageString = '';
  174.         $this->message[] = $this->getInquiryXML();
  175.         foreach ($this->message as $line) {
  176.             $messageString .= $line . "\n";
  177.         }
  178.         return $messageString;
  179.     }
  180.     /**
  181.      * @return Inquiry
  182.      */
  183.     public function getInquiry(): Inquiry
  184.     {
  185.         return $this->inquiry;
  186.     }
  187.     /**
  188.      * @param Inquiry $inquiry
  189.      */
  190.     public function setInquiry(Inquiry $inquiry): void
  191.     {
  192.         $this->inquiry = $inquiry;
  193.     }
  194. }