1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PRReviewWatcher\Controller; |
4
|
|
|
|
5
|
|
|
use Silex\Application; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use PRReviewWatcher\Entity\Project; |
8
|
|
|
use PRReviewWatcher\Form\Type\ProjectType; |
9
|
|
|
|
10
|
|
|
class ProjectController |
11
|
|
|
{ |
12
|
|
|
public function indexAction(Application $app) |
13
|
|
|
{ |
14
|
|
|
$projects = $app['project_repository']->findAll(); |
15
|
|
|
|
16
|
|
|
return $app['twig']->render('index.html.twig', array('projects' => $projects)); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
View Code Duplication |
public function addProjectAction(Request $request, Application $app) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$credentials = $app['credential_repository']->findAllAsArray(); |
22
|
|
|
$project = new Project(); |
23
|
|
|
$projectForm = $app['form.factory']->create(new ProjectType(), $project, ['credentialChoices' => $credentials]); |
24
|
|
|
$projectForm->handleRequest($request); |
25
|
|
|
|
26
|
|
|
if ($projectForm->isSubmitted() && $projectForm->isValid()) { |
27
|
|
|
$app['project_repository']->save($project); |
28
|
|
|
$app['session']->getFlashBag()->add('success', 'The project was successfully created.'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $app['twig']->render('projectList_form.html.twig', array( |
32
|
|
|
'title' => 'New project', |
33
|
|
|
'legend' => 'New project', |
34
|
|
|
'projectForm' => $projectForm->createView(), |
35
|
|
|
)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
View Code Duplication |
public function editProjectAction($id, Request $request, Application $app) |
|
|
|
|
39
|
|
|
{ |
40
|
|
|
$credentials = $app['credential_repository']->findAllAsArray(); |
41
|
|
|
$project = $app['project_repository']->find($id); |
42
|
|
|
$projectForm = $app['form.factory']->create(new ProjectType(), $project, ['credentialChoices' => $credentials]); |
43
|
|
|
$projectForm->handleRequest($request); |
44
|
|
|
|
45
|
|
|
if ($projectForm->isSubmitted() && $projectForm->isValid()) { |
46
|
|
|
$app['project_repository']->save($project); |
47
|
|
|
$app['session']->getFlashBag()->add('success', 'The project was successfully updated.'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $app['twig']->render('projectList_form.html.twig', array( |
51
|
|
|
'title' => 'Edit project', |
52
|
|
|
'legend' => 'Edit project', |
53
|
|
|
'projectForm' => $projectForm->createView(), |
54
|
|
|
)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function deleteProjectAction($id, Application $app) |
58
|
|
|
{ |
59
|
|
|
$app['project_repository']->delete($id); |
60
|
|
|
$app['session']->getFlashBag()->add('success', 'The project was successfully removed.'); |
61
|
|
|
|
62
|
|
|
return $app->redirect('/admin/project'); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.