vendor/pimcore/pimcore/models/Tool/Email/Log.php line 29

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Model\Tool\Email;
  15. use League\Flysystem\FilesystemException;
  16. use League\Flysystem\UnableToWriteFile;
  17. use Pimcore\Logger;
  18. use Pimcore\Model;
  19. use Pimcore\Tool\Storage;
  20. /**
  21.  * @internal
  22.  *
  23.  * @method \Pimcore\Model\Tool\Email\Log\Dao getDao()
  24.  */
  25. class Log extends Model\AbstractModel
  26. {
  27.     /**
  28.      * EmailLog Id
  29.      *
  30.      * @var int
  31.      */
  32.     protected $id;
  33.     /**
  34.      * Id of the email document or null if no document was given
  35.      *
  36.      * @var int | null
  37.      */
  38.     protected $documentId;
  39.     /**
  40.      * Parameters passed for replacement
  41.      *
  42.      * @var array
  43.      */
  44.     protected $params;
  45.     /**
  46.      * Modification date as timestamp
  47.      *
  48.      * @var int
  49.      */
  50.     protected $modificationDate;
  51.     /**
  52.      * The request URI from were the email was sent
  53.      *
  54.      * @var string
  55.      */
  56.     protected $requestUri;
  57.     /**
  58.      * The "from" email address
  59.      *
  60.      * @var string
  61.      */
  62.     protected $from;
  63.     /**
  64.      * Contains the reply to email addresses (multiple recipients are separated by a ",")
  65.      *
  66.      * @var string
  67.      */
  68.     protected $replyTo;
  69.     /**
  70.      * The "to" recipients (multiple recipients are separated by a ",")
  71.      *
  72.      * @var string|null
  73.      */
  74.     protected $to;
  75.     /**
  76.      * The carbon copy recipients (multiple recipients are separated by a ",")
  77.      *
  78.      * @var string|null
  79.      */
  80.     protected $cc;
  81.     /**
  82.      * The blind carbon copy recipients (multiple recipients are separated by a ",")
  83.      *
  84.      * @var string|null
  85.      */
  86.     protected $bcc;
  87.     /**
  88.      * Contains 1 if a html logfile exists and 0 if no html logfile exists
  89.      *
  90.      * @var int
  91.      */
  92.     protected $emailLogExistsHtml;
  93.     /**
  94.      * Contains 1 if a text logfile exists and 0 if no text logfile exists
  95.      *
  96.      * @var int
  97.      */
  98.     protected $emailLogExistsText;
  99.     /**
  100.      * Contains the timestamp when the email was sent
  101.      *
  102.      * @var int
  103.      */
  104.     protected $sentDate;
  105.     /**
  106.      * Contains the rendered html content of the email
  107.      *
  108.      * @var string
  109.      */
  110.     protected $bodyHtml;
  111.     /**
  112.      * Contains the rendered text content of the email
  113.      *
  114.      * @var string
  115.      */
  116.     protected $bodyText;
  117.     /**
  118.      * Contains the rendered subject of the email
  119.      *
  120.      * @var string
  121.      */
  122.     protected $subject;
  123.     /**
  124.      * Error log, when mail send resulted in failure - empty if successfully sent
  125.      *
  126.      * @var ?string
  127.      */
  128.     protected $error;
  129.     /**
  130.      * @param int $id
  131.      *
  132.      * @return $this
  133.      */
  134.     public function setDocumentId($id)
  135.     {
  136.         $this->documentId $id;
  137.         return $this;
  138.     }
  139.     /**
  140.      * @param string $requestUri
  141.      *
  142.      * @return $this
  143.      */
  144.     public function setRequestUri($requestUri)
  145.     {
  146.         $this->requestUri $requestUri;
  147.         return $this;
  148.     }
  149.     /**
  150.      * Returns the request uri
  151.      *
  152.      * @return string
  153.      */
  154.     public function getRequestUri()
  155.     {
  156.         return $this->requestUri;
  157.     }
  158.     /**
  159.      * Returns the email log id
  160.      *
  161.      * @return int
  162.      */
  163.     public function getId()
  164.     {
  165.         return (int)$this->id;
  166.     }
  167.     /**
  168.      * @param int $id
  169.      *
  170.      * @return $this
  171.      */
  172.     public function setId($id)
  173.     {
  174.         $this->id = (int)$id;
  175.         return $this;
  176.     }
  177.     /**
  178.      * @param string $subject
  179.      *
  180.      * @return $this
  181.      */
  182.     public function setSubject($subject)
  183.     {
  184.         $this->subject $subject;
  185.         return $this;
  186.     }
  187.     /**
  188.      * Returns the subject
  189.      *
  190.      * @return string
  191.      */
  192.     public function getSubject()
  193.     {
  194.         return $this->subject;
  195.     }
  196.     /**
  197.      * Returns the EmailLog entry by the given id
  198.      *
  199.      * @static
  200.      *
  201.      * @param int $id
  202.      *
  203.      * @return Log|null
  204.      */
  205.     public static function getById($id)
  206.     {
  207.         $id = (int)$id;
  208.         if ($id 1) {
  209.             return null;
  210.         }
  211.         $emailLog = new Model\Tool\Email\Log();
  212.         $emailLog->getDao()->getById($id);
  213.         $emailLog->setEmailLogExistsHtml();
  214.         $emailLog->setEmailLogExistsText();
  215.         return $emailLog;
  216.     }
  217.     /**
  218.      * Returns the email document id
  219.      *
  220.      * @return int|null
  221.      */
  222.     public function getDocumentId()
  223.     {
  224.         return $this->documentId;
  225.     }
  226.     /**
  227.      * @param array $params
  228.      *
  229.      * @return $this
  230.      */
  231.     public function setParams($params)
  232.     {
  233.         $this->params $params;
  234.         return $this;
  235.     }
  236.     /**
  237.      * Returns the dynamic parameter
  238.      *
  239.      * @return array
  240.      */
  241.     public function getParams()
  242.     {
  243.         return $this->params;
  244.     }
  245.     /**
  246.      * Sets the modification date
  247.      *
  248.      * @param int $modificationDate
  249.      *
  250.      * @return $this
  251.      */
  252.     public function setModificationDate($modificationDate)
  253.     {
  254.         $this->modificationDate $modificationDate;
  255.         return $this;
  256.     }
  257.     /**
  258.      * Returns the modification date
  259.      *
  260.      * @return int - Timestamp
  261.      */
  262.     public function getModificationDate()
  263.     {
  264.         return $this->modificationDate;
  265.     }
  266.     /**
  267.      * Sets the sent date and time
  268.      *
  269.      * @param int $sentDate - Timestamp
  270.      *
  271.      * @return $this
  272.      */
  273.     public function setSentDate($sentDate)
  274.     {
  275.         $this->sentDate $sentDate;
  276.         return $this;
  277.     }
  278.     /**
  279.      * Returns the sent date and time as unix timestamp
  280.      *
  281.      * @return int
  282.      */
  283.     public function getSentDate()
  284.     {
  285.         return $this->sentDate;
  286.     }
  287.     /**
  288.      *  Checks if a html log file exits and sets $this->emailLogExistsHtml to 0 or 1
  289.      */
  290.     public function setEmailLogExistsHtml()
  291.     {
  292.         $storage Storage::get('email_log');
  293.         $storageFile $this->getHtmlLogFilename();
  294.         $this->emailLogExistsHtml $storage->fileExists($storageFile) ? 0;
  295.         return $this;
  296.     }
  297.     /**
  298.      * Returns 1 if a html email log file exists and 0 if no html log file exists
  299.      *
  300.      * @return int - 0 or 1
  301.      */
  302.     public function getEmailLogExistsHtml()
  303.     {
  304.         return $this->emailLogExistsHtml;
  305.     }
  306.     /**
  307.      * Checks if a text log file exits and sets $this->emailLogExistsText to 0 or 1
  308.      */
  309.     public function setEmailLogExistsText()
  310.     {
  311.         $storage Storage::get('email_log');
  312.         $storageFile $this->getTextLogFilename();
  313.         $this->emailLogExistsText $storage->fileExists($storageFile) ? 0;
  314.         return $this;
  315.     }
  316.     /**
  317.      * Returns 1 if a text email log file exists and 0 if no text log file exists
  318.      *
  319.      * @return int - 0 or 1
  320.      */
  321.     public function getEmailLogExistsText()
  322.     {
  323.         return $this->emailLogExistsText;
  324.     }
  325.     /**
  326.      * Returns the filename of the html log
  327.      *
  328.      * @return string
  329.      */
  330.     public function getHtmlLogFilename()
  331.     {
  332.         return 'email-' $this->getId() . '-html.log';
  333.     }
  334.     /**
  335.      * Returns the filename of the text log
  336.      *
  337.      * @return string
  338.      */
  339.     public function getTextLogFilename()
  340.     {
  341.         return 'email-' $this->getId() . '-txt.log';
  342.     }
  343.     /**
  344.      * Returns the content of the html log file
  345.      *
  346.      * @return string | false
  347.      */
  348.     public function getHtmlLog()
  349.     {
  350.         if ($this->getEmailLogExistsHtml()) {
  351.             $storage Storage::get('email_log');
  352.             return $storage->read($this->getHtmlLogFilename());
  353.         }
  354.         return false;
  355.     }
  356.     /**
  357.      * Returns the content of the text log file
  358.      *
  359.      * @return string | false
  360.      */
  361.     public function getTextLog()
  362.     {
  363.         if ($this->getEmailLogExistsText()) {
  364.             $storage Storage::get('email_log');
  365.             return $storage->read($this->getTextLogFilename());
  366.         }
  367.         return false;
  368.     }
  369.     /**
  370.      * Removes the log file entry from the db and removes the log files on the system
  371.      */
  372.     public function delete()
  373.     {
  374.         $storage Storage::get('email_log');
  375.         $storage->delete($this->getHtmlLogFilename());
  376.         $storage->delete($this->getTextLogFilename());
  377.         $this->getDao()->delete();
  378.     }
  379.     public function save()
  380.     {
  381.         $this->getDao()->save();
  382.         $storage Storage::get('email_log');
  383.         if ($html $this->getBodyHtml()) {
  384.             try {
  385.                 $storage->write($this->getHtmlLogFilename(), $html);
  386.             } catch (FilesystemException UnableToWriteFile $exception) {
  387.                 Logger::warn('Could not write html email log file.'.$exception.' LogId: ' $this->getId());
  388.             }
  389.         }
  390.         if ($text $this->getBodyText()) {
  391.             try {
  392.                 $storage->write($this->getTextLogFilename(), $text);
  393.             } catch (FilesystemException UnableToWriteFile $exception) {
  394.                 Logger::warn('Could not write text email log file.'.$exception.' LogId: ' $this->getId());
  395.             }
  396.         }
  397.     }
  398.     /**
  399.      * @param string|null $to
  400.      *
  401.      * @return $this
  402.      */
  403.     public function setTo($to)
  404.     {
  405.         $this->to $to;
  406.         return $this;
  407.     }
  408.     /**
  409.      * Returns the "to" recipients
  410.      *
  411.      * @return string|null
  412.      */
  413.     public function getTo()
  414.     {
  415.         return $this->to;
  416.     }
  417.     /**
  418.      * @param string|null $cc
  419.      *
  420.      * @return $this
  421.      */
  422.     public function setCc($cc)
  423.     {
  424.         $this->cc $cc;
  425.         return $this;
  426.     }
  427.     /**
  428.      * Returns the carbon copy recipients
  429.      *
  430.      * @return string|null
  431.      */
  432.     public function getCc()
  433.     {
  434.         return $this->cc;
  435.     }
  436.     /**
  437.      * @param string|null $bcc
  438.      *
  439.      * @return $this
  440.      */
  441.     public function setBcc($bcc)
  442.     {
  443.         $this->bcc $bcc;
  444.         return $this;
  445.     }
  446.     /**
  447.      * Returns the blind carbon copy recipients
  448.      *
  449.      * @return string|null
  450.      */
  451.     public function getBcc()
  452.     {
  453.         return $this->bcc;
  454.     }
  455.     /**
  456.      * @param string $from
  457.      *
  458.      * @return $this
  459.      */
  460.     public function setFrom($from)
  461.     {
  462.         $this->from $from;
  463.         return $this;
  464.     }
  465.     /**
  466.      * Returns the "from" email address
  467.      *
  468.      * @return string
  469.      */
  470.     public function getFrom()
  471.     {
  472.         return $this->from;
  473.     }
  474.     /**
  475.      * @param string $replyTo
  476.      *
  477.      * @return $this
  478.      */
  479.     public function setReplyTo($replyTo)
  480.     {
  481.         $this->replyTo $replyTo;
  482.         return $this;
  483.     }
  484.     /**
  485.      * Returns the "replyTo" email address
  486.      *
  487.      * @return string
  488.      */
  489.     public function getReplyTo()
  490.     {
  491.         return $this->replyTo;
  492.     }
  493.     /**
  494.      * @param string $html
  495.      *
  496.      * @return $this
  497.      */
  498.     public function setBodyHtml($html)
  499.     {
  500.         $this->bodyHtml $html;
  501.         return $this;
  502.     }
  503.     /**
  504.      * returns the html content of the email
  505.      *
  506.      * @return string | null
  507.      */
  508.     public function getBodyHtml()
  509.     {
  510.         return $this->bodyHtml;
  511.     }
  512.     /**
  513.      * @param string $text
  514.      *
  515.      * @return $this
  516.      */
  517.     public function setBodyText($text)
  518.     {
  519.         $this->bodyText $text;
  520.         return $this;
  521.     }
  522.     /**
  523.      * Returns the text version of the email
  524.      *
  525.      * @return string
  526.      */
  527.     public function getBodyText()
  528.     {
  529.         return $this->bodyText;
  530.     }
  531.     /**
  532.      * @return string|null
  533.      */
  534.     public function getError(): ?string
  535.     {
  536.         return $this->error;
  537.     }
  538.     /**
  539.      * @param string|null $error
  540.      */
  541.     public function setError(?string $error): void
  542.     {
  543.         $this->error $error;
  544.     }
  545. }