src/Services/Marketing/MarketingService.php line 378

Open in your IDE?
  1. <?php 
  2. namespace EADPlataforma\Services\Marketing;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EADPlataforma\Services\Marketing\RdStationService;
  5. use EADPlataforma\Services\Marketing\LaharService;
  6. use EADPlataforma\Services\Marketing\MailChimpService;
  7. use EADPlataforma\Services\Marketing\ActiveCampaignService;
  8. use EADPlataforma\Services\Marketing\EadStationService;
  9. use EADPlataforma\Services\SlackService;
  10. use EADPlataforma\Services\DiscordService;
  11. use EADPlataforma\Services\GeneralService;
  12. use EADPlataforma\Services\ConfigurationService;
  13. use EADPlataforma\Entity\User;
  14. use EADPlataforma\Entity\City;
  15. use EADPlataforma\Entity\State;
  16. use EADPlataforma\Entity\Country;
  17. use EADPlataforma\Entity\ProductOffer;
  18. use EADPlataforma\Enum\TagsMarketingEnum;
  19. use EADPlataforma\Enum\ProductOpportunityEnum;
  20.  
  21. class MarketingService
  22. {
  23.     /**
  24.      * @var RdStationService
  25.      */
  26.     protected $rdStationService;
  27.     /**
  28.      * @var LaharService
  29.      */
  30.     protected $laharService;
  31.     /**
  32.      * @var MailChimpService
  33.      */
  34.     protected $mailChimpService;
  35.     /**
  36.      * @var SlackService
  37.      */
  38.     protected $slackService;
  39.     /**
  40.      * @var DiscordService
  41.      */
  42.     protected $discordService;
  43.     /**
  44.      * @var ActiveCampaignService
  45.      */
  46.     protected $activeCampaignService;
  47.     /**
  48.      * @var EadStationService
  49.      */
  50.     protected $eadStationService;
  51.     /**
  52.      * @var ConfigurationService
  53.      */
  54.     private $configuration;
  55.     /**
  56.      * @var GeneralService
  57.      */
  58.     protected $generalService;
  59.     /**
  60.      * @var \Client
  61.      */
  62.     protected $client;
  63.     /**
  64.      * @var SchoolEntityManager $em
  65.      */
  66.     protected $em;
  67.     /**
  68.      * @var \User
  69.      */
  70.     protected $user;
  71.     /**
  72.      * @var \ProductOffer
  73.      */
  74.     protected $productOffer;
  75.     /**
  76.      * @var string
  77.      */
  78.     protected $email;
  79.     /**
  80.      * @var string
  81.      */
  82.     protected $phone null;
  83.      /**
  84.      * @var string
  85.      */
  86.     protected $birthday null;
  87.     /**
  88.      * @var string
  89.      */
  90.     protected $bio null;
  91.     /**
  92.      * @var string
  93.      */
  94.     protected $city null;
  95.     /**
  96.      * @var string
  97.      */
  98.     protected $state null;
  99.     /**
  100.      * @var string
  101.      */
  102.     protected $country null;
  103.     /**
  104.      * @var string
  105.      */
  106.     protected $website null;
  107.     /**
  108.      * @var string
  109.      */
  110.     protected $twitter null;
  111.     /**
  112.      * @var string
  113.      */
  114.     protected $linkedin null;
  115.     /**
  116.      * @var string
  117.      */
  118.     protected $facebook null;
  119.     /**
  120.      * @var int
  121.      */
  122.     protected $tag TagsMarketingEnum::TAG_VISITED_PAGE;
  123.     /**
  124.      * @var int
  125.      */
  126.     protected $opportunity null;
  127.     /**
  128.      * @var string
  129.      */
  130.     protected $textComplement '';
  131.     /**
  132.      * @var Bool
  133.      */
  134.     protected $debug false;
  135.     /**
  136.      * Constructor
  137.      *
  138.      * @param RdStationService $rdStationService
  139.      * @param LaharService $laharService
  140.      * @param MailChimpService $mailChimpService
  141.      * @param ActiveCampaignService $activeCampaignService
  142.      * @param EadStationService $eadStationService
  143.      * @param SlackService $slackService
  144.      * @param DiscordService $discordService
  145.      * @param ConfigurationService $configuration
  146.      * @param GeneralService $generalService
  147.      */
  148.     public function __construct(
  149.         RdStationService $rdStationService
  150.         LaharService $laharService
  151.         MailChimpService $mailChimpService
  152.         ActiveCampaignService $activeCampaignService,
  153.         EadStationService $eadStationService
  154.         SlackService $slackService,
  155.         DiscordService $discordService,
  156.         ConfigurationService $configuration,
  157.         GeneralService $generalService
  158.     )
  159.     {
  160.         $this->rdStationService $rdStationService;
  161.         $this->laharService $laharService;
  162.         $this->mailChimpService $mailChimpService;
  163.         $this->activeCampaignService $activeCampaignService;
  164.         $this->eadStationService $eadStationService;
  165.         $this->slackService $slackService;
  166.         $this->discordService $discordService;
  167.         $this->configuration $configuration;
  168.         $this->client $this->configuration->getClient();
  169.         $this->generalService $generalService;
  170.         $this->em $this->generalService->getService('SchoolEntityManager');
  171.     }
  172.     public function setDebug(?bool $debug)
  173.     {
  174.         $this->debug $debug;
  175.         return $this;
  176.     }
  177.     public function getTagInfo()
  178.     {
  179.         $info = [
  180.             TagsMarketingEnum::TAG_REGISTERED_EAD => (object)[
  181.                 "flag" => 'user',
  182.                 "textTag" => 'Cadastrou-se no EAD',
  183.                 "actionLahar" => 'Cadastro',
  184.             ],
  185.             TagsMarketingEnum::TAG_CONFIRM_REGISTER => (object)[
  186.                 "flag" => 'user',
  187.                 "textTag" => 'Confirmou cadastro no EAD',
  188.                 "actionLahar" => 'Cadastro',
  189.             ],
  190.             TagsMarketingEnum::TAG_SIGNUP_NEWSLETTER => (object)[
  191.                 "flag" => 'user',
  192.                 "textTag" => 'Assinou Newsletter',
  193.                 "actionLahar" => 'Cadastro',
  194.             ],
  195.             TagsMarketingEnum::TAG_REGISTERED_EAD_FACEBOOK => (object)[
  196.                 "flag" => 'user',
  197.                 "textTag" => 'Cadastrou-se no EAD com Facebook',
  198.                 "actionLahar" => 'Cadastro',
  199.             ],
  200.             TagsMarketingEnum::TAG_REGISTERED_EAD_GOOGLE => (object)[
  201.                 "flag" => 'user',
  202.                 "textTag" => 'Cadastrou-se no EAD com Google',
  203.                 "actionLahar" => 'Cadastro',
  204.             ],
  205.             TagsMarketingEnum::TAG_VISITED_COURSE_PAGE => (object)[
  206.                 "flag" => 'visit',
  207.                 "textTag" => 'Visitou Página: ',
  208.                 "actionLahar" => 'Visitou Página',
  209.             ],
  210.             TagsMarketingEnum::TAG_ADD_CART => (object)[
  211.                 "flag" => 'product',
  212.                 "textTag" => 'Adicionou ao Carrinho: ',
  213.                 "actionLahar" => 'Adicionou ao Carrinho',
  214.             ],
  215.             TagsMarketingEnum::TAG_REMOVE_CART => (object)[
  216.                 "flag" => 'product',
  217.                 "textTag" => 'Removeu do Carrinho: ',
  218.                 "actionLahar" => 'Removeu do Carrinho',
  219.             ],
  220.             TagsMarketingEnum::TAG_ADD_DESIRE_LIST => (object)[
  221.                 "flag" => 'product',
  222.                 "textTag" => 'Lista Desejo: ',
  223.                 "actionLahar" => 'Lista de Desejo',
  224.             ],
  225.             TagsMarketingEnum::TAG_ADD_WAIT_LIST => (object)[
  226.                 "flag" => 'product',
  227.                 "textTag" => 'Lista de espera: ',
  228.                 "actionLahar" => 'Lista de Espera',
  229.             ],
  230.             TagsMarketingEnum::TAG_VISITED_PAGE => (object)[
  231.                 "flag" => 'visit',
  232.                 "textTag" => 'Visitou Página: ',
  233.                 "actionLahar" => 'Visitou Página',
  234.             ],
  235.             TagsMarketingEnum::TAG_ISSUE_CERTIFICATE => (object)[
  236.                 "flag" => 'course',
  237.                 "textTag" => 'Emitiu Certificado: ',
  238.                 "actionLahar" => 'Emitiu Certificado',
  239.             ],
  240.             TagsMarketingEnum::TAG_BEGIN_COURSE => (object)[
  241.                 "flag" => 'course',
  242.                 "textTag" => 'Iniciou o curso: ',
  243.                 "actionLahar" => 'Cadastro',
  244.             ],
  245.             TagsMarketingEnum::TAG_FINISH_COURSE => (object)[
  246.                 "flag" => 'course',
  247.                 "textTag" => 'Concluiu o curso: ',
  248.                 "actionLahar" => 'Concluiu Curso',
  249.             ],
  250.             TagsMarketingEnum::TAG_WAITING_PAYMENT => (object)[
  251.                 "flag" => 'payment',
  252.                 "textTag" => 'Aguardando Pagamento: ',
  253.                 "actionLahar" => 'Matrícula',
  254.             ],
  255.             TagsMarketingEnum::TAG_PAYMENT_APPROVED => (object)[
  256.                 "flag" => 'payment',
  257.                 "textTag" => 'Pagamento Aprovado: ',
  258.                 "actionLahar" => 'Matrícula',
  259.             ],
  260.             TagsMarketingEnum::TAG_PAYMENT_CANCELED => (object)[
  261.                 "flag" => 'payment',
  262.                 "textTag" => 'Pagamento Cancelado: ',
  263.                 "actionLahar" => 'Matrícula',
  264.             ],
  265.             TagsMarketingEnum::TAG_PAYMENT_DISPUTE => (object)[
  266.                 "flag" => 'payment',
  267.                 "textTag" => 'Pagamento em disputa: ',
  268.                 "actionLahar" => 'Matrícula',
  269.             ],
  270.             TagsMarketingEnum::TAG_PAYMENT_REPAYMENT => (object)[
  271.                 "flag" => 'payment',
  272.                 "textTag" => 'Reembolso: ',
  273.                 "actionLahar" => 'Matrícula',
  274.             ],
  275.             TagsMarketingEnum::TAG_PAYMENT_REPAYMENT_PROGRESS => (object)[
  276.                 "flag" => 'payment',
  277.                 "textTag" => 'Estorno em andamento: ',
  278.                 "actionLahar" => 'Matrícula',
  279.             ],
  280.             TagsMarketingEnum::TAG_PAYMENT_CHARGEBACK => (object)[
  281.                 "flag" => 'payment',
  282.                 "textTag" => 'Chargeback: ',
  283.                 "actionLahar" => 'Matrícula',
  284.             ],
  285.             TagsMarketingEnum::TAG_START_SUBSCRIPTION => (object)[
  286.                 "flag" => 'subscription',
  287.                 "textTag" => 'Assinou o plano: ',
  288.                 "actionLahar" => 'Matrícula',
  289.             ],
  290.             TagsMarketingEnum::TAG_CANCELED_SUBSCRIPTION => (object)[
  291.                 "flag" => 'subscription',
  292.                 "textTag" => 'Cancelou o plano: ',
  293.                 "actionLahar" => 'Cancelou plano'
  294.             ],
  295.             TagsMarketingEnum::TAG_BILL_EXPIRATION => (object)[
  296.                 "flag" => 'subscription',
  297.                 "textTag" => 'Boleto Vencido: ',
  298.                 "actionLahar" => 'Boleto Vencido',
  299.             ],
  300.             TagsMarketingEnum::TAG_PIX_EXPIRATION => (object)[
  301.                 "flag" => 'subscription',
  302.                 "textTag" => 'Pix Vencido: ',
  303.                 "actionLahar" => 'PIX Vencido',
  304.             ],
  305.             TagsMarketingEnum::TAG_TRIAL_PERIOD => (object)[
  306.                 "flag" => 'subscription',
  307.                 "textTag" => 'Período experimental: ',
  308.                 "actionLahar" => 'Período experimental',
  309.             ],
  310.             TagsMarketingEnum::TAG_ASSOCIATE_GROUP => (object)[
  311.                 "flag" => 'group',
  312.                 "textTag" => 'Participa do grupo: ',
  313.                 "actionLahar" => 'Cadastro',
  314.             ],
  315.             TagsMarketingEnum::TAG_ENROLLMENT_MANUALLY => (object)[
  316.                 "flag" => 'enrollment',
  317.                 "textTag" => 'Matriculado Manualmente: ',
  318.                 "actionLahar" => 'Matrícula',
  319.             ],
  320.             TagsMarketingEnum::TAG_ENROLLMENT_SUBSCRIPTION => (object)[
  321.                 "flag" => 'enrollment',
  322.                 "textTag" => 'Matriculado via assinatura: ',
  323.                 "actionLahar" => 'Matrícula',
  324.             ],
  325.             TagsMarketingEnum::TAG_ENROLLMENT_GROUP => (object)[
  326.                 "flag" => 'enrollment',
  327.                 "textTag" => 'Matriculado via grupo: ',
  328.                 "actionLahar" => 'Matrícula',
  329.             ],
  330.             TagsMarketingEnum::TAG_ENROLLMENT_COUPON => (object)[
  331.                 "flag" => 'enrollment',
  332.                 "textTag" => 'Matriculado via cupom: ',
  333.                 "actionLahar" => 'Matrícula',
  334.             ],
  335.             TagsMarketingEnum::TAG_FREE_COURSE_ACCESS => (object)[
  336.                 "flag" => 'enrollment',
  337.                 "textTag" => 'Acessou Curso Grátis: ',
  338.                 "actionLahar" => 'Cadastro',
  339.             ],
  340.         ];
  341.         return $info[$this->tag];
  342.     }
  343.     public function setUser(User $user)
  344.     {
  345.         $this->user $user;
  346.         return $this;
  347.     }
  348.     public function setProductOffer(ProductOffer $productOffer)
  349.     {
  350.         $this->productOffer $productOffer;
  351.         return $this;
  352.     }
  353.     public function setEmail(string $email)
  354.     {
  355.         $this->email $email;
  356.         return $this;
  357.     }
  358.     public function setPhone(string $phone)
  359.     {
  360.         $this->phone = (!empty($phone) ? $phone $this->phone);
  361.         return $this;
  362.     }
  363.     public function setTag(int $tag)
  364.     {
  365.         $this->tag = (!empty($tag) ? $tag $this->tag);
  366.         return $this;
  367.     }
  368.     public function setOpportunity(int $opportunity)
  369.     {
  370.         $this->opportunity = (!empty($opportunity) ? $opportunity $this->opportunity);
  371.         return $this;
  372.     }
  373.     public function setTextComplement(string $textComplement)
  374.     {
  375.         $this->textComplement = (
  376.             !empty($textComplement) ? $textComplement $this->textComplement
  377.         );
  378.         return $this;
  379.     }
  380.     public function getTagMarketingDomain()
  381.     {
  382.         $tagMarketingDomain '';
  383.         if(
  384.             $this->configuration->get('tags_marketing_domain') == TagsMarketingEnum::YES
  385.             && !empty($this->client->getDomainPrimary())
  386.         ){
  387.             $tagMarketingDomain "{$this->client->getDomainPrimary()} - ";
  388.         }
  389.         return $tagMarketingDomain;
  390.     }
  391.     public function getData()
  392.     {
  393.         $tagInfo $this->getTagInfo();
  394.         $data = [
  395.             "userName" => $this->email,
  396.             "email" => $this->email,
  397.             "phone" => $this->phone,
  398.             "userId" => null,
  399.             "birthday" => null,
  400.             "bio" => null,
  401.             "city" => null,
  402.             "state" => null,
  403.             "country" => null,
  404.             "website" => null,
  405.             "twitter" => null,
  406.             "linkedin" => null,
  407.             "facebook" => null,
  408.             "tag" => $this->getTagMarketingDomain() . $tagInfo->textTag $this->textComplement,
  409.             "actionLahar" => $tagInfo->actionLahar,
  410.             "actionActiveCampaign" => 'contact_tag_add',
  411.             "pipedrivePersonId" => null,
  412.             "productOfferId" => null,
  413.             "opportunity" => null,
  414.             "traffic_medium" => (!empty($this->client->getDomainPrimary()) ? $this->client->getDomainPrimary() : null)
  415.         ];
  416.         if(
  417.             $this->tag == TagsMarketingEnum::TAG_REGISTERED_EAD || 
  418.             $this->tag == TagsMarketingEnum::TAG_CONFIRM_REGISTER
  419.         ){
  420.             $data["actionActiveCampaign"] = 'contact_add';
  421.         }
  422.         if($this->user)
  423.         {
  424.             $user $this->user;
  425.             $data["userId"] = $this->user->getId();
  426.             $data["userName"] = $this->user->getName();
  427.             $data["email"] = $this->user->getEmail();
  428.             $data["phone"] = $this->user->getPhone();
  429.             $data['birthday'] = $this->user->getBirthDate();
  430.             $data['bio'] = $this->user->getBiography(); 
  431.             $data['city'] = (!empty($user->getCity()) ? $user->getCity()->getName() : null);
  432.             $data['state'] = (!empty($user->getState()) ? $user->getState()->getUf() : null);
  433.             $data['country'] = (!empty($user->getCountry()) ? $user->getCountry()->getName() : null);
  434.             $data['website'] = $this->user->getWebsite(); 
  435.             $data['twitter'] = $this->user->getTwitter(); 
  436.             $data['linkedin'] = $this->user->getLinkedin(); 
  437.             $data['facebook'] = $this->user->getFacebook(); 
  438.             $data["pipedrivePersonId"] = $this->user->getPipedrivePerson();
  439.         }
  440.         if(!empty($this->opportunity)){
  441.             if($this->productOffer){
  442.                 $data["productOfferId"] = $this->productOffer->getId();
  443.             }
  444.             $data["opportunity"] = $this->opportunity;
  445.         }
  446.         if($this->debug){
  447.             $discordService $this->generalService->getService('DiscordService');
  448.             $discordService->setChannel('debug');
  449.             $discordService->setMessage(json_encode($data));
  450.             $discordService->sendDiscord();
  451.         }
  452.       
  453.         return $data;
  454.     }
  455.     public function send()
  456.     {
  457.         if($this->debug){
  458.             $discordService $this->generalService->getService(
  459.                 'DiscordService'
  460.             );
  461.             $discordService->setChannel('debug');
  462.             $discordService->setMessage("Start Marketing");
  463.             $discordService->sendDiscord();
  464.         }
  465.         $data $this->getData();
  466.         $tagInfo $this->getTagInfo();
  467.        $this->veriFyTagRdStation($data$tagInfo->flag);
  468.        $this->verifyTagLahar($data$tagInfo->flag);
  469.        $this->verifyTagActiveCampaign($data$tagInfo->flag);
  470.        $this->verifyTagMailChimp($data);
  471.        $this->verifySlack($data$tagInfo->flag);
  472.        $this->verifyDiscord($data$tagInfo->flag);
  473.        $this->verifyOpportunity($data);
  474.        $this->sendNotesPipedrive($data);
  475.     }
  476.     public function veriFyTagRdStation($data$flag){
  477.         if($this->configuration->get("app_rd_{$flag}") == TagsMarketingEnum::YES){
  478.             $this->rdStationService->setDebug($this->debug);
  479.             $this->rdStationService->prepareConversion($data);
  480.         }
  481.     }
  482.     public function verifyTagLahar($data$flag){
  483.         if($this->configuration->get("app_lahar_{$flag}") == TagsMarketingEnum::YES){
  484.             $this->laharService->prepareConversion($data);
  485.         }
  486.     }
  487.     public function verifyTagActiveCampaign($data$flag){
  488.         if($this->configuration->get("app_activecampaign_{$flag}") == TagsMarketingEnum::YES){
  489.             $this->activeCampaignService->prepareConversion($data);
  490.         }
  491.     }
  492.     public function verifyTagMailChimp($data){
  493.         $this->mailChimpService->prepareConversion($data);
  494.     }
  495.     public function verifySlack($data$flag){
  496.         if($this->configuration->get("app_slack_{$flag}") == TagsMarketingEnum::YES){
  497.             if($this->tag == TagsMarketingEnum::TAG_SIGNUP_NEWSLETTER)
  498.             {
  499.                 $data["email"] = $this->email;
  500.                 $data["userName"] = $this->email;
  501.                 $data["phone"] = '---';
  502.                 $data["userName"] = '---';
  503.             }
  504.             $this->slackService->sendSlack($data);
  505.         }
  506.     }
  507.     public function verifyDiscord($data$flag){
  508.         if($this->configuration->get("app_discord_{$flag}") == TagsMarketingEnum::YES){
  509.          
  510.             if($this->tag == TagsMarketingEnum::TAG_SIGNUP_NEWSLETTER)
  511.             {
  512.                 $data["email"] = $this->email;
  513.                 $data["userName"] = $this->email;
  514.                 $data["phone"] = '---';
  515.                 $data["userId"] = '---';
  516.             }
  517.           
  518.             $this->discordService->setChannel('clientServer');
  519.             $this->discordService->prepareMessage($data);
  520.         }
  521.     }
  522.     public function verifyOpportunity($data){
  523.         $this->eadStationService->sendOpportunity($data);
  524.     }
  525.     public function sendNotesPipedrive($data)
  526.     {  
  527.         $crmService $this->generalService->getService('CRM\\CrmService');
  528.         
  529.         if($data["pipedrivePersonId"] != null)
  530.         {
  531.             $pipedriveId $data["pipedrivePersonId"];
  532.             $tag $data["tag"];
  533.             $crmService->saveNote($pipedriveId$tag);
  534.         }
  535.     }
  536. }