Skip to content

Commit e366044

Browse files
committed
feature #756 Add service Tag repository and revamping code (yceruto)
This PR was merged into the master branch. Discussion ---------- Add service Tag repository and revamping code This completes throughout the demo that amazing feature that is "Service Entity Repository". Commits ------- 5209859 Add service Tag repository and revamping code
2 parents f2aecf4 + 5209859 commit e366044

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

src/Form/DataTransformer/TagArrayToStringTransformer.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace App\Form\DataTransformer;
1313

1414
use App\Entity\Tag;
15-
use Doctrine\Common\Persistence\ObjectManager;
15+
use App\Repository\TagRepository;
1616
use Symfony\Component\Form\DataTransformerInterface;
1717

1818
/**
@@ -26,11 +26,11 @@
2626
*/
2727
class TagArrayToStringTransformer implements DataTransformerInterface
2828
{
29-
private $manager;
29+
private $tags;
3030

31-
public function __construct(ObjectManager $manager)
31+
public function __construct(TagRepository $tags)
3232
{
33-
$this->manager = $manager;
33+
$this->tags = $tags;
3434
}
3535

3636
/**
@@ -58,7 +58,7 @@ public function reverseTransform($string): array
5858
$names = array_filter(array_unique(array_map('trim', explode(',', $string))));
5959

6060
// Get the current tags and find the new ones that should be created.
61-
$tags = $this->manager->getRepository(Tag::class)->findBy([
61+
$tags = $this->tags->findBy([
6262
'name' => $names,
6363
]);
6464
$newNames = array_diff($names, $tags);

src/Form/Type/TagsInputType.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

1212
namespace App\Form\Type;
1313

14-
use App\Entity\Tag;
1514
use App\Form\DataTransformer\TagArrayToStringTransformer;
16-
use Doctrine\Common\Persistence\ObjectManager;
15+
use App\Repository\TagRepository;
1716
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
1817
use Symfony\Component\Form\AbstractType;
1918
use Symfony\Component\Form\Extension\Core\Type\TextType;
@@ -31,11 +30,11 @@
3130
*/
3231
class TagsInputType extends AbstractType
3332
{
34-
private $manager;
33+
private $tags;
3534

36-
public function __construct(ObjectManager $manager)
35+
public function __construct(TagRepository $tags)
3736
{
38-
$this->manager = $manager;
37+
$this->tags = $tags;
3938
}
4039

4140
/**
@@ -49,7 +48,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4948
// but here we're doing the transformation in two steps (Collection <-> array <-> string)
5049
// and reuse the existing CollectionToArrayTransformer.
5150
->addModelTransformer(new CollectionToArrayTransformer(), true)
52-
->addModelTransformer(new TagArrayToStringTransformer($this->manager), true)
51+
->addModelTransformer(new TagArrayToStringTransformer($this->tags), true)
5352
;
5453
}
5554

@@ -58,7 +57,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5857
*/
5958
public function buildView(FormView $view, FormInterface $form, array $options)
6059
{
61-
$view->vars['tags'] = $this->manager->getRepository(Tag::class)->findAll();
60+
$view->vars['tags'] = $this->tags->findAll();
6261
}
6362

6463
/**

src/Repository/TagRepository.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace App\Repository;
13+
14+
use App\Entity\Tag;
15+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
16+
use Doctrine\Common\Persistence\ManagerRegistry;
17+
18+
/**
19+
* This custom Doctrine repository is empty because so far we don't need any custom
20+
* method to query for application user information. But it's always a good practice
21+
* to define a custom repository that will be used when the application grows.
22+
*
23+
* See https://symfony.com/doc/current/doctrine/repository.html
24+
*
25+
* @author Yonel Ceruto <[email protected]>
26+
*/
27+
class TagRepository extends ServiceEntityRepository
28+
{
29+
public function __construct(ManagerRegistry $registry)
30+
{
31+
parent::__construct($registry, Tag::class);
32+
}
33+
}

tests/Form/DataTransformer/TagArrayToStringTransformerTest.php

+3-12
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313

1414
use App\Entity\Tag;
1515
use App\Form\DataTransformer\TagArrayToStringTransformer;
16-
use Doctrine\Common\Persistence\ObjectManager;
17-
use Doctrine\ORM\EntityRepository;
16+
use App\Repository\TagRepository;
1817
use PHPUnit\Framework\TestCase;
1918

2019
/**
@@ -108,22 +107,14 @@ public function testTransform()
108107
*/
109108
private function getMockedTransformer(array $findByReturnValues = []): TagArrayToStringTransformer
110109
{
111-
$tagRepository = $this->getMockBuilder(EntityRepository::class)
110+
$tagRepository = $this->getMockBuilder(TagRepository::class)
112111
->disableOriginalConstructor()
113112
->getMock();
114113
$tagRepository->expects($this->any())
115114
->method('findBy')
116115
->will($this->returnValue($findByReturnValues));
117116

118-
$entityManager = $this
119-
->getMockBuilder(ObjectManager::class)
120-
->disableOriginalConstructor()
121-
->getMock();
122-
$entityManager->expects($this->any())
123-
->method('getRepository')
124-
->will($this->returnValue($tagRepository));
125-
126-
return new TagArrayToStringTransformer($entityManager);
117+
return new TagArrayToStringTransformer($tagRepository);
127118
}
128119

129120
/**

0 commit comments

Comments
 (0)