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.

243 line
8.9KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Twig;
  3. use Lc\CaracoleBundle\Builder\Order\OrderShopBuilder;
  4. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  5. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  6. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  7. use Lc\CaracoleBundle\Model\User\UserMerchantInterface;
  8. use Lc\CaracoleBundle\Repository\Config\TaxRateStore;
  9. use Lc\CaracoleBundle\Repository\Config\UnitStore;
  10. use Lc\CaracoleBundle\Repository\Merchant\MerchantStore;
  11. use Lc\CaracoleBundle\Repository\Order\OrderShopStore;
  12. use Lc\CaracoleBundle\Repository\Product\ProductCategoryStore;
  13. use Lc\CaracoleBundle\Repository\Reminder\ReminderStore;
  14. use Lc\CaracoleBundle\Repository\Section\SectionStore;
  15. use Lc\CaracoleBundle\Resolver\MerchantResolver;
  16. use Lc\CaracoleBundle\Resolver\SectionResolver;
  17. use Lc\CaracoleBundle\Resolver\VisitorResolver;
  18. use Lc\SovBundle\Solver\Setting\SettingSolver;
  19. use Symfony\Component\Security\Core\Security;
  20. use Twig\Extension\AbstractExtension;
  21. use Twig\TwigFunction;
  22. class StoreTwigExtension extends AbstractExtension
  23. {
  24. protected MerchantStore $merchantStore;
  25. protected SectionStore $sectionStore;
  26. protected ReminderStore $reminderStore;
  27. protected MerchantResolver $merchantResolver;
  28. protected SectionResolver $sectionResolver;
  29. protected UnitStore $unitStore;
  30. protected TaxRateStore $taxRateStore;
  31. protected ProductCategoryStore $productCategoryStore;
  32. protected OrderShopStore $orderShopStore;
  33. protected SettingSolver $settingSolver;
  34. protected OrderShopBuilder $orderShopBuilder;
  35. protected Security $security;
  36. protected VisitorResolver $visitorResolver;
  37. public function __construct(
  38. MerchantResolver $merchantResolver,
  39. SectionResolver $sectionResolver,
  40. MerchantStore $merchantStore,
  41. SectionStore $sectionStore,
  42. ReminderStore $reminderStore,
  43. UnitStore $unitStore,
  44. TaxRateStore $taxRateStore,
  45. ProductCategoryStore $productCategoryStore,
  46. OrderShopStore $orderShopStore,
  47. SettingSolver $settingSolver,
  48. OrderShopBuilder $orderShopBuilder,
  49. Security $security,
  50. VisitorResolver $visitorResolver
  51. ) {
  52. $this->merchantResolver = $merchantResolver;
  53. $this->sectionResolver = $sectionResolver;
  54. $this->merchantStore = $merchantStore;
  55. $this->sectionStore = $sectionStore;
  56. $this->reminderStore = $reminderStore;
  57. $this->unitStore = $unitStore;
  58. $this->taxRateStore = $taxRateStore;
  59. $this->productCategoryStore = $productCategoryStore;
  60. $this->orderShopStore = $orderShopStore;
  61. $this->settingSolver = $settingSolver;
  62. $this->orderShopBuilder = $orderShopBuilder;
  63. $this->security = $security;
  64. $this->visitorResolver = $visitorResolver;
  65. }
  66. public function getFunctions()
  67. {
  68. return array(
  69. new TwigFunction('carac_sections', [$this, 'getSections']),
  70. new TwigFunction('carac_section_current', [$this, 'getSectionCurrent']),
  71. new TwigFunction('carac_cart_current', [$this, 'getCartCurrent']),
  72. new TwigFunction('carac_merchants', [$this, 'getMerchants']),
  73. new TwigFunction('carac_reminders', [$this, 'getReminders']),
  74. new TwigFunction('carac_units', [$this, 'getUnits']),
  75. new TwigFunction('carac_tax_rates', [$this, 'getTaxRates']),
  76. new TwigFunction('carac_reduction_cart_codes', [$this, 'getTaxRates']),
  77. new TwigFunction('product_categories', [$this, 'getProductCategories']),
  78. new TwigFunction('cart_current', [$this, 'getCartCurrent']),
  79. new TwigFunction('cart_current_visited', [$this, 'getCartCurrentVisited']),
  80. new TwigFunction('merchant_current', [$this, 'getMerchantCurrent']),
  81. new TwigFunction('user_merchant_current', [$this, 'getUserMerchantCurrent']),
  82. new TwigFunction('section_current', [$this, 'getSectionCurrent']),
  83. new TwigFunction('section_current_default', [$this, 'getSectionCurrentDefault']),
  84. new TwigFunction('section_current_visited', [$this, 'getSectionCurrentVisited']),
  85. new TwigFunction('is_out_of_sections', [$this, 'isOutOfSections']),
  86. new TwigFunction('is_inside_section', [$this, 'isInsideSection']),
  87. new TwigFunction('section_current_slug', [$this, 'getSectionCurrentSlug']),
  88. new TwigFunction('section_current_default_slug', [$this, 'getSectionCurrentDefaultSlug']),
  89. new TwigFunction('section_current_visited_slug', [$this, 'getSectionCurrentVisitedSlug']),
  90. new TwigFunction('merchant_setting', [$this, 'getMerchantSetting']),
  91. new TwigFunction('merchant_setting_current', [$this, 'getMerchantSettingCurrent']),
  92. new TwigFunction('section_setting', [$this, 'getSectionSetting']),
  93. new TwigFunction('section_setting_current', [$this, 'getSectionSettingCurrent']),
  94. new TwigFunction('visitor_current', [$this, 'getVisitorCurrent']),
  95. );
  96. }
  97. public function isInsideSection(): bool
  98. {
  99. return (bool) $this->sectionResolver->getCurrent();
  100. }
  101. public function isOutOfSections()
  102. {
  103. return $this->sectionResolver->isOutOfSection();
  104. }
  105. public function getSections()
  106. {
  107. return $this->sectionStore
  108. ->setMerchant($this->merchantResolver->getCurrent())
  109. ->getOnline();
  110. }
  111. public function getMerchants()
  112. {
  113. return $this->merchantStore->getOnline();
  114. }
  115. public function getSectionCurrent(): ?SectionInterface
  116. {
  117. return $this->sectionResolver->getCurrent();
  118. }
  119. public function getSectionCurrentDefault(): ?SectionInterface
  120. {
  121. return $this->sectionResolver->getCurrent(true);
  122. }
  123. public function getSectionCurrentVisited(): ?SectionInterface
  124. {
  125. return $this->sectionResolver->getCurrent(true, true);
  126. }
  127. public function getSectionCurrentSlug(): string
  128. {
  129. return $this->sectionResolver->getCurrent()->getSlug();
  130. }
  131. public function getSectionCurrentDefaultSlug(): string
  132. {
  133. return $this->sectionResolver->getCurrent(true)->getSlug();
  134. }
  135. public function getSectionCurrentVisitedSlug(): string
  136. {
  137. return $this->sectionResolver->getCurrent(true, true)->getSlug();
  138. }
  139. public function getCartCurrent(): OrderShopInterface
  140. {
  141. return $this->orderShopBuilder->createIfNotExist(
  142. $this->sectionResolver->getCurrent(),
  143. $this->security->getUser(),
  144. $this->visitorResolver->getCurrent(),
  145. true
  146. );
  147. }
  148. public function getCartCurrentVisited(): OrderShopInterface
  149. {
  150. return $this->orderShopBuilder->createIfNotExist(
  151. $this->sectionResolver->getCurrent(true, true),
  152. $this->security->getUser(),
  153. $this->visitorResolver->getCurrent(),
  154. true
  155. );
  156. }
  157. public function getMerchantCurrent(): MerchantInterface
  158. {
  159. return $this->merchantResolver->getCurrent();
  160. }
  161. public function getUserMerchantCurrent(): ?UserMerchantInterface
  162. {
  163. return $this->merchantResolver->getUserMerchant();
  164. }
  165. public function getMerchantSetting(MerchantInterface $merchant, string $settingName): string
  166. {
  167. return $this->settingSolver->getSettingValue($merchant, $settingName);
  168. }
  169. public function getMerchantSettingCurrent(string $settingName): ?string
  170. {
  171. return $this->settingSolver->getSettingValue($this->getMerchantCurrent(), $settingName);
  172. }
  173. public function getSectionSetting(SectionInterface $section, string $settingName): ?string
  174. {
  175. return $this->settingSolver->getSettingValue($section, $settingName);
  176. }
  177. public function getSectionSettingCurrent(string $settingName)
  178. {
  179. return $this->settingSolver->getSettingValue($this->getSectionCurrent(), $settingName);
  180. }
  181. public function getProductCategories()
  182. {
  183. return $this->productCategoryStore
  184. ->setSection($this->sectionResolver->getCurrent())
  185. ->getParent();
  186. }
  187. public function getReminders($params = [])
  188. {
  189. return $this->reminderStore
  190. ->setMerchant($this->merchantResolver->getCurrent())
  191. ->setSection($this->sectionResolver->getCurrent())
  192. ->get($params);
  193. }
  194. public function getUnits()
  195. {
  196. return $this->unitStore->getAsArray();
  197. }
  198. public function getTaxRates()
  199. {
  200. return $this->taxRateStore->setMerchant($this->merchantResolver->getCurrent())->getAsArray();
  201. }
  202. public function getReductionCartCodes()
  203. {
  204. //TODO mettre à jour une fois les repo fait
  205. return array();
  206. }
  207. public function getVisitorCurrent()
  208. {
  209. return $this->visitorResolver->getCurrent();
  210. }
  211. }