1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Developtech\AgilityBundle\Manager; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
6
|
|
|
|
7
|
|
|
use Developtech\AgilityBundle\Entity\Project; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
10
|
|
|
|
11
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
12
|
|
|
|
13
|
|
|
use Developtech\AgilityBundle\Utils\Slugger; |
14
|
|
|
|
15
|
|
|
class ProjectManager { |
16
|
|
|
/** @var Doctrine\ORM\EntityManager **/ |
17
|
|
|
protected $em; |
18
|
|
|
/** @var Developtech\AgilityBundle\Utils\Slugger **/ |
19
|
|
|
protected $slugger; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Doctrine\ORM\EntityManager $em |
23
|
|
|
* @param Slugger $slugger |
24
|
|
|
*/ |
25
|
5 |
|
public function __construct(EntityManager $em, Slugger $slugger) { |
|
|
|
|
26
|
5 |
|
$this->em = $em; |
|
|
|
|
27
|
5 |
|
$this->slugger = $slugger; |
|
|
|
|
28
|
5 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
1 |
|
public function getProjects() { |
34
|
1 |
|
return $this->em->getRepository(Project::class)->findAll(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $slug |
39
|
|
|
* @throws NotFoundHttpException |
40
|
|
|
* @return ProjectModel |
41
|
|
|
*/ |
42
|
1 |
|
public function getProject($slug) { |
43
|
1 |
|
$project = $this->em->getRepository(Project::class)->findOneBySlug($slug); |
44
|
1 |
|
if($project === null) { |
45
|
|
|
throw new NotFoundHttpException('Project not found'); |
46
|
|
|
} |
47
|
1 |
|
return $project; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $name |
52
|
|
|
* @param UserInterface $productOwner |
53
|
|
|
* @return \DevelopTech\AgilityBundle\ProjectModel |
54
|
|
|
*/ |
55
|
1 |
|
public function createProject($name, UserInterface $productOwner) { |
56
|
|
|
$project = |
57
|
1 |
|
(new Project()) |
58
|
1 |
|
->setName($name) |
59
|
1 |
|
->setSlug($this->slugger->slugify($name)) |
60
|
1 |
|
->setProductOwner($productOwner) |
61
|
1 |
|
->setBetaTestStatus('closed') |
62
|
1 |
|
->setNbBetaTesters(0) |
63
|
1 |
|
; |
64
|
1 |
|
$this->em->persist($project); |
65
|
1 |
|
$this->em->flush(); |
66
|
1 |
|
return $project; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param integer $id |
71
|
|
|
* @param string $name |
72
|
|
|
* @param integer $betaTestStatus |
73
|
|
|
* @param integer $nbBetaTesters |
74
|
|
|
* @param UserInterface $productOwner |
75
|
|
|
* @throws NotFoundHttpException |
76
|
|
|
* @return ProjectModel |
77
|
|
|
*/ |
78
|
2 |
|
public function editProject($id, $name, $betaTestStatus, $nbBetaTesters, UserInterface $productOwner = null) { |
79
|
2 |
|
if (($project = $this->em->getRepository(Project::class)->find($id)) === null) { |
80
|
1 |
|
throw new NotFoundHttpException('Project not found'); |
81
|
|
|
} |
82
|
|
|
$project |
83
|
1 |
|
->setName($name) |
84
|
1 |
|
->setSlug($this->slugger->slugify($name)) |
85
|
1 |
|
->setBetaTestStatus($betaTestStatus) |
86
|
1 |
|
->setNbBetaTesters($nbBetaTesters) |
87
|
|
|
; |
88
|
1 |
|
if($productOwner !== null) { |
89
|
|
|
$project->setProductOwner($productOwner); |
90
|
|
|
} |
91
|
1 |
|
$this->em->flush(); |
92
|
1 |
|
return $project; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.