AuditManager.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 $serviceId
  26. * @param array $classes
  27. */
  28. public function setReader($serviceId, array $classes)
  29. {
  30. $this->readers[$serviceId] = $classes;
  31. }
  32. /**
  33. * @param $class
  34. * @return bool
  35. */
  36. public function hasReader($class)
  37. {
  38. foreach ($this->readers as $classes) {
  39. if (in_array($class, $classes)) {
  40. return true;
  41. }
  42. }
  43. return false;
  44. }
  45. /**
  46. * @param $class
  47. * @return \Sonata\AdminBundle\Model\AuditReaderInterface
  48. * @throws \RuntimeException
  49. */
  50. public function getReader($class)
  51. {
  52. foreach ($this->readers as $readerId => $classes) {
  53. if (in_array($class, $classes)) {
  54. return $this->container->get($readerId);
  55. }
  56. }
  57. throw new \RuntimeException(sprintf('The class %s does not have any reader manager', $class));
  58. }
  59. }