vendor/roothirsch/core-bundle/Site/SiteProvider.php line 104

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\CoreBundle\Site;
  3. use Doctrine\DBAL\Exception\InvalidFieldNameException;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Gedmo\Translatable\Entity\Repository\TranslationRepository;
  6. use Gedmo\Translatable\Entity\Translation;
  7. use Roothirsch\CoreBundle\Site\Entity\Site;
  8. use Roothirsch\CoreBundle\Site\Repository\SiteRepository;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. class SiteProvider
  11. {
  12. /**
  13. * @var Entity\Site|null
  14. */
  15. private $site;
  16. /**
  17. * Undocumented variable
  18. *
  19. * @var Request
  20. */
  21. protected $request;
  22. private $repository;
  23. private TranslationRepository $translationRepository;
  24. /**
  25. * @return string
  26. */
  27. public function getTitle()
  28. {
  29. $translations = $this->getTranslations();
  30. if($this->request) {
  31. if(array_key_exists($this->request->getLocale(), $translations)){
  32. return $translations[$this->request->getLocale()]['title'];
  33. }
  34. }
  35. return $this->getSite()->getTitle();
  36. }
  37. /**
  38. * @return string
  39. */
  40. public function getLogo()
  41. {
  42. return $this->getSite()->getLogo();
  43. }
  44. /**
  45. * @return string
  46. */
  47. public function getTheme()
  48. {
  49. if(!$this->request){
  50. return $this->getSite()->getTheme();
  51. }
  52. $overrideTheme = $this->request->get('_override_theme', false);
  53. if ($overrideTheme !== false) {
  54. return $overrideTheme;
  55. }
  56. return $this->getSite()->getTheme();
  57. }
  58. public function getWebsite(){
  59. return $this->getSite()->getWebsite();
  60. }
  61. public function getHostUrl(){
  62. return $this->request->server->get("HTTP_ORIGIN");
  63. }
  64. public function getAddress(){
  65. return $this->getSite()->getAddress();
  66. }
  67. public function getCompany(){
  68. return $this->getSite()->getCompany();
  69. }
  70. public function getTax() {
  71. return $this->getSite()->getTax();
  72. }
  73. public function isMaintenanceEnabled() {
  74. return $this->getSite()->isMaintenanceEnabled();
  75. }
  76. public function getMaintenanceMessage() {
  77. $translations = $this->getTranslations();
  78. if($this->request) {
  79. if(array_key_exists($this->request->getLocale(), $translations) && isset($translations[$this->request->getLocale()]['maintenanceMessage'])){
  80. return $translations[$this->request->getLocale()]['maintenanceMessage'];
  81. }
  82. }
  83. return $this->getSite()->getMaintenanceMessage();
  84. }
  85. /**
  86. * SettingsProvider constructor.
  87. */
  88. public function __construct(RequestStack $requestStack, EntityManagerInterface $entityManager)
  89. {
  90. try {
  91. $this->translationRepository = $entityManager->getRepository(Translation::class);
  92. $this->repository = $entityManager->getRepository(Site::class);
  93. $this->request = $requestStack->getCurrentRequest();
  94. } catch(InvalidFieldNameException $e) {
  95. }
  96. }
  97. public function getSite() {
  98. if (empty($this->site)) {
  99. $this->site = $this->repository->findOneBy(["id"=>1]);
  100. }
  101. return $this->site;
  102. }
  103. public function getTranslations()
  104. {
  105. return $this->translationRepository->findTranslations($this->getSite());
  106. }
  107. }