<?php
# src/EventSubscriber/EasyAdminSubscriber.php
namespace App\EventSubscriber;
use App\Entity\Project;
use App\Service\SlackService;
use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
use Gitlab\Client;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\String\Slugger\SluggerInterface;
class AfterProjectCreated implements EventSubscriberInterface
{
private $slackService;
private $slugger;
private $gitlab;
public function __construct(SlackService $slackService, SluggerInterface $slugger, Client $gitlab)
{
$this->slackService = $slackService;
$this->slugger = $slugger;
$this->gitlab = $gitlab;
}
public static function getSubscribedEvents()
{
return [
BeforeEntityPersistedEvent::class => ['afterProjectCreated'],
];
}
public function afterProjectCreated(BeforeEntityPersistedEvent $event)
{
$entity = $event->getEntityInstance();
if (!($entity instanceof Project)) {
return;
}
$slug = strtolower($this->slugger->slug($entity->getName()));
$channel = $this->slackService->createChannel($slug. '-suivi');
if(isset($channel['channel'])){
$entity->setSlackId($channel['channel']['id']);
$res = $this->slackService->invitePeople($channel['channel']['id'], "U02RQB2JQK1,U02S50X8CEP");
}
$channelInternal = $this->slackService->createChannel($slug);
if(isset($channelInternal['channel'])){
$entity->setSlackInternalId($channelInternal['channel']['id']);
$res = $this->slackService->invitePeople($channelInternal['channel']['id'], "U02RQB2JQK1,U02S50X8CEP");
}
if($entity->isMonolitic()){
$res = $this->gitlab->projects()->create($entity->getName(), [
'description' => $entity->getName(),
'issues_enabled' => false,
'namespace_id' => 3833244
]);
$entity->setGit($res['web_url']);
$entity->setGitBack($res['web_url']);
$entity->setGitlabId($res['id']);
}
else {
$res = $this->gitlab->projects()->create($entity->getName(). '-front', [
'description' => $entity->getName(). '-front',
'issues_enabled' => false,
'namespace_id' => 3833244
]);
$entity->setGit($res['web_url']);
$entity->setGitlabId($res['id']);
$res2 = $this->gitlab->projects()->create($entity->getName(). '-back', [
'description' => $entity->getName(). ' back',
'issues_enabled' => false,
'namespace_id' => 3833244
]);
$entity->setGitlabBackId($res2['id']);
$entity->setGitBack($res2['web_url']);
}
}
}