src/Services/SacoManager.php line 262

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\CoachingSession;
  4. use App\Entity\CodePromos;
  5. use App\Entity\CoursLangue;
  6. use App\Entity\Elearning;
  7. use App\Entity\LiveSession;
  8. use App\Entity\Post;
  9. use App\Entity\PostCategory;
  10. use App\Entity\StageAcademy;
  11. use App\Entity\Subscription;
  12. use App\Entity\VideoPro;
  13. use DateTimeInterface;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  16. use Symfony\Component\Form\Form;
  17. use Symfony\Component\Security\Core\User\UserInterface;
  18. use Symfony\Component\String\Slugger\SluggerInterface;
  19. use Symfony\Contracts\HttpClient\HttpClientInterface;
  20. class SacoManager
  21. {
  22.     /** @var  FileUploader */
  23.     private $fileUploader;
  24.     /** @var  SluggerInterface */
  25.     private $slugger;
  26.     /** @var  EntityManagerInterface */
  27.     private $em;
  28.     /** @var string  */
  29.     private $apiLogin 'sacofrance@sacoeducation.com';
  30.     /** @var string  */
  31.     private $apiPassword 't48^ehXBdBk9^gVLr2eW#C^y';
  32.     /** @var string  */
  33.     private $apiUrl 'https://www.sacoeducation.com/api';
  34.     /** @var string  */
  35.     private $apiShopUrl 'https://shop.sacohair.fr';
  36.     /** @var  HttpClientInterface */
  37.     private $httpClient;
  38.     /** @var  ParameterBagInterface $params */
  39.     private $params;
  40.     public function __construct(FileUploader $fileUploaderSluggerInterface $sluggerEntityManagerInterface $entityManagerHttpClientInterface $httpClientParameterBagInterface $parameterBag)
  41.     {
  42.         $this->fileUploader $fileUploader;
  43.         $this->slugger $slugger;
  44.         $this->em $entityManager;
  45.         $this->httpClient $httpClient;
  46.         $this->params $parameterBag;
  47.     }
  48.     /**
  49.      * @param StageAcademy $stageAcademy
  50.      * @param Form $formFactory
  51.      * @return StageAcademy
  52.      */
  53.     public function stageAcademyUpdate(StageAcademy $stageAcademyForm $formFactory): StageAcademy
  54.     {
  55.         // UPLOAD IMAGE
  56.         $image $formFactory->get('image')->getData();
  57.         if ($image) {
  58.             $fileName $this->fileUploader->upload($image);
  59.             $stageAcademy->setImage($fileName);
  60.         }
  61.         // UPLOAD PDF BROCHURE
  62.         $pdf $formFactory->get('pdf')->getData();
  63.         if ($pdf) {
  64.             $fileName $this->fileUploader->upload($pdf);
  65.             $stageAcademy->setPdf($fileName);
  66.         }
  67.         // UPLOAD IMAGE BROCHURE
  68.         $imageBrochure $formFactory->get('brochureImage')->getData();
  69.         if ($imageBrochure) {
  70.             $fileName $this->fileUploader->upload($imageBrochure);
  71.             $stageAcademy->setBrochureImage($fileName);
  72.         }
  73.         // SLUGIFY TITLE
  74.         if (!$stageAcademy->getId()) {
  75.             $stageAcademy->setSlug(strtolower($this->slugger->slug($stageAcademy->getCoach().'-'.$stageAcademy->getTitle())));
  76.         }
  77.         return $stageAcademy;
  78.     }
  79.     /**
  80.      * @param Elearning $elearning
  81.      * @param Form $formFactory
  82.      * @return Elearning
  83.      */
  84.     public function elearningUpdate(Elearning $elearningForm $formFactory): Elearning
  85.     {
  86.         // UPLOAD IMAGE
  87.         $image $formFactory->get('image')->getData();
  88.         if ($image) {
  89.             $fileName $this->fileUploader->upload($image);
  90.             $elearning->setImage($fileName);
  91.         }
  92.         // UPLOAD PDF BROCHURE
  93.         $video $formFactory->get('video')->getData();
  94.         if ($video) {
  95.             $fileName $this->fileUploader->upload($video);
  96.             $elearning->setVideo($fileName);
  97.         }
  98.         // SLUGIFY TITLE
  99.         if (!$elearning->getId()) {
  100.             $elearning->setSlug(strtolower($this->slugger->slug($elearning->getTitle())));
  101.         }
  102.         return $elearning;
  103.     }
  104.     /**
  105.      * @param VideoPro $elearning
  106.      * @param Form $formFactory
  107.      * @return VideoPro
  108.      */
  109.     public function videoProUpdate(VideoPro $elearningForm $formFactory): VideoPro
  110.     {
  111.         // UPLOAD IMAGE
  112.         $image $formFactory->get('image')->getData();
  113.         if ($image) {
  114.             if(!is_null($elearning->getId()) && !is_null($elearning->getImage())) {
  115.                 unlink($this->params->get('kernel.project_dir').'/public/uploads/'.$elearning->getImage());
  116.             }
  117.             $fileName $this->fileUploader->upload($image);
  118.             $elearning->setImage($fileName);
  119.         }
  120.         // UPLOAD PDF BROCHURE
  121.         $video $formFactory->get('video')->getData();
  122.         if ($video) {
  123.             if(!is_null($elearning->getId()) && !is_null($elearning->getVideo())) {
  124.                 unlink($this->params->get('kernel.project_dir').'/public/uploads/'.$elearning->getVideo());
  125.             }
  126.             $fileName $this->fileUploader->upload($video);
  127.             $elearning->setVideo($fileName);
  128.         }
  129.         // SLUGIFY TITLE
  130.         if (!$elearning->getId()) {
  131.             $elearning->setSlug(strtolower($this->slugger->slug($elearning->getTitle())));
  132.         }
  133.         return $elearning;
  134.     }
  135.     /**
  136.      * @param LiveSession $live
  137.      * @param Form $formFactory
  138.      * @return LiveSession
  139.      */
  140.     public function formationLiveUpdate(LiveSession $liveForm $formFactory): LiveSession
  141.     {
  142.         // UPLOAD IMAGE
  143.         $image $formFactory->get('image')->getData();
  144.         if ($image) {
  145.             $fileName $this->fileUploader->upload($image);
  146.             $live->setImage($fileName);
  147.         }
  148.         $video $formFactory->get('video')->getData();
  149.         if ($video) {
  150.             $fileName $this->fileUploader->upload($video);
  151.             $live->setVideo($fileName);
  152.         }
  153.         // SLUGIFY TITLE
  154.         if (!$live->getId()) {
  155.             $live->setSlug(strtolower($this->slugger->slug($live->getTitle())));
  156.         }
  157.         return $live;
  158.     }
  159.     /**
  160.      * @param CoachingSession $coaching
  161.      * @param Form $formFactory
  162.      * @return CoachingSession
  163.      */
  164.     public function coachingLiveUpdate(CoachingSession $coachingForm $formFactory): CoachingSession
  165.     {
  166.         // UPLOAD IMAGE
  167.         $image $formFactory->get('image')->getData();
  168.         if ($image) {
  169.             $fileName $this->fileUploader->upload($image);
  170.             $coaching->setImage($fileName);
  171.         }
  172.         // UPLOAD IMAGE DEROULEMENT
  173.         $image $formFactory->get('imageDeroulement')->getData();
  174.         if ($image) {
  175.             $fileName $this->fileUploader->upload($image);
  176.             $coaching->setImageDeroulement($fileName);
  177.         }
  178.         // SLUGIFY TITLE
  179.         if (!$coaching->getId()) {
  180.             $coaching->setSlug(strtolower($this->slugger->slug($coaching->getTitle())));
  181.         }
  182.         return $coaching;
  183.     }
  184.     /**
  185.      * @param CoursLangue $cours
  186.      * @param Form $formFactory
  187.      * @return CoursLangue
  188.      */
  189.     public function coursLangueUpdate(CoursLangue $coursForm $formFactory): CoursLangue
  190.     {
  191.         // UPLOAD IMAGE
  192.         $image $formFactory->get('image')->getData();
  193.         if ($image) {
  194.             $fileName $this->fileUploader->upload($image);
  195.             $cours->setImage($fileName);
  196.         }
  197.         // UPLOAD IMAGE
  198.         $image $formFactory->get('cover')->getData();
  199.         if ($image) {
  200.             $fileName $this->fileUploader->upload($image);
  201.             $cours->setCover($fileName);
  202.         }
  203.         // UPLOAD IMAGE DEROULEMENT
  204.         $image $formFactory->get('imageDeroulement')->getData();
  205.         if ($image) {
  206.             $fileName $this->fileUploader->upload($image);
  207.             $cours->setImageDeroulement($fileName);
  208.         }
  209.         // UPLOAD PDF BROCHURE
  210.         $video $formFactory->get('video')->getData();
  211.         if ($video) {
  212.             $fileName $this->fileUploader->upload($video);
  213.             $cours->setVideo($fileName);
  214.         }
  215.         // SLUGIFY TITLE
  216.         if (!$cours->getId()) {
  217.             $cours->setSlug(strtolower($this->slugger->slug($cours->getTitle())));
  218.         }
  219.         return $cours;
  220.     }
  221.     /**
  222.      * @param Post $post
  223.      * @param Form $formFactory
  224.      * @param int|null $category
  225.      * @param EntityManagerInterface $em
  226.      * @param UserInterface $user
  227.      * @return Post
  228.      */
  229.     public function postUpdate(Post $postForm $formFactory, ?int $category nullEntityManagerInterface $emUserInterface $user): Post
  230.     {
  231.         // UPLOAD IMAGE
  232.         $image $formFactory->get('image')->getData();
  233.         if ($image) {
  234.             $fileName $this->fileUploader->upload($image);
  235.             $post->setImage($fileName);
  236.         }
  237.         $video $formFactory->get('video')->getData();
  238.         if ($video) {
  239.             $fileName $this->fileUploader->upload($video);
  240.             $post->setVideo($fileName);
  241.         }
  242.         if ($category) {
  243.             $category $em->getRepository(PostCategory::class)->find($category);
  244.             if ($category$post->setCategory($category);
  245.         } else {
  246.             $post->setCategory(NULL);
  247.         }
  248.         // SLUGIFY TITLE
  249.         if (!$post->getId()) {
  250.             $post->setSlug(strtolower($this->slugger->slug($post->getTitle())));
  251.             $post->setAuthor($user);
  252.         }
  253.         return $post;
  254.     }
  255.     /**
  256.      * @param string $codeReduction
  257.      * @return array
  258.      */
  259.     public function checkCodePromos(string $codeReduction): array
  260.     {
  261.         $code $this->em->getRepository(CodePromos::class)->findOneByCode($codeReduction);
  262.         if($code && $code->getIsActive()) {
  263.             return [
  264.                 'success' => true,
  265.             ];
  266.         } elseif($code && !$code->getIsActive()) {
  267.             return [
  268.                 'success' => false,
  269.                 'message' => 'Le code à expiré.'
  270.             ];
  271.         } else {
  272.             $subscription $this->em->getRepository(Subscription::class)->findOneByAttachedAccountCode($codeReduction);
  273.             if($subscription && $subscription->getIsActiveCode()) {
  274.                 return [
  275.                     'success' => true,
  276.                 ];
  277.             } elseif($subscription && !$subscription->getIsActiveCode()) {
  278.                 return [
  279.                     'success' => false,
  280.                     'message' => 'Ce code à atteigné sa lmimite des comptes : '.$subscription->getAttachedAccountLimit()
  281.                 ];
  282.             } else {
  283.                 return [
  284.                     'success' => false,
  285.                     'message' => 'Le code n\'existe pas.'
  286.                 ];
  287.             }
  288.         }
  289.     }
  290.     /**
  291.      * Connexion a l'api du site sacoeducation.com
  292.      *
  293.      * @return null|string
  294.      */
  295.     private function sacoApiLogin(): ?string
  296.     {
  297.         $token $this->httpClient->request('POST'$this->apiUrl.'/login', [
  298.             'body' => [
  299.                 'email' => $this->apiLogin,
  300.                 'password' => $this->apiPassword
  301.             ]
  302.         ]);
  303.         $result json_decode($token->getContent());
  304.         if(!$result)
  305.             return null;
  306.         return $result->result->passkey;
  307.     }
  308.     /**
  309.      * GET LIST OF CODE PROMOS IN WEB SITE sacoeducation.com
  310.      * @return array|null
  311.      */
  312.     public function sacoApiGetCodePromos(): ?array
  313.     {
  314.         $promos $this->httpClient->request('GET'$this->apiUrl.'/promocodelists', [
  315.             'headers' => [
  316.                 'authkey' => $this->sacoApiLogin()
  317.             ]
  318.         ]);
  319.         $result json_decode($promos->getContent());
  320.         if($result->error) return null;
  321.         return $result->result;
  322.     }
  323.     /**
  324.      * Création d'un code promos sur le site sacoeducation.com
  325.      *
  326.      * @param string $name
  327.      * @param int $periode
  328.      * @param DateTimeInterface $dateStart
  329.      * @param DateTimeInterface $dateEnd
  330.      * @return bool|null
  331.      */
  332.     public function sacoApiAddCodePromo(string $nameint $periodeDateTimeInterface $dateStartDateTimeInterface $dateEnd): ?bool
  333.     {
  334.         $code $this->httpClient->request('POST'$this->apiUrl.'/addpromocoade', [
  335.             'headers' => [
  336.                 'authkey' => $this->sacoApiLogin()
  337.             ],
  338.             'body' => [
  339.                 'name' => $name,
  340.                 'free_period' => $periode,
  341.                 'start_date' => $dateStart->format('Y-m-d'),
  342.                 'end_date' => $dateEnd->format('Y-m-d'),
  343.             ]
  344.         ]);
  345.         $result json_decode($code->getContent());
  346.         if($result->error) return false;
  347.         return true;
  348.     }
  349.     /**
  350.      * Création d'un code promos sur le shop shop.sacohair.fr
  351.      *
  352.      * @param string $name
  353.      * @param int $periode
  354.      * @param DateTimeInterface $dateStart
  355.      * @param DateTimeInterface $dateEnd
  356.      * @return bool|null
  357.      */
  358.     public function sacoApiAddCodePromoToShop(string $namefloat $percentDateTimeInterface $dateStartDateTimeInterface $dateEnd): ?bool
  359.     {
  360.         $this->httpClient->request('POST'$this->apiShopUrl.'/kwantic-coupon', [
  361.             'body' => [
  362.                 'name' => $name,
  363.                 'percent' => $percent,
  364.                 'start_date' => $dateStart->format('Y-m-d'),
  365.                 'end_date' => $dateEnd->format('Y-m-d'),
  366.             ]
  367.         ]);
  368.         return true;
  369.     }
  370. }