|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Manager\Project; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
9
|
|
|
|
|
10
|
|
|
use App\Entity\Project\Project; |
|
11
|
|
|
use App\Entity\Project\Member; |
|
12
|
|
|
use App\Entity\User\User; |
|
13
|
|
|
use App\Entity\Organization; |
|
14
|
|
|
|
|
15
|
|
|
use App\Event\Project\NewMemberEvent; |
|
16
|
|
|
use App\Event\Project\NewProjectEvent; |
|
17
|
|
|
|
|
18
|
|
|
use App\Utils\Slugger; |
|
19
|
|
|
|
|
20
|
|
|
class ProjectManager |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var EntityManagerInterface **/ |
|
23
|
|
|
protected $em; |
|
24
|
|
|
/** @var EventDispatcherInterface **/ |
|
25
|
|
|
protected $eventDispatcher; |
|
26
|
|
|
/** @var Slugger **/ |
|
27
|
|
|
protected $slugger; |
|
28
|
|
|
|
|
29
|
10 |
|
public function __construct(EntityManagerInterface $em, EventDispatcherInterface $eventDispatcher, Slugger $slugger) |
|
30
|
|
|
{ |
|
31
|
10 |
|
$this->em = $em; |
|
32
|
10 |
|
$this->eventDispatcher = $eventDispatcher; |
|
33
|
10 |
|
$this->slugger = $slugger; |
|
34
|
10 |
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
public function getAll(): array |
|
37
|
|
|
{ |
|
38
|
1 |
|
return $this->em->getRepository(Project::class)->findAll(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
3 |
|
public function get(string $slug) |
|
42
|
|
|
{ |
|
43
|
3 |
|
return $this->em->getRepository(Project::class)->findOneBySlug($slug); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
1 |
|
public function countAll(): int |
|
47
|
|
|
{ |
|
48
|
1 |
|
return $this->em->getRepository(Project::class)->countAll(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
public function createProject(string $name, string $description, User $productOwner, Organization $organization = null): Project |
|
52
|
|
|
{ |
|
53
|
1 |
|
if ($this->em->getRepository(Project::class)->findOneByName($name) !== null) { |
|
|
|
|
|
|
54
|
|
|
throw new BadRequestHttpException('projects.existing_name'); |
|
55
|
|
|
} |
|
56
|
|
|
$project = |
|
57
|
1 |
|
(new Project()) |
|
58
|
1 |
|
->setName($name) |
|
59
|
1 |
|
->setSlug($this->slugger->slugify($name)) |
|
60
|
1 |
|
->setDescription($description) |
|
61
|
1 |
|
->setProductOwner($productOwner) |
|
62
|
|
|
; |
|
63
|
1 |
|
if ($organization !== null) { |
|
64
|
1 |
|
$project->setOrganization($organization); |
|
65
|
|
|
} |
|
66
|
1 |
|
$this->em->persist($project); |
|
67
|
1 |
|
$this->em->flush($project); |
|
68
|
|
|
|
|
69
|
1 |
|
$this->eventDispatcher->dispatch(NewProjectEvent::NAME, new NewProjectEvent($project)); |
|
70
|
|
|
|
|
71
|
1 |
|
return $project; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
public function joinProject(Project $project, User $user, bool $isNews = true): Member |
|
75
|
|
|
{ |
|
76
|
3 |
|
if ($this->getProjectMember($project, $user) !== null) { |
|
77
|
1 |
|
throw new BadRequestHttpException('projects.already_joined'); |
|
78
|
|
|
} |
|
79
|
|
|
$membership = |
|
80
|
2 |
|
(new Member()) |
|
81
|
2 |
|
->setUser($user) |
|
82
|
2 |
|
->setProject($project) |
|
83
|
2 |
|
->setIsActive(true) |
|
84
|
|
|
; |
|
85
|
2 |
|
$this->em->persist($membership); |
|
86
|
2 |
|
$this->em->flush($membership); |
|
87
|
2 |
|
if ($isNews === true) { |
|
88
|
1 |
|
$this->eventDispatcher->dispatch(NewMemberEvent::NAME, new NewMemberEvent($project, $user)); |
|
89
|
|
|
} |
|
90
|
2 |
|
return $membership; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
1 |
|
public function getProjectMembers(Project $project): array |
|
94
|
|
|
{ |
|
95
|
1 |
|
return $this->em->getRepository(Member::class)->findByProject($project); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
4 |
|
public function getProjectMember(Project $project, User $user): ?Member |
|
99
|
|
|
{ |
|
100
|
4 |
|
return $this->em->getRepository(Member::class)->findOneBy([ |
|
101
|
4 |
|
'project' => $project, |
|
102
|
4 |
|
'user' => $user |
|
103
|
|
|
]); |
|
104
|
|
|
} |
|
105
|
|
|
} |