<?php
namespace App\Services;
use App\Entity\CoachingSession;
use App\Entity\CodePromos;
use App\Entity\CoursLangue;
use App\Entity\Elearning;
use App\Entity\LiveSession;
use App\Entity\Post;
use App\Entity\PostCategory;
use App\Entity\StageAcademy;
use App\Entity\Subscription;
use App\Entity\VideoPro;
use DateTimeInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\String\Slugger\SluggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class SacoManager
{
/** @var FileUploader */
private $fileUploader;
/** @var SluggerInterface */
private $slugger;
/** @var EntityManagerInterface */
private $em;
/** @var string */
private $apiLogin = 'sacofrance@sacoeducation.com';
/** @var string */
private $apiPassword = 't48^ehXBdBk9^gVLr2eW#C^y';
/** @var string */
private $apiUrl = 'https://www.sacoeducation.com/api';
/** @var string */
private $apiShopUrl = 'https://shop.sacohair.fr';
/** @var HttpClientInterface */
private $httpClient;
/** @var ParameterBagInterface $params */
private $params;
public function __construct(FileUploader $fileUploader, SluggerInterface $slugger, EntityManagerInterface $entityManager, HttpClientInterface $httpClient, ParameterBagInterface $parameterBag)
{
$this->fileUploader = $fileUploader;
$this->slugger = $slugger;
$this->em = $entityManager;
$this->httpClient = $httpClient;
$this->params = $parameterBag;
}
/**
* @param StageAcademy $stageAcademy
* @param Form $formFactory
* @return StageAcademy
*/
public function stageAcademyUpdate(StageAcademy $stageAcademy, Form $formFactory): StageAcademy
{
// UPLOAD IMAGE
$image = $formFactory->get('image')->getData();
if ($image) {
$fileName = $this->fileUploader->upload($image);
$stageAcademy->setImage($fileName);
}
// UPLOAD PDF BROCHURE
$pdf = $formFactory->get('pdf')->getData();
if ($pdf) {
$fileName = $this->fileUploader->upload($pdf);
$stageAcademy->setPdf($fileName);
}
// UPLOAD IMAGE BROCHURE
$imageBrochure = $formFactory->get('brochureImage')->getData();
if ($imageBrochure) {
$fileName = $this->fileUploader->upload($imageBrochure);
$stageAcademy->setBrochureImage($fileName);
}
// SLUGIFY TITLE
if (!$stageAcademy->getId()) {
$stageAcademy->setSlug(strtolower($this->slugger->slug($stageAcademy->getCoach().'-'.$stageAcademy->getTitle())));
}
return $stageAcademy;
}
/**
* @param Elearning $elearning
* @param Form $formFactory
* @return Elearning
*/
public function elearningUpdate(Elearning $elearning, Form $formFactory): Elearning
{
// UPLOAD IMAGE
$image = $formFactory->get('image')->getData();
if ($image) {
$fileName = $this->fileUploader->upload($image);
$elearning->setImage($fileName);
}
// UPLOAD PDF BROCHURE
$video = $formFactory->get('video')->getData();
if ($video) {
$fileName = $this->fileUploader->upload($video);
$elearning->setVideo($fileName);
}
// SLUGIFY TITLE
if (!$elearning->getId()) {
$elearning->setSlug(strtolower($this->slugger->slug($elearning->getTitle())));
}
return $elearning;
}
/**
* @param VideoPro $elearning
* @param Form $formFactory
* @return VideoPro
*/
public function videoProUpdate(VideoPro $elearning, Form $formFactory): VideoPro
{
// UPLOAD IMAGE
$image = $formFactory->get('image')->getData();
if ($image) {
if(!is_null($elearning->getId()) && !is_null($elearning->getImage())) {
unlink($this->params->get('kernel.project_dir').'/public/uploads/'.$elearning->getImage());
}
$fileName = $this->fileUploader->upload($image);
$elearning->setImage($fileName);
}
// UPLOAD PDF BROCHURE
$video = $formFactory->get('video')->getData();
if ($video) {
if(!is_null($elearning->getId()) && !is_null($elearning->getVideo())) {
unlink($this->params->get('kernel.project_dir').'/public/uploads/'.$elearning->getVideo());
}
$fileName = $this->fileUploader->upload($video);
$elearning->setVideo($fileName);
}
// SLUGIFY TITLE
if (!$elearning->getId()) {
$elearning->setSlug(strtolower($this->slugger->slug($elearning->getTitle())));
}
return $elearning;
}
/**
* @param LiveSession $live
* @param Form $formFactory
* @return LiveSession
*/
public function formationLiveUpdate(LiveSession $live, Form $formFactory): LiveSession
{
// UPLOAD IMAGE
$image = $formFactory->get('image')->getData();
if ($image) {
$fileName = $this->fileUploader->upload($image);
$live->setImage($fileName);
}
$video = $formFactory->get('video')->getData();
if ($video) {
$fileName = $this->fileUploader->upload($video);
$live->setVideo($fileName);
}
// SLUGIFY TITLE
if (!$live->getId()) {
$live->setSlug(strtolower($this->slugger->slug($live->getTitle())));
}
return $live;
}
/**
* @param CoachingSession $coaching
* @param Form $formFactory
* @return CoachingSession
*/
public function coachingLiveUpdate(CoachingSession $coaching, Form $formFactory): CoachingSession
{
// UPLOAD IMAGE
$image = $formFactory->get('image')->getData();
if ($image) {
$fileName = $this->fileUploader->upload($image);
$coaching->setImage($fileName);
}
// UPLOAD IMAGE DEROULEMENT
$image = $formFactory->get('imageDeroulement')->getData();
if ($image) {
$fileName = $this->fileUploader->upload($image);
$coaching->setImageDeroulement($fileName);
}
// SLUGIFY TITLE
if (!$coaching->getId()) {
$coaching->setSlug(strtolower($this->slugger->slug($coaching->getTitle())));
}
return $coaching;
}
/**
* @param CoursLangue $cours
* @param Form $formFactory
* @return CoursLangue
*/
public function coursLangueUpdate(CoursLangue $cours, Form $formFactory): CoursLangue
{
// UPLOAD IMAGE
$image = $formFactory->get('image')->getData();
if ($image) {
$fileName = $this->fileUploader->upload($image);
$cours->setImage($fileName);
}
// UPLOAD IMAGE
$image = $formFactory->get('cover')->getData();
if ($image) {
$fileName = $this->fileUploader->upload($image);
$cours->setCover($fileName);
}
// UPLOAD IMAGE DEROULEMENT
$image = $formFactory->get('imageDeroulement')->getData();
if ($image) {
$fileName = $this->fileUploader->upload($image);
$cours->setImageDeroulement($fileName);
}
// UPLOAD PDF BROCHURE
$video = $formFactory->get('video')->getData();
if ($video) {
$fileName = $this->fileUploader->upload($video);
$cours->setVideo($fileName);
}
// SLUGIFY TITLE
if (!$cours->getId()) {
$cours->setSlug(strtolower($this->slugger->slug($cours->getTitle())));
}
return $cours;
}
/**
* @param Post $post
* @param Form $formFactory
* @param int|null $category
* @param EntityManagerInterface $em
* @param UserInterface $user
* @return Post
*/
public function postUpdate(Post $post, Form $formFactory, ?int $category = null, EntityManagerInterface $em, UserInterface $user): Post
{
// UPLOAD IMAGE
$image = $formFactory->get('image')->getData();
if ($image) {
$fileName = $this->fileUploader->upload($image);
$post->setImage($fileName);
}
$video = $formFactory->get('video')->getData();
if ($video) {
$fileName = $this->fileUploader->upload($video);
$post->setVideo($fileName);
}
if ($category) {
$category = $em->getRepository(PostCategory::class)->find($category);
if ($category) $post->setCategory($category);
} else {
$post->setCategory(NULL);
}
// SLUGIFY TITLE
if (!$post->getId()) {
$post->setSlug(strtolower($this->slugger->slug($post->getTitle())));
$post->setAuthor($user);
}
return $post;
}
/**
* @param string $codeReduction
* @return array
*/
public function checkCodePromos(string $codeReduction): array
{
$code = $this->em->getRepository(CodePromos::class)->findOneByCode($codeReduction);
if($code && $code->getIsActive()) {
return [
'success' => true,
];
} elseif($code && !$code->getIsActive()) {
return [
'success' => false,
'message' => 'Le code à expiré.'
];
} else {
$subscription = $this->em->getRepository(Subscription::class)->findOneByAttachedAccountCode($codeReduction);
if($subscription && $subscription->getIsActiveCode()) {
return [
'success' => true,
];
} elseif($subscription && !$subscription->getIsActiveCode()) {
return [
'success' => false,
'message' => 'Ce code à atteigné sa lmimite des comptes : '.$subscription->getAttachedAccountLimit()
];
} else {
return [
'success' => false,
'message' => 'Le code n\'existe pas.'
];
}
}
}
/**
* Connexion a l'api du site sacoeducation.com
*
* @return null|string
*/
private function sacoApiLogin(): ?string
{
$token = $this->httpClient->request('POST', $this->apiUrl.'/login', [
'body' => [
'email' => $this->apiLogin,
'password' => $this->apiPassword
]
]);
$result = json_decode($token->getContent());
if(!$result)
return null;
return $result->result->passkey;
}
/**
* GET LIST OF CODE PROMOS IN WEB SITE sacoeducation.com
* @return array|null
*/
public function sacoApiGetCodePromos(): ?array
{
$promos = $this->httpClient->request('GET', $this->apiUrl.'/promocodelists', [
'headers' => [
'authkey' => $this->sacoApiLogin()
]
]);
$result = json_decode($promos->getContent());
if($result->error) return null;
return $result->result;
}
/**
* Création d'un code promos sur le site sacoeducation.com
*
* @param string $name
* @param int $periode
* @param DateTimeInterface $dateStart
* @param DateTimeInterface $dateEnd
* @return bool|null
*/
public function sacoApiAddCodePromo(string $name, int $periode, DateTimeInterface $dateStart, DateTimeInterface $dateEnd): ?bool
{
$code = $this->httpClient->request('POST', $this->apiUrl.'/addpromocoade', [
'headers' => [
'authkey' => $this->sacoApiLogin()
],
'body' => [
'name' => $name,
'free_period' => $periode,
'start_date' => $dateStart->format('Y-m-d'),
'end_date' => $dateEnd->format('Y-m-d'),
]
]);
$result = json_decode($code->getContent());
if($result->error) return false;
return true;
}
/**
* Création d'un code promos sur le shop shop.sacohair.fr
*
* @param string $name
* @param int $periode
* @param DateTimeInterface $dateStart
* @param DateTimeInterface $dateEnd
* @return bool|null
*/
public function sacoApiAddCodePromoToShop(string $name, float $percent, DateTimeInterface $dateStart, DateTimeInterface $dateEnd): ?bool
{
$this->httpClient->request('POST', $this->apiShopUrl.'/kwantic-coupon', [
'body' => [
'name' => $name,
'percent' => $percent,
'start_date' => $dateStart->format('Y-m-d'),
'end_date' => $dateEnd->format('Y-m-d'),
]
]);
return true;
}
}