← Back
Editing: SolutionService.php
<?php namespace App\Service; use App\Entity\Solution; use App\Repository\SolutionRepository; use Symfony\Component\HttpFoundation\RequestStack; class SolutionService { private ?Solution $current = null; public function __construct( private readonly string $solution, private readonly array $solutions, private readonly SolutionRepository $solutionRepository, private readonly RequestStack $requestStack ) { } public function init(): void { if ( !$this->setSolutionFromEnv() && !$this->setSolutionFromDomain() && !$this->setSolutionFromHeader() ) { $this->setSolutionFromDefault(); } } private function setSolutionFromEnv(): ?Solution { $solution = null; if ($this->requestStack->getCurrentRequest()) { $request = $this->requestStack->getCurrentRequest(); $domain = $request->getHost(); foreach ($this->solutions as $solutionKey => $solutionValue) { if ($solutionValue['domain'] === $domain) { $solution = $this->solutionRepository->findOneBy(['slug' => $solutionValue['slug']]); break; } } $this->current = $solution; } return $solution; } private function setSolutionFromDomain(): ?Solution { $solution = null; if ($this->requestStack->getCurrentRequest()) { $request = $this->requestStack->getCurrentRequest(); $domain = $request->getHost(); $solution = $this->solutionRepository->findOneBy(['domain' => $domain]); $this->current = $solution; } return $solution; } private function setSolutionFromDefault(): void { $slug = $this->solution; $this->current = $this->solutionRepository->findOneBy(['slug' => $slug]); } public function getCurrent(): ?Solution { if (!$this->current) { $this->init(); } return $this->current; } private function setSolutionFromHeader(): ?Solution { $solution = null; if ($this->requestStack->getCurrentRequest()) { $request = $this->requestStack->getCurrentRequest(); if ($request->headers->has('X-Api-Solution')) { $solution = $this->solutionRepository->findOneBy(['name' => $request->headers->get('X-Api-Solution')]); $this->current = $solution; } } return $solution; } public function setCurrent($current): void { $this->current = $current; } /** * @return string[] */ public function getSections(): array { if ('clean-manager' === $this->current->getSlug()) { return [ 'PROPRETE' => 'Propreté', 'SECURITE' => 'Sécurité', 'INFORMATION' => 'Information et éclairage', 'PARKING' => 'Parking', ]; } else { return [ 'CONTRÔLES PREVENTIFS' => 'Contrôles préventifs', ]; } } public function getSolutionBySlug(string $slug): Solution { return $this->solutionRepository->findOneBy(['slug' => $slug]); } }
Save File
Cancel