src/EventSubscriber/AfterProjectCreated.php line 34

Open in your IDE?
  1. <?php
  2. # src/EventSubscriber/EasyAdminSubscriber.php
  3. namespace App\EventSubscriber;
  4. use App\Entity\Project;
  5. use App\Service\SlackService;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  8. use Gitlab\Client;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\String\Slugger\SluggerInterface;
  11. class AfterProjectCreated implements EventSubscriberInterface
  12. {
  13.     private $slackService;
  14.     private $slugger;
  15.     private $gitlab;
  16.     public function __construct(SlackService  $slackServiceSluggerInterface $sluggerClient $gitlab)
  17.     {
  18.         $this->slackService $slackService;
  19.         $this->slugger $slugger;
  20.         $this->gitlab $gitlab;
  21.     }
  22.     public static function getSubscribedEvents()
  23.     {
  24.         return [
  25.             BeforeEntityPersistedEvent::class => ['afterProjectCreated'],
  26.         ];
  27.     }
  28.     public function afterProjectCreated(BeforeEntityPersistedEvent $event)
  29.     {
  30.         $entity $event->getEntityInstance();
  31.         if (!($entity instanceof Project)) {
  32.             return;
  33.         }
  34.         $slug strtolower($this->slugger->slug($entity->getName()));
  35.         $channel $this->slackService->createChannel($slug'-suivi');
  36.         if(isset($channel['channel'])){
  37.             $entity->setSlackId($channel['channel']['id']);
  38.             $res =  $this->slackService->invitePeople($channel['channel']['id'], "U02RQB2JQK1,U02S50X8CEP");
  39.         }
  40.         $channelInternal $this->slackService->createChannel($slug);
  41.         if(isset($channelInternal['channel'])){
  42.             $entity->setSlackInternalId($channelInternal['channel']['id']);
  43.             $res =  $this->slackService->invitePeople($channelInternal['channel']['id'], "U02RQB2JQK1,U02S50X8CEP");
  44.         }
  45.         if($entity->isMonolitic()){
  46.             $res $this->gitlab->projects()->create($entity->getName(), [
  47.                 'description' => $entity->getName(),
  48.                 'issues_enabled' => false,
  49.                 'namespace_id' => 3833244
  50.             ]);
  51.             $entity->setGit($res['web_url']);
  52.             $entity->setGitBack($res['web_url']);
  53.             $entity->setGitlabId($res['id']);
  54.         }
  55.         else {
  56.             $res $this->gitlab->projects()->create($entity->getName(). '-front', [
  57.                 'description' => $entity->getName(). '-front',
  58.                 'issues_enabled' => false,
  59.                 'namespace_id' => 3833244
  60.             ]);
  61.             $entity->setGit($res['web_url']);
  62.             $entity->setGitlabId($res['id']);
  63.             $res2 $this->gitlab->projects()->create($entity->getName(). '-back', [
  64.                 'description' => $entity->getName(). ' back',
  65.                 'issues_enabled' => false,
  66.                 'namespace_id' => 3833244
  67.             ]);
  68.             $entity->setGitlabBackId($res2['id']);
  69.             $entity->setGitBack($res2['web_url']);
  70.         }
  71.     }
  72. }