src/App/Subscriber/OneRoster/OneRosterOrgProcessSubscriber.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber\OneRoster;
  3. use App\Entity\System\School;
  4. use Cms\CoreBundle\Entity\AbstractOneRosterEntity;
  5. use Cms\CoreBundle\Entity\OneRoster\OneRosterOrg;
  6. use Cms\CoreBundle\Entity\OneRosterSync;
  7. use Cms\CoreBundle\Events\OneRosterProcessEvent;
  8. use Cms\CoreBundle\Model\Interfaces\OneRosterable\AbstractOneRosterableSubscriber;
  9. use Doctrine\Common\Util\ClassUtils;
  10. /**
  11.  */
  12. final class OneRosterOrgProcessSubscriber extends AbstractOneRosterableSubscriber
  13. {
  14.     /**
  15.      * {@inheritdoc}
  16.      */
  17.     static public function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             OneRosterProcessEvent::EVENT__ORG => [
  21.                 ['populateSchools'100],
  22.             ],
  23.         ];
  24.     }
  25.     /**
  26.      * @param OneRosterProcessEvent $event
  27.      */
  28.     public function populateSchools(OneRosterProcessEvent $event): void
  29.     {
  30.         // ensure we are meant to process this
  31.         if ( ! $this->checkTypes($event->getSync(), [
  32.             OneRosterSync::STRATEGIES__SCHOOLS,
  33.         ])) {
  34.             return;
  35.         }
  36.         // get the org
  37.         $org $event->getEntity();
  38.         if ( ! $org instanceof OneRosterOrg) {
  39.             throw new \Exception(sprintf(
  40.                 'Org is not of proper type, got "%s".',
  41.                 ClassUtils::getClass($org)
  42.             ));
  43.         }
  44.         // branch on the org type
  45.         switch (true) {
  46.             case $this->checkTypes($event->getSync(), [OneRosterSync::STRATEGIES__SCHOOLS]) && $org->isType(AbstractOneRosterEntity::ENUMS__ORG_TYPE__DISTRICT):
  47.             case $this->checkTypes($event->getSync(), [OneRosterSync::STRATEGIES__SCHOOLS])
  48.                 && $org->isType(AbstractOneRosterEntity::ENUMS__ORG_TYPE__SCHOOL)
  49.                 && ! $event->getSync()->hasFlag(OneRosterSync::FLAGS__SINGLE_SCHOOL):
  50.                 break;
  51.             default:
  52.                 // DEBUGGING
  53.                 $event->getOutput()->writeln(sprintf(
  54.                     'Org type for #%s is "%s", skipping...',
  55.                     $org->getSourcedId(),
  56.                     $org->getType()
  57.                 ));
  58.                 return;
  59.         }
  60.         // try to find an existing school for the org based on the oneroster id
  61.         $school $this->em->getRepository(School::class)->findOneBy([
  62.             'oneRosterOrg' => $org->getSourcedId(),
  63.         ]);
  64.         // if we don't have one, attempt to find one based on the name
  65.         if ( ! $school) {
  66.             $school $this->em->getRepository(School::class)->findOneBy([
  67.                 'name' => $org->getName(),
  68.             ]);
  69.         }
  70.         // if we still don't have one, make one
  71.         if ( ! $school) {
  72.             $school = (new School())
  73.                 ->setType(
  74.                 $org->isType(AbstractOneRosterEntity::ENUMS__ORG_TYPE__DISTRICT)
  75.                         ? School::TYPES__DISTRICT
  76.                         School::TYPES__OTHER
  77.                 )
  78.             ;
  79.         }
  80.         // set stuff
  81.         $school
  82.             ->setName($school->getName() ?: $org->getName())
  83.             ->setOneRosterOrg($org->getSourcedId())
  84.         ;
  85.         // TODO: schools should now be a onerosterable object...
  86.         // save the school
  87.         $this->em->save($school);
  88.     }
  89. }