src\Manager\Admin\UserProduitManager.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Manager\Admin;
  3. use App\Entity\Admin\Produit;
  4. use App\Entity\Admin\Proprietaire;
  5. use App\Entity\Admin\User;
  6. use App\Entity\Admin\UserProduit;
  7. use App\Repository\Admin\UserProduitRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\Persistence\ObjectRepository;
  10. use Symfony\Component\HttpFoundation\Request;
  11. class UserProduitManager
  12. {
  13.     public function __construct(private readonly EntityManagerInterface $entityManager) {}
  14.     public function getRepository(): UserProduitRepository
  15.     {
  16.         return $this->entityManager->getRepository(UserProduit::class);
  17.     }
  18.     private function save(UserProduit $userProduit): UserProduit
  19.     {
  20.         $this->entityManager->persist($userProduit);
  21.         $this->entityManager->flush();
  22.         return $userProduit;
  23.     }
  24.     public function insert(User $user, Produit $profil, bool $isActive, ?bool $hasDbAuth, ?Proprietaire $defaultProprietaire): UserProduit
  25.     {
  26.         $userProduit = new UserProduit();
  27.         $userProduit
  28.             ->setUser($user)
  29.             ->setProduit($profil)
  30.             ->setIsActive($isActive)
  31.             ->setDefaultProprietaire($defaultProprietaire)
  32.             ->setHasDbAuth($hasDbAuth ? true : false);
  33.         return $this->save($userProduit);
  34.     }
  35.     public function delete(UserProduit $userProduit): void
  36.     {
  37.         $this->entityManager->remove($userProduit);
  38.         $this->entityManager->flush();
  39.     }
  40.     public function update(UserProduit $userProduit): UserProduit
  41.     {
  42.         return $this->save($userProduit);
  43.     }
  44.     public function checkAndInsert(User $user, Produit $produit, bool $isActive = false, ?bool $hasDbAuth = false, bool $insert = true, ?int $defaultProprietaire): ?UserProduit
  45.     {
  46.         $userProduit = $this->getRepository()->findOneBy(['user' => $user, 'produit' => $produit]);
  47.         if (!empty($defaultProprietaire)) {
  48.             $defaultProprietaire = $this->entityManager->getRepository(Proprietaire::class)->find($defaultProprietaire);
  49.         }
  50.         if ($userProduit) {
  51.             if ($insert) {
  52.                 if (!is_null($hasDbAuth)) $userProduit->setHasDbAuth($hasDbAuth);
  53.                 $userProduit->setDefaultProprietaire($defaultProprietaire);
  54.                 $userProduit->setIsActive($isActive);
  55.                 $userProduit = $this->update($userProduit);
  56.             }
  57.         } else {
  58.             $userProduit = $this->insert($user, $produit, $isActive, $hasDbAuth, $defaultProprietaire);
  59.         }
  60.         return $userProduit;
  61.     }
  62.     public function processRequest(User $user, Produit $produit, Request $request, bool $okForm): ?UserProduit
  63.     {
  64.         // Réglages
  65.         $hasDbAuth = $request->get('user_reglages_hasDbAuth_' . $produit->getId());
  66.         $hasDbAuth = ((@$hasDbAuth == 'on' || @$hasDbAuth == '1') ? 1 : 0);
  67.         $defaultProprietaire = $request->get('user_reglages_defaultProprietaire_' . $produit->getId());
  68.         $isActiveProduit = $request->get('user_produit_' . $produit->getId());
  69.         $isActiveProduit = ((@$isActiveProduit == 'on' || @$isActiveProduit == '1') ? 1 : 0);
  70.         return $this->checkAndInsert($user, $produit, $isActiveProduit, $hasDbAuth, $okForm, $defaultProprietaire);
  71.     }
  72. }