Skip to content

Commit 474ab34

Browse files
davidbenatespina
authored andcommitted
Created DoctrineAdminRepository and PdoAdminRepository
1 parent 623ecae commit 474ab34

16 files changed

Lines changed: 304 additions & 222 deletions

src/LIN3S/AdminBundle/Configuration/Factory/EntityConfigurationFactory.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@
1616
use LIN3S\AdminBundle\Configuration\Model\ListField;
1717
use LIN3S\AdminBundle\Configuration\Model\ListFilter;
1818
use LIN3S\AdminBundle\Registry\ServiceRegistry;
19-
use LIN3S\AdminBundle\Repository\QueryBuilder;
19+
use LIN3S\AdminBundle\Repository\AdminRepositoryFactory;
2020

2121
final class EntityConfigurationFactory
2222
{
2323
private $config;
2424
private $actions;
2525
private $listFields;
2626
private $listFilters;
27-
private $queryBuilder;
27+
private $repositoryFactory;
2828

2929
public function __construct(
3030
$config,
3131
ServiceRegistry $actions,
3232
ServiceRegistry $listFields,
3333
ServiceRegistry $listFilters,
34-
QueryBuilder $queryBuilder
34+
AdminRepositoryFactory $repositoryFactory
3535
) {
3636
$this->config = $config['entities'];
3737
$this->actions = $actions;
3838
$this->listFields = $listFields;
3939
$this->listFilters = $listFilters;
40-
$this->queryBuilder = $queryBuilder;
40+
$this->repositoryFactory = $repositoryFactory;
4141
}
4242

4343
public function createFor($entity)
@@ -52,7 +52,7 @@ public function createFor($entity)
5252
$this->listFieldsForEntity($entity),
5353
$this->listFiltersForEntity($entity),
5454
$entityConfig['list']['global_actions'],
55-
$this->queryBuilder,
55+
$this->repositoryFactory->build($entityConfig['persistence_strategy']),
5656
$entityConfig['name'],
5757
$entityConfig['list']['amount_per_page'],
5858
$entityConfig['list']['order_by']

src/LIN3S/AdminBundle/Configuration/Model/Entity.php

Lines changed: 16 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -11,109 +11,25 @@
1111

1212
namespace LIN3S\AdminBundle\Configuration\Model;
1313

14-
use LIN3S\AdminBundle\Repository\QueryBuilder;
14+
use LIN3S\AdminBundle\Repository\AdminRepository;
1515

1616
/**
17-
* Entity configuration.
18-
*
1917
* @author Gorka Laucirica <gorka.lauzirika@gmail.com>
2018
*/
2119
final class Entity
2220
{
23-
/**
24-
* Collection of actions.
25-
*
26-
* @var array
27-
*/
28-
protected $actions;
29-
30-
/**
31-
* The entity name.
32-
*
33-
* @var string
34-
*/
35-
protected $name;
36-
37-
/**
38-
* The class name.
39-
*
40-
* @var string
41-
*/
42-
protected $className;
43-
44-
/**
45-
* The list actions.
46-
*
47-
* @var array
48-
*/
49-
protected $listActions;
50-
51-
/**
52-
* The list fields.
53-
*
54-
* @var array
55-
*/
56-
protected $listFields;
57-
58-
/**
59-
* The list filters.
60-
*
61-
* @var array
62-
*/
63-
protected $listFilters;
64-
65-
/**
66-
* The entities per page.
67-
*
68-
* @var int
69-
*/
70-
protected $listEntitiesPerPage;
71-
72-
/**
73-
* The list global actions.
74-
*
75-
* @var array
76-
*/
77-
protected $listGlobalActions;
78-
79-
/**
80-
* The list order by default.
81-
*
82-
* @var array
83-
*/
84-
protected $listOrderByDefault;
85-
86-
/**
87-
* The entity name, for visualization purposes.
88-
*
89-
* It contains the singular and plural cases.
90-
*
91-
* @var array
92-
*/
21+
private $actions;
22+
private $name;
23+
private $className;
24+
private $listActions;
25+
private $listFields;
26+
private $listFilters;
27+
private $listEntitiesPerPage;
28+
private $listGlobalActions;
29+
private $listOrderByDefault;
9330
private $printNames;
31+
private $repository;
9432

95-
/**
96-
* The query builder.
97-
*
98-
* @var QueryBuilder
99-
*/
100-
protected $queryBuilder;
101-
102-
/**
103-
* EntityConfiguration constructor.
104-
*
105-
* @param string $name The entity name
106-
* @param string $className The class name
107-
* @param array $actions Collection of actions
108-
* @param array $listActions List actions
109-
* @param array $listFields List fields
110-
* @param array $listFilters List filters
111-
* @param array $listGlobalActions List global actions
112-
* @param QueryBuilder $queryBuilder The query builder
113-
* @param array $printNames The entity name, for visualization purposes
114-
* @param int $listEntitiesPerPage The number of entities per page
115-
* @param array $listOrderByDefault The order by default
116-
*/
11733
public function __construct(
11834
$name,
11935
$className,
@@ -122,7 +38,7 @@ public function __construct(
12238
array $listFields = [],
12339
array $listFilters = [],
12440
array $listGlobalActions = [],
125-
QueryBuilder $queryBuilder,
41+
AdminRepository $repository,
12642
array $printNames,
12743
$listEntitiesPerPage,
12844
array $listOrderByDefault = []
@@ -135,7 +51,7 @@ public function __construct(
13551
$this->actions = $actions;
13652
$this->listActions = $listActions;
13753
$this->listGlobalActions = $listGlobalActions;
138-
$this->queryBuilder = $queryBuilder;
54+
$this->repository = $repository;
13955
$this->printNames = $printNames;
14056
$this->listEntitiesPerPage = $listEntitiesPerPage;
14157
$this->listOrderByDefault = $listOrderByDefault;
@@ -368,10 +284,10 @@ public function printNames()
368284
/**
369285
* Gets the query builder.
370286
*
371-
* @return QueryBuilder
287+
* @return AdminRepository
372288
*/
373-
public function queryBuilder()
289+
public function repository()
374290
{
375-
return $this->queryBuilder;
291+
return $this->repository;
376292
}
377293
}

src/LIN3S/AdminBundle/Controller/AdminController.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ class AdminController extends Controller
2424
/**
2525
* List action.
2626
*
27-
* @param string $entity The entity name
28-
* @param Request $request The request
27+
* @param string $entity The entity name
28+
* @param Request $request The request
2929
*
3030
* @return \Symfony\Component\HttpFoundation\Response
3131
*/
3232
public function listAction($entity, Request $request)
3333
{
3434
$entityConfig = $this->get('lin3s_admin.configuration.factory.entity')->createFor($entity);
35-
$entities = $this->get('lin3s_admin.repository')->findByRequest($request, $entityConfig);
36-
$totalCount = $this->get('lin3s_admin.repository')->countAll($request, $entityConfig);
35+
$repository = $entityConfig->repository();
36+
$entities = $repository->findByRequest($request, $entityConfig);
37+
$totalCount = $repository->countAll($request, $entityConfig);
3738

3839
return $this->render('@Lin3sAdmin/Admin/list.html.twig', [
3940
'entities' => $entities,
@@ -57,8 +58,7 @@ public function customAction($entity, $action, $id = null, Request $request)
5758
$entityConfig = $this->get('lin3s_admin.configuration.factory.entity')->createFor($entity);
5859
$entityObject = null;
5960
if ($id) {
60-
$manager = $this->getDoctrine()->getRepository($entityConfig->className());
61-
$entityObject = $manager->find($id);
61+
$entityObject = $entityConfig->repository()->find($entityConfig, $id);
6262
}
6363
if ($id && !$entityObject) {
6464
throw $this->createNotFoundException(
@@ -73,7 +73,11 @@ public function customAction($entity, $action, $id = null, Request $request)
7373
$callableAction = $entityConfig->getAction($action);
7474
} catch (\Exception $e) {
7575
throw $this->createNotFoundException(
76-
sprintf('Action "%s" for entity "%s" not found, did you registered it in the config?', $action, $entity)
76+
sprintf(
77+
'Action "%s" for entity "%s" not found, did you registered it in the config?',
78+
$action,
79+
$entity
80+
)
7781
);
7882
}
7983

src/LIN3S/AdminBundle/DependencyInjection/Configuration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public function getConfigTreeBuilder()
3232
->arrayNode('entities')->requiresAtLeastOneElement()
3333
->prototype('array')
3434
->children()
35+
->scalarNode('persistence_strategy')
36+
->defaultValue('doctrine')
37+
->end()
3538
->arrayNode('name')
3639
->children()
3740
->scalarNode('singular')->end()

src/LIN3S/AdminBundle/Repository/AdminRepository.php

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,13 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616

1717
/**
18-
* Admin repository.
19-
*
2018
* @author Gorka Laucirica <gorka.lauzirika@gmail.com>
2119
*/
22-
class AdminRepository
20+
interface AdminRepository
2321
{
24-
/**
25-
* Finds the result about request criteria and given entity configuration.
26-
*
27-
* @param Request $request The request
28-
* @param Entity $config The entity configuration
29-
*
30-
* @return array
31-
*/
32-
public function findByRequest(Request $request, Entity $config)
33-
{
34-
$queryBuilder = $config->queryBuilder()->generate($request, $config);
35-
36-
$postPerPage = $config->listEntitiesPerPage();
37-
38-
$offset = ($request->get('page', 1) - 1) * $postPerPage;
39-
$limit = $postPerPage;
40-
41-
$queryBuilder->setFirstResult($offset);
42-
$queryBuilder->setMaxResults($limit);
43-
44-
return $queryBuilder->getQuery()->getResult();
45-
}
22+
public function find(Entity $config, $id);
4623

47-
/**
48-
* Counts all the result about request criteria and given entity configuration.
49-
*
50-
* @param Request $request The request
51-
* @param Entity $config The entity configuration
52-
*
53-
* @return int
54-
*/
55-
public function countAll(Request $request, Entity $config)
56-
{
57-
$queryBuilder = $config->queryBuilder()->generate($request, $config);
58-
$queryBuilder->select($queryBuilder->expr()->count('a'));
24+
public function findByRequest(Request $request, Entity $config);
5925

60-
return count($queryBuilder->getQuery()->getScalarResult());
61-
}
26+
public function countAll(Request $request, Entity $config);
6227
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Admin Bundle.
5+
*
6+
* Copyright (c) 2015-present LIN3S <info@lin3s.com>
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 LIN3S\AdminBundle\Repository;
13+
14+
/**
15+
* @author Beña Espiña <benatespina@gmail.com>
16+
*/
17+
class AdminRepositoryFactory
18+
{
19+
private $repositories;
20+
21+
public function __construct($repositories)
22+
{
23+
$this->repositories = $repositories;
24+
}
25+
26+
public function build($strategy)
27+
{
28+
if (!array_key_exists($strategy, $this->repositories)) {
29+
throw new AdminRepositoryStrategyDoesNotExist();
30+
}
31+
32+
return $this->repositories[$strategy];
33+
}
34+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Admin Bundle.
5+
*
6+
* Copyright (c) 2015-present LIN3S <info@lin3s.com>
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 LIN3S\AdminBundle\Repository;
13+
14+
/**
15+
* @author Beña Espiña <benatespina@gmail.com>
16+
*/
17+
class AdminRepositoryStrategyDoesNotExist extends \Exception
18+
{
19+
}

0 commit comments

Comments
 (0)