MailHandler.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. /**
  12. * Base class for all mail handlers
  13. *
  14. * @author Gyula Sallai
  15. */
  16. abstract class MailHandler extends AbstractProcessingHandler
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function handleBatch(array $records)
  22. {
  23. $messages = array();
  24. foreach ($records as $record) {
  25. if ($record['level'] < $this->level) {
  26. continue;
  27. }
  28. $messages[] = $this->processRecord($record);
  29. }
  30. if (!empty($messages)) {
  31. $this->send((string) $this->getFormatter()->formatBatch($messages), $messages);
  32. }
  33. }
  34. /**
  35. * Send a mail with the given content
  36. *
  37. * @param string $content formatted email body to be sent
  38. * @param array $records the array of log records that formed this content
  39. */
  40. abstract protected function send($content, array $records);
  41. /**
  42. * {@inheritdoc}
  43. */
  44. protected function write(array $record)
  45. {
  46. $this->send((string) $record['formatted'], array($record));
  47. }
  48. protected function getHighestRecord(array $records)
  49. {
  50. $highestRecord = null;
  51. foreach ($records as $record) {
  52. if ($highestRecord === null || $highestRecord['level'] < $record['level']) {
  53. $highestRecord = $record;
  54. }
  55. }
  56. return $highestRecord;
  57. }
  58. }