src/Helpers/System/ConfigHelper.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Helpers\System;
  3. use Symfony\Component\DependencyInjection\ContainerInterface as Container;
  4. use Symfony\Component\Yaml\Parser;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. class ConfigHelper
  7. {
  8. protected $container;
  9. protected $em;
  10. protected $config;
  11. public function __construct(Container $container, EntityManagerInterface $em)
  12. {
  13. $this->container = $container;
  14. $this->em = $em;
  15. $this->config = array();
  16. // Get everything into an array
  17. $allConfig = $this->em->getRepository(\App\Entity\System\Config::class)->findAll();
  18. foreach($allConfig AS $someConfig)
  19. {
  20. $this->config[$someConfig->getKey()] = $someConfig;
  21. }
  22. }
  23. public function getAll()
  24. {
  25. return $this->config;
  26. }
  27. public function get($key)
  28. {
  29. if(!array_key_exists($key, $this->config))
  30. return null;
  31. else
  32. return $this->config[$key];
  33. }
  34. public function getValue($key, $default = null)
  35. {
  36. if(!array_key_exists($key, $this->config))
  37. return $default;
  38. else
  39. return $this->config[$key]->getValue();
  40. }
  41. public function setValue($key, $value)
  42. {
  43. if(!array_key_exists($key, $this->config))
  44. {
  45. throw new \Exception("Config item not found: " . $key);
  46. }
  47. else
  48. {
  49. $this->config[$key]->setValue($value);
  50. }
  51. // Persist and flush
  52. $this->em->persist($this->config[$key]);
  53. $this->em->flush($this->config[$key]);
  54. }
  55. public function create($opt = array())
  56. {
  57. $newItem = new \App\Entity\System\Config();
  58. $newItem->setKey($opt['key']);
  59. $newItem->setValue($opt['value']);
  60. $newItem->setLabel($opt['label']);
  61. $newItem->setCategory($opt['category']);
  62. // Persist and flush
  63. $this->em->persist($newItem);
  64. $this->em->flush();
  65. $this->config[$opt['key']] = $newItem;
  66. }
  67. }