PackageManager.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 Doctrine\ORM\EntityManager;
  13. use Packagist\WebBundle\Entity\Package;
  14. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  15. /**
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class PackageManager
  19. {
  20. protected $em;
  21. protected $mailer;
  22. protected $twig;
  23. protected $logger;
  24. protected $options;
  25. public function __construct(EntityManager $em, \Swift_Mailer $mailer, \Twig_Environment $twig, LoggerInterface $logger, array $options)
  26. {
  27. $this->em = $em;
  28. $this->mailer = $mailer;
  29. $this->twig = $twig;
  30. $this->logger = $logger;
  31. $this->options = $options;
  32. }
  33. public function notifyUpdateFailure(Package $package, \Exception $e, $details = null)
  34. {
  35. if (!$package->isUpdateFailureNotified()) {
  36. $recipients = array();
  37. foreach ($package->getMaintainers() as $maintainer) {
  38. if ($maintainer->isNotifiableForFailures()) {
  39. $recipients[$maintainer->getEmail()] = $maintainer->getUsername();
  40. }
  41. }
  42. if ($recipients) {
  43. $body = $this->twig->render('PackagistWebBundle:Email:update_failed.txt.twig', array(
  44. 'package' => $package,
  45. 'exception' => get_class($e),
  46. 'exceptionMessage' => $e->getMessage(),
  47. 'details' => $details,
  48. ));
  49. $message = \Swift_Message::newInstance()
  50. ->setSubject($package->getName().' failed to update, invalid composer.json data')
  51. ->setFrom($this->options['from'], $this->options['fromName'])
  52. ->setTo($recipients)
  53. ->setBody($body)
  54. ;
  55. try {
  56. $this->mailer->send($message);
  57. } catch (\Swift_TransportException $e) {
  58. $this->logger->error('['.get_class($e).'] '.$e->getMessage());
  59. return false;
  60. }
  61. }
  62. $package->setUpdateFailureNotified(true);
  63. $this->em->flush();
  64. }
  65. return true;
  66. }
  67. public function notifyNewMaintainer($user, $package)
  68. {
  69. $body = $this->twig->render('PackagistWebBundle:Email:maintainer_added.txt.twig', array(
  70. 'package_name' => $package->getName()
  71. ));
  72. $message = \Swift_Message::newInstance()
  73. ->setSubject('You have been added to ' . $package->getName() . ' as a maintainer')
  74. ->setFrom($this->options['from'], $this->options['fromName'])
  75. ->setTo($user->getEmail())
  76. ->setBody($body)
  77. ;
  78. try {
  79. $this->mailer->send($message);
  80. } catch (\Swift_TransportException $e) {
  81. $this->logger->error('['.get_class($e).'] '.$e->getMessage());
  82. return false;
  83. }
  84. return true;
  85. }
  86. }