CraftCamp /
official-website
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Manager\Community; |
||
| 4 | |||
| 5 | use Doctrine\ORM\EntityManagerInterface; |
||
| 6 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
| 7 | |||
| 8 | use App\Entity\Community\Community; |
||
| 9 | use App\Entity\Picture; |
||
| 10 | use App\Entity\User\User; |
||
| 11 | |||
| 12 | use App\Utils\Slugger; |
||
| 13 | |||
| 14 | use App\Event\Community\CommunityCreationEvent; |
||
| 15 | |||
| 16 | class CommunityManager |
||
| 17 | { |
||
| 18 | /** @var EntityManagerInterface **/ |
||
| 19 | protected $em; |
||
| 20 | /** @var EventDispatcherInterface **/ |
||
| 21 | protected $eventDispatcher; |
||
| 22 | /** @var MemberManager **/ |
||
| 23 | protected $memberManager; |
||
| 24 | /** @var Slugger **/ |
||
| 25 | protected $slugger; |
||
| 26 | |||
| 27 | 4 | public function __construct(EntityManagerInterface $entityManager, EventDispatcherInterface $eventDispatcher, MemberManager $memberManager, Slugger $slugger) |
|
| 28 | { |
||
| 29 | 4 | $this->em = $entityManager; |
|
| 30 | 4 | $this->eventDispatcher = $eventDispatcher; |
|
| 31 | 4 | $this->memberManager = $memberManager; |
|
| 32 | 4 | $this->slugger = $slugger; |
|
| 33 | 4 | } |
|
| 34 | |||
| 35 | 1 | public function createCommunity(User $founder, string $name, $picture = null): Community |
|
| 36 | { |
||
| 37 | $community = |
||
| 38 | 1 | (new Community()) |
|
| 39 | 1 | ->setName($name) |
|
| 40 | 1 | ->setSlug($this->slugger->slugify($name)) |
|
| 41 | ; |
||
| 42 | 1 | if ($picture !== null) { |
|
| 43 | 1 | $community->setPicture( |
|
| 44 | 1 | (new Picture()) |
|
| 45 | 1 | ->setName("community-{$community->getSlug()}") |
|
| 46 | 1 | ->setFile($picture) |
|
| 47 | ); |
||
| 48 | } |
||
| 49 | 1 | $this->em->persist($community); |
|
| 50 | 1 | $this->em->flush(); |
|
| 51 | 1 | $this->memberManager->createMembership($community, $founder, true, false); |
|
| 52 | 1 | $this->eventDispatcher->dispatch(CommunityCreationEvent::NAME, new CommunityCreationEvent($community, $founder)); |
|
| 53 | 1 | return $community; |
|
| 54 | } |
||
| 55 | |||
| 56 | 1 | public function countAll(): int |
|
| 57 | { |
||
| 58 | 1 | return $this->em->getRepository(Community::class)->countAll(); |
|
| 59 | } |
||
| 60 | |||
| 61 | 2 | public function getAll(): array |
|
| 62 | { |
||
| 63 | 2 | return $this->em->getRepository(Community::class)->findAll(); |
|
| 64 | } |
||
| 65 | |||
| 66 | public function get(string $slug): Community |
||
| 67 | { |
||
| 68 | return $this->em->getRepository(Community::class)->findOneBySlug($slug); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 69 | } |
||
| 70 | } |