Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit f2ba880

Browse files
committed
Refactored Factory
1 parent 634fac2 commit f2ba880

File tree

4 files changed

+68
-40
lines changed

4 files changed

+68
-40
lines changed

AutoRoute/AutoRouteManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function __construct(BuilderUnitChainFactory $bucf)
3131
*
3232
* @param object Mapped document for which to generate the AutoRoute
3333
*
34-
* @return AutoRoute
34+
* @return BuilderContext
3535
*/
3636
public function updateAutoRouteForDocument($document)
3737
{

AutoRoute/RouteStackBuilderUnitChainFactory.php renamed to AutoRoute/Factory.php

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22

33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute;
44

5-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStackChain;
65
use Symfony\Component\DependencyInjection\ContainerInterface;
76

87
/**
9-
* Hmm.. the role of this class has changed. It should now
10-
* take care both the RouteStack (content path) and the
11-
* Route Content (content name).
12-
*
138
* @author Daniel Leech <[email protected]>
149
*/
15-
class RouteStackBuilderUnitChainFactory
10+
class Factory
1611
{
1712
protected $mapping;
1813

@@ -21,15 +16,15 @@ class RouteStackBuilderUnitChainFactory
2116
protected $routeStackChains;
2217

2318
protected $serviceIds = array(
24-
'path_provider' => array(),
19+
'provider' => array(),
2520
'exists_action' => array(),
2621
'not_exists_action' => array(),
2722
);
2823

2924
protected $container;
3025
protected $builder;
3126

32-
public function __construct(ContainerInterface $container, BuilderInterface $builder)
27+
public function __construct(ContainerInterface $container, RouteStackBuilder $builder)
3328
{
3429
$this->container = $container;
3530
$this->builder = $builder;
@@ -66,28 +61,54 @@ public function hasMapping($classFqn)
6661

6762
protected function generateRouteStackChain($classFqn)
6863
{
69-
if (!isset($this->mapping[$classFqn])) {
70-
throw new Exception\ClassNotMappedException($classFqn);
71-
}
64+
$mapping = $this->getMapping($classFqn);
7265

73-
$config = $this->mapping[$classFqn];
74-
$routeStackChain = new RouteStackChain($this->builder);
66+
$routeStackChain = new RouteStackBuilderUnitChain($this->builder);
7567

76-
foreach ($config as $builderName => $builderConfig) {
68+
foreach ($mapping['content_path'] as $builderName => $builderConfig) {
7769
$pathProvider = $this->getBuilderService($builderConfig, 'provider', 'name');
7870
$existsAction = $this->getBuilderService($builderConfig, 'exists_action', 'strategy');
7971
$notExistsAction = $this->getBuilderService($builderConfig, 'not_exists_action', 'strategy');
8072

81-
$stackBuilder = new RouteStackBuilder(
73+
$stackBuilderUnit = new RouteStackBuilderUnit(
8274
$pathProvider,
8375
$existsAction,
8476
$notExistsAction
8577
);
8678

87-
$routeStackChain->addBuilder($builderName, $stackBuilder);
79+
$routeStackChain->addRouteStackBuilderUnit($builderName, $stackBuilderUnit);
80+
}
81+
82+
return $routeStackChain;
83+
}
84+
85+
protected function getMapping($classFqn)
86+
{
87+
if (!isset($this->mapping[$classFqn])) {
88+
throw new Exception\ClassNotMappedException($classFqn);
8889
}
8990

90-
return $chain;
91+
$mapping = $this->mapping[$classFqn];
92+
$this->validateMapping($classFqn, $mapping);
93+
94+
return $mapping;
95+
}
96+
97+
private function validateMapping($classFqn, $mapping)
98+
{
99+
$exists = function ($name, $check) use ($classFqn, $mapping) {
100+
if (!$check($mapping)) {
101+
throw new \RuntimeException(sprintf(
102+
'%s not defined in mapping for class "%s": %s',
103+
$name,
104+
$classFqn,
105+
print_r($mapping, true)
106+
));
107+
}
108+
};
109+
110+
$exists('content_path', function ($mapping) { return isset($mapping['content_path']); });
111+
$exists('content_name', function ($mapping) { return isset($mapping['content_name']); });
91112
}
92113

93114
private function getBuilderService($builderConfig, $type, $aliasKey)

Tests/AutoRoute/BuilderUnitChainFactoryTest.php renamed to Tests/AutoRoute/FactoryTest.php

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,22 @@
22

33
namespace Symfony\Cmf\Bundle\RoutingAutoBundle\Tests\AutoRoute;
44

5-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\AutoRouteManager;
6-
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
7-
use Doctrine\Common\Collections\ArrayCollection;
85
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderContext;
9-
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderUnitChainFactory;
6+
use Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\Factory;
107

11-
class BuilderUnitChainFactoryTest extends \PHPUnit_Framework_TestCase
8+
class FactoryTest extends \PHPUnit_Framework_TestCase
129
{
1310
public function setUp()
1411
{
15-
$this->builder = $this->getMock(
16-
'Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\BuilderInterface'
17-
);
12+
$this->builder = $this->getMockBuilder(
13+
'Symfony\Cmf\Bundle\RoutingAutoBundle\AutoRoute\RouteStackBuilder'
14+
)->disableOriginalConstructor()->getMock();
1815

1916
$this->container = $this->getMock(
2017
'Symfony\Component\DependencyInjection\ContainerInterface'
2118
);
2219

23-
$this->bucf = new BuilderUnitChainFactory(
20+
$this->bucf = new Factory(
2421
$this->container, $this->builder
2522
);
2623

@@ -36,8 +33,8 @@ public function setUp()
3633
'throw_excep_service_id' => $this->throwExceptionPath,
3734
);
3835

39-
$this->bucf->registerAlias('path_provider', 'fixed', 'fixed_service_id');
40-
$this->bucf->registerAlias('path_provider', 'dynamic', 'dynamic_service_id');
36+
$this->bucf->registerAlias('provider', 'fixed', 'fixed_service_id');
37+
$this->bucf->registerAlias('provider', 'dynamic', 'dynamic_service_id');
4138
$this->bucf->registerAlias('exists_action', 'create', 'create_service_id');
4239
$this->bucf->registerAlias('not_exists_action', 'throw_excep', 'throw_excep_service_id');
4340
}
@@ -55,16 +52,26 @@ public function provideTestGetChain()
5552
return array(
5653
array(
5754
array(
58-
'base' => array(
59-
'path_provider' => array(
60-
'name' => 'fixed',
61-
'message' => 'foobar'
62-
),
63-
'exists_action' => array(
64-
'strategy' => 'create'
55+
'content_path' => array(
56+
'base' => array(
57+
'provider' => array(
58+
'name' => 'fixed',
59+
'message' => 'foobar'
60+
),
61+
'exists_action' => array(
62+
'strategy' => 'create'
63+
),
64+
'not_exists_action' => array(
65+
'strategy' => 'throw_excep',
66+
),
6567
),
66-
'not_exists_action' => array(
67-
'strategy' => 'throw_excep',
68+
),
69+
'content_name' => array(
70+
'base' => array(
71+
'provider' => array(
72+
'name' => 'fixed',
73+
'message' => 'barfoo'
74+
),
6875
),
6976
),
7077
),
@@ -80,8 +87,8 @@ public function provideTestGetChain()
8087
*/
8188
public function testGetChain($config, $assertOptions)
8289
{
83-
$this->bucf->registerAlias('path_provider', 'fixed', 'fixed_service_id');
84-
$this->bucf->registerAlias('path_provider', 'dynamic', 'dynamic_service_id');
90+
$this->bucf->registerAlias('provider', 'fixed', 'fixed_service_id');
91+
$this->bucf->registerAlias('provider', 'dynamic', 'dynamic_service_id');
8592
$this->bucf->registerAlias('exists_action', 'create', 'create_service_id');
8693
$this->bucf->registerAlias('not_exists_action', 'throw_excep', 'throw_excep_service_id');
8794

Tests/Functional/app/console

100644100755
File mode changed.

0 commit comments

Comments
 (0)