You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 3 години
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Lc\SovBundle\Builder\Setting;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\SovBundle\Definition\SiteSettingDefinition;
  5. use Lc\SovBundle\Factory\Setting\SiteSettingFactory;
  6. use Lc\SovBundle\Repository\Site\SiteStore;
  7. use Lc\SovBundle\Solver\Setting\SettingSolver;
  8. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  9. class SettingBuilder
  10. {
  11. protected EntityManagerInterface $entityManager;
  12. protected SettingSolver $settingSolver;
  13. protected SiteStore $siteStore;
  14. protected SiteSettingDefinition $siteSettingDefinition;
  15. protected SiteSettingFactory $siteSettingFactory;
  16. public function __construct(
  17. EntityManagerInterface $entityManager,
  18. SettingSolver $settingSolver,
  19. SiteStore $siteStore,
  20. SiteSettingDefinition $siteSettingDefinition,
  21. SiteSettingFactory $siteSettingFactory
  22. ) {
  23. $this->entityManager = $entityManager;
  24. $this->settingSolver = $settingSolver;
  25. $this->siteStore = $siteStore;
  26. $this->siteSettingDefinition = $siteSettingDefinition;
  27. $this->siteSettingFactory = $siteSettingFactory;
  28. }
  29. public function initSiteSettings()
  30. {
  31. $site = $this->siteStore->getOneByDevAlias('default');
  32. if ($site) {
  33. $settings = $this->siteSettingDefinition->getSettings();
  34. foreach ($settings as $category => $settingList) {
  35. foreach ($settingList as $settingName => $setting) {
  36. $entitySetting = $this->settingSolver->getSetting($site, $settingName);
  37. if (!$entitySetting) {
  38. $text = null;
  39. $date = null;
  40. $file = null;
  41. $fieldValue = isset($setting['default']) ? $setting['default'] : null;
  42. if ($setting['field'] == 'text') {
  43. $text = $fieldValue;
  44. }
  45. if ($setting['field'] == 'date') {
  46. $date = $fieldValue;
  47. }
  48. if ($setting['field'] == 'file') {
  49. $file = $fieldValue;
  50. }
  51. $entitySetting = $this->siteSettingFactory->create(
  52. $site,
  53. $setting['name'],
  54. $text,
  55. $date,
  56. $file
  57. );
  58. $this->entityManager->create($entitySetting);
  59. } else {
  60. $methodGetValue = 'get' . ucfirst($setting['field']);
  61. if ($entitySetting->$methodGetValue() === null
  62. && isset($setting['default'])
  63. && $setting['default'] !== null) {
  64. $methodSetValue = 'set' . ucfirst($setting['field']);
  65. $entitySetting->$methodSetValue($setting['default']);
  66. $this->entityManager->update($entitySetting);
  67. }
  68. }
  69. }
  70. }
  71. $this->entityManager->flush();
  72. } else {
  73. throw new NotFoundHttpException('Le site par défaut n\'existe pas.');
  74. }
  75. }
  76. }