src/Products/NotificationsBundle/Subscriber/OneRoster/OneRosterUserTweakSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. namespace Products\NotificationsBundle\Subscriber\OneRoster;
  3. use Cms\CoreBundle\Entity\OneRoster\OneRosterUser;
  4. use Cms\CoreBundle\Entity\OneRosterSync;
  5. use Cms\CoreBundle\Events\OneRosterTweakEvent;
  6. use Doctrine\Common\Util\ClassUtils;
  7. use Products\NotificationsBundle\Entity\Profile;
  8. use Products\NotificationsBundle\Entity\Student;
  9. use Products\NotificationsBundle\Util\Reachability;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. /**
  12.  * Class OneRosterUserTweakSubscriber
  13.  * @package Products\NotificationsBundle\Subscriber\OneRoster
  14.  */
  15. final class OneRosterUserTweakSubscriber extends AbstractNotificationsOneRosterSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * {@inheritdoc}
  19.      */
  20.     static public function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             OneRosterTweakEvent::EVENT__USER => [
  24.                 //['discardableEnable', 0],
  25.                 ['calculateStudentReachability'0],
  26.                 //['discardableDisable', 0],
  27.             ],
  28.         ];
  29.     }
  30.     /**
  31.      * @param OneRosterTweakEvent $event
  32.      */
  33.     public function calculateStudentReachability(OneRosterTweakEvent $event): void
  34.     {
  35.         // ensure we are meant to process this
  36.         if ( ! $this->checkTypes($event->getJob(), [
  37.             OneRosterSync::STRATEGIES__NOTIFICATIONS__FAMILY,
  38.             OneRosterSync::STRATEGIES__NOTIFICATIONS__STUDENTS,
  39.         ])) {
  40.             return;
  41.         }
  42.         // get the user
  43.         $user $event->getEntity();
  44.         if ( ! $user instanceof OneRosterUser) {
  45.             throw new \Exception(sprintf(
  46.                 'User is not of proper type, got "%s".',
  47.                 ClassUtils::getClass($user)
  48.             ));
  49.         }
  50.         // branch on the role type
  51.         switch (true) {
  52.             case $this->checkTypes($event->getJob(), [OneRosterSync::STRATEGIES__NOTIFICATIONS__FAMILY]) && $user->isRoleStudent():
  53.             case $this->checkTypes($event->getJob(), [OneRosterSync::STRATEGIES__NOTIFICATIONS__STUDENTS]) && $user->isRoleStudent():
  54.                 // noop, code will continue after the switch statement
  55.                 break;
  56.             default:
  57.                 // DEBUGGING
  58.                 $event->getOutput()->writeln(sprintf(
  59.                     'User role for #%s is "%s", skipping...',
  60.                     $user->getSourcedId(),
  61.                     $user->getRole()
  62.                 ));
  63.                 // quit early
  64.                 return;
  65.         }
  66.         // load up the student, this must match something, and it should at this point...
  67.         $student $this->em->getRepository(Student::class)->findOneBy([
  68.             'onerosterId' => $user->getSourcedId(),
  69.         ]);
  70.         // if we don't have a profile we have a problem
  71.         if ( ! $student) {
  72.             throw new \Exception(sprintf(
  73.                 'Could not load student "%s".',
  74.                 $user->getSourcedId()
  75.             ));
  76.         }
  77.         // get all the profiles for this student, should be attached now
  78.         /** @var array|Profile[] $profiles */
  79.         $profiles $this->em->getRepository(Profile::class)->createQueryBuilder('profiles')
  80.             ->leftJoin('profiles.relationships''relationships')
  81.             ->andWhere('relationships.student = :student')
  82.             ->setParameter('student'$student)
  83.             ->getQuery()
  84.             ->getResult();
  85.         // reset the students standing
  86.         $student->setStanding(Reachability::STANDINGS__NONE);
  87.         // loop over all the profiles
  88.         foreach ($profiles as $profile) {
  89.             // merge the standing into the student
  90.             $student->mergeStanding($profile->getStanding());
  91.         }
  92.         // save the student
  93.         $this->em->save(
  94.             $student
  95.         );
  96.     }
  97. }