Skip to content

Commit 4a87e1d

Browse files
committed
Moved doctrine knowledge into a manager class
The controller didn't need to know about the implementation details of doctrine, it only needs to know that our application can provide some blog posts objects. Therefore we introduce a "Manager" class whose job it is to do that.
1 parent 55c0bf8 commit 4a87e1d

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

spec/Peterjmit/BlogBundle/Controller/BlogControllerSpec.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,18 @@
55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
77

8-
use Doctrine\Common\Persistence\ManagerRegistry;
9-
use Doctrine\Common\Persistence\ObjectManager;
10-
use Doctrine\Common\Persistence\ObjectRepository;
8+
use Peterjmit\BlogBundle\Model\BlogManagerInterface;
119

1210
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
1311
use Symfony\Component\HttpFoundation\Response;
1412

1513
class BlogControllerSpec extends ObjectBehavior
1614
{
1715
function let(
18-
ManagerRegistry $registry,
19-
ObjectManager $manager,
20-
ObjectRepository $repository,
16+
BlogManagerInterface $manager,
2117
EngineInterface $templating
2218
) {
23-
$registry->getManager()->willReturn($manager);
24-
$manager->getRepository('PeterjmitBlogBundle:Blog')->willReturn($repository);
25-
26-
$this->beConstructedWith($registry, $templating);
19+
$this->beConstructedWith($manager, $templating);
2720
}
2821

2922
function it_is_initializable()
@@ -32,11 +25,11 @@ function it_is_initializable()
3225
}
3326

3427
function it_should_respond_to_index_action(
35-
ObjectRepository $repository,
28+
BlogManagerInterface $manager,
3629
EngineInterface $templating,
3730
Response $mockResponse
3831
) {
39-
$repository->findAll()->willReturn(array());
32+
$manager->findAll()->willReturn(array());
4033

4134
$templating
4235
->renderResponse(

src/Peterjmit/BlogBundle/Controller/BlogController.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@
22

33
namespace Peterjmit\BlogBundle\Controller;
44

5-
use Doctrine\Common\Persistence\ManagerRegistry;
5+
use Peterjmit\BlogBundle\Model\BlogManagerInterface;
66
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
77

88
class BlogController
99
{
10-
private $doctrine;
10+
private $manager;
1111
private $templating;
1212

13-
public function __construct(ManagerRegistry $doctrine, EngineInterface $templating)
13+
public function __construct(BlogManagerInterface $manager, EngineInterface $templating)
1414
{
15-
$this->doctrine = $doctrine;
15+
$this->manager = $manager;
1616
$this->templating = $templating;
1717
}
1818

1919
public function indexAction()
2020
{
21-
$entityManager = $this->doctrine->getManager();
22-
$posts = $entityManager->getRepository('PeterjmitBlogBundle:Blog')->findAll();
21+
$posts = $this->manager->findAll();
2322

2423
return $this->templating->renderResponse('PeterjmitBlogBundle:Blog:index.html.twig', array(
2524
'posts' => $posts
@@ -28,8 +27,7 @@ public function indexAction()
2827

2928
public function showAction($id)
3029
{
31-
$entityManager = $this->doctrine->getManager();
32-
$post = $entityManager->getRepository('PeterjmitBlogBundle:Blog')->find($id);
30+
$post = $this->manager->find($id);
3331

3432
if (!$post) {
3533
throw $this->createNotFoundException(sprintf('Blog post %s was not found', $id));
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Peterjmit\BlogBundle\Model;
4+
5+
interface BlogManagerInterface
6+
{
7+
function findAll();
8+
function find($id);
9+
}

0 commit comments

Comments
 (0)