AuditManager.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Model;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. class AuditManager implements AuditManagerInterface
  13. {
  14. protected $classes = array();
  15. protected $readers = array();
  16. protected $container;
  17. /**
  18. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  19. */
  20. public function __construct(ContainerInterface $container)
  21. {
  22. $this->container = $container;
  23. }
  24. /**
  25. * @param string $serviceId
  26. * @param array $classes
  27. */
  28. public function setReader($serviceId, array $classes)
  29. {
  30. $this->readers[$serviceId] = $classes;
  31. }
  32. /**
  33. * @param string $class
  34. *
  35. * @return bool
  36. */
  37. public function hasReader($class)
  38. {
  39. foreach ($this->readers as $classes) {
  40. if (in_array($class, $classes)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. /**
  47. * @param string $class
  48. *
  49. * @return \Sonata\AdminBundle\Model\AuditReaderInterface
  50. * @throws \RuntimeException
  51. */
  52. public function getReader($class)
  53. {
  54. foreach ($this->readers as $readerId => $classes) {
  55. if (in_array($class, $classes)) {
  56. return $this->container->get($readerId);
  57. }
  58. }
  59. throw new \RuntimeException(sprintf('The class %s does not have any reader manager', $class));
  60. }
  61. }