src/Controller/OfferController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\UserOffer;
  4. use App\Service\Mailer;
  5. use App\Service\OfferEntityService;
  6. use App\Service\StripeService;
  7. use Bluesquare\StorageBundle\Storage;
  8. use Bluesquare\ValidatorBundle\Validator;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\File\UploadedFile;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. class OfferController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/offres-emploi", name="offer.index", methods={"GET"})
  18.      */
  19.     public function index(Request $requestOfferEntityService $offerEntityServiceStripeService $stripeService)
  20.     {
  21.         if ($request->isXmlHttpRequest()) {
  22.             $search $request->query->get('search');
  23.             $offers $offerEntityService->searchFromPublished($search);
  24.             return $this->render('offer/search-offer.html.twig', [
  25.                 'offers' => $offers,
  26.             ]);
  27.         }
  28.         $subscriptionStatus $stripeService->getSubscriptionStatus($this->getUser());
  29.         return $this->render('offer/index.html.twig', [
  30.             'offers' => $offerEntityService->published(),
  31.             'userSubscriptionsStatus' => $subscriptionStatus,
  32.         ]);
  33.     }
  34.     /**
  35.      * @Route("/offres-emploi/{id}", name="offer.view", methods={"GET", "POST"})
  36.      */
  37.     public function view(OfferEntityService $offerEntityServiceEntityManagerInterface $manager$idValidator $validatorStorage $storageMailer $mailer)
  38.     {
  39.         $offer $offerEntityService->get($id);
  40.         if (!$offer) {
  41.             throw $this->createNotFoundException();
  42.         }
  43.         $visibility false;
  44.         if ($validator->post() && $this->getUser()) {
  45.             $validator->required('message');
  46.             if ($validator->hasFile('resume')) {
  47.                 $resume $validator->getFile('resume');
  48.                 $mime $resume->getClientMimeType();
  49.                 if (!\in_array($mime, ['image/png''image/jpeg''image/jpg''application/pdf'], true)) {
  50.                     $validator->error('resume''Format invalide (.png, .jpg. jpeg, .pdf autorisés)');
  51.                 }
  52.             }
  53.             if ($validator->hasFile('letter')) {
  54.                 $letter $validator->getFile('letter');
  55.                 $mime $letter->getClientMimeType();
  56.                 if (!\in_array($mime, ['image/png''image/jpeg''image/jpg''application/pdf'], true)) {
  57.                     $validator->error('letter''Format invalide (.doc, .pdf, .txt autorisés)');
  58.                 }
  59.             }
  60.             if ($validator->check()) {
  61.                 if (!$manager->getRepository(UserOffer::class)->findOneBy(['user' => $this->getUser(), 'offer' => $offer])) {
  62.                     $userOffer = new UserOffer();
  63.                     $userOffer->setUser($this->getUser());
  64.                     $userOffer->setOffer($offer);
  65.                     $userOffer->setAppliedAt(new \DateTime());
  66.                     $files = [];
  67.                     if ($validator->hasFile('resume')) {
  68.                         $files[] = $resume;
  69.                         if ($resume instanceof UploadedFile) {
  70.                             $userOffer->setResumeFilename($resume->getClientOriginalName());
  71.                             $userOffer->setResumeType($resume->getMimeType());
  72.                             $storage->store($userOffer'resume_file'$resume->getRealPath());
  73.                         }
  74.                     }
  75.                     if ($validator->hasFile('letter')) {
  76.                         $files[] = $letter;
  77.                     }
  78.                     if (!empty($offer->getContactEmail())) {
  79.                         $vars = [
  80.                             'offer' => $offer,
  81.                             'user' => $this->getUser(),
  82.                             'message' => nl2br(htmlspecialchars($validator->get('message'))),
  83.                         ];
  84.                         $mailer->send('appliance'$offer->getContactEmail(), $vars, ['%offer%' => $offer->getName()], [], $files);
  85.                     }
  86.                     $manager->persist($userOffer);
  87.                     $manager->flush();
  88.                 }
  89.             } else {
  90.                 $validator->keep();
  91.             }
  92.         }
  93.         if ($this->getUser()) {
  94.             if ($manager->getRepository(UserOffer::class)->findOneBy(['user' => $this->getUser(), 'offer' => $offer])) {
  95.                 $visibility true;
  96.             }
  97.         }
  98.         return $this->render('offer/offer.html.twig', [
  99.             'offer' => $offerEntityService->format($offer),
  100.             'candidated' => $visibility,
  101.             'validator' => $validator,
  102.         ]);
  103.     }
  104. }