PackageManager.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of Packagist.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. * Nils Adermann <naderman@naderman.de>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Packagist\WebBundle\Model;
  12. use Swift_Mailer;
  13. use Twig_Environment;
  14. use Doctrine\ORM\EntityManager;
  15. use Packagist\WebBundle\Entity\Package;
  16. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  17. /**
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class PackageManager
  21. {
  22. protected $em;
  23. protected $mailer;
  24. protected $twig;
  25. protected $logger;
  26. protected $options;
  27. public function __construct(EntityManager $em, Swift_Mailer $mailer, Twig_Environment $twig, LoggerInterface $logger, array $options)
  28. {
  29. $this->em = $em;
  30. $this->mailer = $mailer;
  31. $this->twig = $twig;
  32. $this->logger = $logger;
  33. $this->options = $options;
  34. }
  35. public function notifyUpdateFailure(Package $package, \Exception $e, $details = null)
  36. {
  37. if (!$package->isUpdateFailureNotified()) {
  38. $recipients = array();
  39. foreach ($package->getMaintainers() as $maintainer) {
  40. if ($maintainer->isNotifiableForFailures()) {
  41. $recipients[$maintainer->getEmail()] = $maintainer->getUsername();
  42. }
  43. }
  44. if ($recipients) {
  45. $body = $this->twig->render('PackagistWebBundle:Email:update_failed.txt.twig', array(
  46. 'package' => $package,
  47. 'exception' => get_class($e),
  48. 'exceptionMessage' => $e->getMessage(),
  49. 'details' => $details,
  50. ));
  51. $message = \Swift_Message::newInstance()
  52. ->setSubject($package->getName().' failed to update, invalid composer.json data')
  53. ->setFrom($this->options['from'], $this->options['fromName'])
  54. ->setTo($recipients)
  55. ->setBody($body)
  56. ;
  57. try {
  58. $this->mailer->send($message);
  59. } catch (\Swift_TransportException $e) {
  60. $this->logger->err('['.get_class($e).'] '.$e->getMessage());
  61. return false;
  62. }
  63. }
  64. $package->setUpdateFailureNotified(true);
  65. $this->em->flush();
  66. }
  67. return true;
  68. }
  69. public function notifyNewMaintainer($user, $package)
  70. {
  71. $body = $this->twig->render('PackagistWebBundle:Email:maintainer_added.txt.twig', array(
  72. 'package_name' => $package->getName()
  73. ));
  74. $message = \Swift_Message::newInstance()
  75. ->setSubject('You have been added to ' . $package->getName() . ' as a maintainer')
  76. ->setFrom($this->options['from'], $this->options['fromName'])
  77. ->setTo($user->getEmail())
  78. ->setBody($body)
  79. ;
  80. try {
  81. $this->mailer->send($message);
  82. } catch (\Swift_TransportException $e) {
  83. $this->logger->err('['.get_class($e).'] '.$e->getMessage());
  84. return false;
  85. }
  86. return true;
  87. }
  88. }