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

Commit d750f9e

Browse files
committed
Various CS fix
1 parent cf18943 commit d750f9e

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

Adapter/PhpcrOdmAdapter.php

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,19 @@ public function removeAutoRoute(AutoRouteInterface $autoRoute)
114114
*/
115115
public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoRouteTag)
116116
{
117-
$path = $this->baseRoutePath;
118-
$routeType = AutoRouteInterface::TYPE_PRIMARY;
119-
$document = $parentDocument = $this->dm->find(null, $path);
117+
$basePath = $this->baseRoutePath;
118+
$document = $parentDocument = $this->dm->find(null, $basePath);
120119
if (null === $parentDocument) {
121120
throw new \RuntimeException(sprintf('The "route_basepath" configuration points to a non-existant path "%s".',
122-
$path
121+
$basePath
123122
));
124123
}
125124

126125
$segments = preg_split('#/#', $uriContext->getUri(), null, PREG_SPLIT_NO_EMPTY);
127126
$headName = array_pop($segments);
128127
foreach ($segments as $segment) {
129-
$path .= '/' . $segment;
130-
$document = $this->dm->find(null, $path);
128+
$basePath .= '/' . $segment;
129+
$document = $this->dm->find(null, $basePath);
131130

132131
if (null === $document) {
133132
$document = new Generic();
@@ -138,24 +137,34 @@ public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoR
138137
$parentDocument = $document;
139138
}
140139

141-
$finalAutoRoutePath = $path . '/' . $headName;
142-
$node = $this->dm->find(null, $finalAutoRoutePath);
143-
if ($node) {
144-
if ($node instanceof Generic) {
145-
return $this->convertGenericNodeInAutoRouteNode($node, $contentDocument, $autoRouteTag, $routeType);
140+
$path = $basePath . '/' . $headName;
141+
$existingDocument = $this->dm->find(null, $path);
142+
143+
if ($existingDocument) {
144+
if ($existingDocument instanceof Generic) {
145+
return $this->migrateGenericToAutoRoute(
146+
$existingDocument,
147+
$contentDocument,
148+
$autoRouteTag,
149+
AutoRouteInterface::TYPE_PRIMARY
150+
);
146151
}
147-
$nodeClass = get_class($node);
148-
$genericFqcn = 'Doctrine\ODM\PHPCR\Document\Generic';
152+
149153
throw new \RuntimeException(
150-
"Unexpected node class '$nodeClass' at path '$finalAutoRoutePath'. Only '$genericFqcn' expected."
154+
sprintf(
155+
'Encountered existing PHPCR-ODM document at path "%s" of class "%s", the route tree should ' .
156+
'contain only instances of AutoRouteInterface.',
157+
$path,
158+
get_class($existingDocument)
159+
)
151160
);
152161
}
153162
$headRoute = new $this->autoRouteFqcn();
154163
$headRoute->setContent($contentDocument);
155164
$headRoute->setName($headName);
156165
$headRoute->setParent($document);
157166
$headRoute->setAutoRouteTag($autoRouteTag);
158-
$headRoute->setType($routeType);
167+
$headRoute->setType(AutoRouteInterface::TYPE_PRIMARY);
159168

160169
return $headRoute;
161170
}
@@ -203,11 +212,13 @@ public function getReferringAutoRoutes($contentDocument)
203212
public function findRouteForUri($uri, UriContext $uriContext)
204213
{
205214
$path = $this->getPathFromUri($uri);
206-
$node = $this->dm->find(null, $path);
207-
if ($node instanceof AutoRouteInterface) {
208-
return $node;
215+
$document = $this->dm->find(null, $path);
216+
217+
if ($document instanceof AutoRouteInterface) {
218+
return $document;
209219
}
210-
return false;
220+
221+
return null;
211222
}
212223

213224
private function getPathFromUri($uri)
@@ -216,29 +227,34 @@ private function getPathFromUri($uri)
216227
}
217228

218229
/**
219-
* @param Generic $node
230+
* Convert the given generic document to an auto route document, migrating any children which it may have.
231+
*
232+
* @param Generic $document
220233
* @param object $contentDocument
221234
* @param string $autoRouteTag
222235
* @param string $routeType
223236
* @return AutoRouteInterface
224237
*/
225-
private function convertGenericNodeInAutoRouteNode(Generic $node, $contentDocument, $autoRouteTag, $routeType)
238+
private function migrateGenericToAutoRoute(Generic $document, $contentDocument, $autoRouteTag, $routeType)
226239
{
227240
$autoRouteClassName = $this->autoRouteFqcn;
228241
$mapper = $this->dm->getConfiguration()->getDocumentClassMapper();
229-
$mapper->writeMetadata($this->dm, $node->getNode(), $autoRouteClassName);
242+
$mapper->writeMetadata($this->dm, $document->getNode(), $autoRouteClassName);
230243
$this->dm->getPhpcrSession()->save();
231244
// Detach is needed to force Doctrine to re-load the node
232-
$this->dm->detach($node);
233-
$autoRoute = $this->dm->find(null, $node->getId());
245+
$this->dm->detach($document);
246+
$autoRoute = $this->dm->find(null, $document->getId());
247+
234248
if (!$autoRoute instanceof $autoRouteClassName) {
235249
throw new \RuntimeException(
236-
"Something went wrong converting Generic node into an AutoRouteInterface node."
250+
'Something went wrong converting Generic node into an AutoRouteInterface node.'
237251
);
238252
}
253+
239254
$autoRoute->setContent($contentDocument);
240255
$autoRoute->setAutoRouteTag($autoRouteTag);
241256
$autoRoute->setType($routeType);
257+
242258
return $autoRoute;
243259
}
244260
}

Tests/Functional/EventListener/AutoRouteListenerTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,6 @@ public function testConflictResolverDefaultThrowException()
555555

556556
public function testGenericNodeShouldBeConvertedInAnAutoRouteNode()
557557
{
558-
$this->assertNull($this->getDm()->find(null, '/test/auto-route/blog'));
559-
$this->assertNull($this->getDm()->find(null, '/test/auto-route/blog/my-post'));
560-
561558
$blog = new Blog;
562559
$blog->path = '/test/my-post';
563560
$blog->title = 'My Post';

Tests/Unit/Adapter/PhpcrOdmAdapterTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ public function testFindRouteForUri()
184184
$this->assertSame($expectedRoute, $res);
185185
}
186186

187-
public function testFindRouteForUriShouldReturnFalseWhenNodeAtGivenPathIsNotAnAutoRoute()
187+
public function testFindRouteForUriShouldReturnNullWhenNodeAtGivenPathIsNotAnAutoRoute()
188188
{
189189
$uri = '/this/is/uri';
190190
$genericNode = $this->prophesize('Doctrine\ODM\PHPCR\Document\Generic');
191191
$this->dm->find(null, $this->baseRoutePath . $uri)->willReturn($genericNode);
192-
$this->assertFalse($this->adapter->findRouteForUri($uri, $this->uriContext->reveal()));
192+
$this->assertNull($this->adapter->findRouteForUri($uri, $this->uriContext->reveal()));
193193
}
194194
}

0 commit comments

Comments
 (0)