src/AppBundle/Handler/GenerateNotificationHandler.php line 44

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Handler;
  3. use AppBundle\Common\ArrayToolkit;
  4. use Biz\Classroom\Service\ClassroomService;
  5. use Biz\CloudPlatform\QueueJob\PushJob;
  6. use Biz\CloudPlatform\Service\AppService;
  7. use Biz\Course\Service\MemberService;
  8. use Biz\System\Service\SettingService;
  9. use Biz\User\CurrentUser;
  10. use Biz\User\Service\NotificationService;
  11. use Codeages\Biz\Framework\Context\Biz;
  12. use Codeages\Biz\Framework\Queue\Service\QueueService;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  15. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  16. use VipPlugin\Biz\Vip\Service\LevelService;
  17. use VipPlugin\Biz\Vip\Service\VipService;
  18. class GenerateNotificationHandler
  19. {
  20.     /**
  21.      * @var ContainerInterface
  22.      */
  23.     private $container;
  24.     /**
  25.      * @var Biz
  26.      */
  27.     private $biz;
  28.     public function __construct(ContainerInterface $container)
  29.     {
  30.         $this->container $container;
  31.         $this->biz $this->container->get('biz');
  32.     }
  33.     /**
  34.      * Do the magic.
  35.      *
  36.      * @param InteractiveLoginEvent $event
  37.      */
  38.     public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
  39.     {
  40.         $user $this->biz['user'];
  41.         $this->sendCoursesOverdueNotification($user);
  42.         $this->sendClassroomsOverdueNotification($user);
  43.         $this->sendVipsOverdueNotification($user);
  44.     }
  45.     protected function sendCoursesOverdueNotification(CurrentUser $user)
  46.     {
  47.         list($courses$courseMembers) = $this->getCourseMemberService()->findWillOverdueCourses();
  48.         $courseMembers ArrayToolkit::index($courseMembers'courseId');
  49.         foreach ((array) $courses as $key => $course) {
  50.             if (!= $course['parentId']) {
  51.                 continue;
  52.             }
  53.             $message = [
  54.                 'courseId' => $course['id'],
  55.                 'courseTitle' => $course['title'],
  56.                 'endtime' => date('Y-m-d'$courseMembers[$course['id']]['deadline']),
  57.             ];
  58.             $this->courseOverduePush($user$message);
  59.             $this->getNotificationService()->notify($user['id'], 'course-deadline'$message);
  60.             $courseMemberId $courseMembers[$course['id']]['id'];
  61.             $this->getCourseMemberService()->updateMember($courseMemberId, ['deadlineNotified' => 1]);
  62.         }
  63.     }
  64.     private function courseOverduePush($user$message)
  65.     {
  66.         $from = [
  67.             'id' => $message['courseId'],
  68.             'type' => 'course',
  69.         ];
  70.         $to = [
  71.             'id' => $user['id'],
  72.             'type' => 'user',
  73.             'convNo' => $this->getConvNo(),
  74.         ];
  75.         $body = [
  76.             'type' => 'course.deadline',
  77.             'courseId' => $message['courseId'],
  78.             'title' => "《{$message['courseTitle']}》",
  79.             'message' => "您加入的课程《{$message['courseTitle']}》将在{$message['endtime']}到期",
  80.         ];
  81.         $this->createPushJob($from$to$body);
  82.     }
  83.     protected function sendClassroomsOverdueNotification($user)
  84.     {
  85.         list($classrooms$classroomMembers) = $this->getClassroomService()->findWillOverdueClassrooms();
  86.         $classroomMembers ArrayToolkit::index($classroomMembers'classroomId');
  87.         foreach ($classrooms as $key => $classroom) {
  88.             $message = [
  89.                 'classroomId' => $classroom['id'],
  90.                 'classroomTitle' => $classroom['title'],
  91.                 'endtime' => date('Y-m-d'$classroomMembers[$classroom['id']]['deadline']),
  92.             ];
  93.             $this->classroomOverduePush($user$message);
  94.             $this->getNotificationService()->notify($user['id'], 'classroom-deadline'$message);
  95.             $classroomMemberId $classroomMembers[$classroom['id']]['id'];
  96.             $this->getClassroomService()->updateMember($classroomMemberId, ['deadlineNotified' => 1]);
  97.         }
  98.     }
  99.     private function classroomOverduePush($user$message)
  100.     {
  101.         $from = [
  102.             'id' => $message['classroomId'],
  103.             'type' => 'classroom',
  104.         ];
  105.         $to = [
  106.             'id' => $user['id'],
  107.             'type' => 'user',
  108.             'convNo' => $this->getConvNo(),
  109.         ];
  110.         $body = [
  111.             'type' => 'classroom.deadline',
  112.             'classroomId' => $message['classroomId'],
  113.             'title' => "《{$message['classroomTitle']}》",
  114.             'message' => "您加入的班级《{$message['classroomTitle']}》将在{$message['endtime']}到期",
  115.         ];
  116.         $this->createPushJob($from$to$body);
  117.     }
  118.     protected function sendVipsOverdueNotification($user)
  119.     {
  120.         if (!$this->isPluginInstalled('vip')) {
  121.             return false;
  122.         }
  123.         $vipSetting $this->getSettingService()->get('vip', []);
  124.         if (array_key_exists('deadlineNotify'$vipSetting) && == $vipSetting['deadlineNotify']) {
  125.             $vip $this->getVipService()->getMemberByUserId($user['id']);
  126.             $currentTime time();
  127.             if (!= $vip['deadlineNotified'] && $currentTime $vip['deadline']
  128.                 && ($currentTime $vipSetting['daysOfNotifyBeforeDeadline'] * 24 60 60) > $vip['deadline']
  129.             ) {
  130.                 $message = ['endtime' => date('Y-m-d'$vip['deadline']), 'levelId' => $vip['levelId']];
  131.                 $this->vipOverduePush($user$message);
  132.                 $this->getNotificationService()->notify($user['id'], 'vip-deadline'$message);
  133.                 $this->getVipService()->updateDeadlineNotified($vip['id'], 1);
  134.             }
  135.         }
  136.     }
  137.     private function vipOverduePush($user$message)
  138.     {
  139.         $levelId $message['levelId'];
  140.         $level $this->getLevelService()->getLevel($levelId);
  141.         $from = [
  142.             'id' => 0,
  143.             'type' => 'vip',
  144.         ];
  145.         $to = [
  146.             'id' => $user['id'],
  147.             'type' => 'user',
  148.             'convNo' => $this->getConvNo(),
  149.         ];
  150.         $body = [
  151.             'type' => 'vip.deadline',
  152.             'title' => $level['name'],
  153.             'message' => "您购买的会员将在{$message['endtime']}到期",
  154.         ];
  155.         $this->createPushJob($from$to$body);
  156.     }
  157.     public function generateUrl(
  158.         $route,
  159.         array $parameters = [],
  160.         $referenceType UrlGeneratorInterface::ABSOLUTE_PATH
  161.     ) {
  162.         return $this->container->get('router')->generate($route$parameters$referenceType);
  163.     }
  164.     private function createPushJob($from$to$body)
  165.     {
  166.         $pushJob = new PushJob([
  167.             'from' => $from,
  168.             'to' => $to,
  169.             'body' => $body,
  170.         ]);
  171.         $this->getQueueService()->pushJob($pushJob);
  172.     }
  173.     private function getConvNo()
  174.     {
  175.         $imSetting $this->getSettingService()->get('app_im', []);
  176.         $convNo = isset($imSetting['convNo']) && !empty($imSetting['convNo']) ? $imSetting['convNo'] : '';
  177.         return $convNo;
  178.     }
  179.     /**
  180.      * @return QueueService
  181.      */
  182.     protected function getQueueService()
  183.     {
  184.         return $this->biz->service('Queue:QueueService');
  185.     }
  186.     /**
  187.      * @return ClassroomService
  188.      */
  189.     private function getClassroomService()
  190.     {
  191.         return $this->biz->service('Classroom:ClassroomService');
  192.     }
  193.     /**
  194.      * @return NotificationService
  195.      */
  196.     private function getNotificationService()
  197.     {
  198.         return $this->biz->service('User:NotificationService');
  199.     }
  200.     /**
  201.      * @return AppService
  202.      */
  203.     private function getAppService()
  204.     {
  205.         return $this->biz->service('CloudPlatform:AppService');
  206.     }
  207.     /**
  208.      * @return SettingService
  209.      */
  210.     protected function getSettingService()
  211.     {
  212.         return $this->biz->service('System:SettingService');
  213.     }
  214.     /**
  215.      * @return VipService
  216.      */
  217.     protected function getVipService()
  218.     {
  219.         return $this->biz->service('VipPlugin:Vip:VipService');
  220.     }
  221.     /**
  222.      * @return LevelService
  223.      */
  224.     protected function getLevelService()
  225.     {
  226.         return $this->biz->service('VipPlugin:Vip:LevelService');
  227.     }
  228.     /**
  229.      * @return MemberService
  230.      */
  231.     protected function getCourseMemberService()
  232.     {
  233.         return $this->biz->service('Course:MemberService');
  234.     }
  235.     protected function isPluginInstalled($pluginName)
  236.     {
  237.         return $this->container->get('kernel')->getPluginConfigurationManager()->isPluginInstalled($pluginName);
  238.     }
  239. }