Skip to content

Commit fff0c7c

Browse files
committed
Simplifying code using VoidCache
1 parent 266671a commit fff0c7c

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

src/SchemaAnalyzer.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Mouf\Database\SchemaAnalyzer;
44

55
use Doctrine\Common\Cache\Cache;
6+
use Doctrine\Common\Cache\VoidCache;
67
use Doctrine\DBAL\Schema\AbstractSchemaManager;
78
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
89
use Doctrine\DBAL\Schema\Schema;
@@ -51,11 +52,15 @@ class SchemaAnalyzer
5152
public function __construct(AbstractSchemaManager $schemaManager, Cache $cache = null, $schemaCacheKey = null)
5253
{
5354
$this->schemaManager = $schemaManager;
54-
$this->cache = $cache;
55-
$this->cachePrefix = $schemaCacheKey;
5655
if (empty($schemaCacheKey) && $cache) {
5756
throw new SchemaAnalyzerException('You must provide a schema cache key if you configure SchemaAnalyzer with cache support.');
5857
}
58+
if ($cache) {
59+
$this->cache = $cache;
60+
} else {
61+
$this->cache = new VoidCache();
62+
}
63+
$this->cachePrefix = $schemaCacheKey;
5964
}
6065

6166
/**
@@ -70,15 +75,10 @@ public function __construct(AbstractSchemaManager $schemaManager, Cache $cache =
7075
public function detectJunctionTables()
7176
{
7277
$junctionTablesKey = $this->cachePrefix."_junctiontables";
73-
$junctionTables = false;
74-
if ($this->cache) {
75-
$junctionTables = $this->cache->fetch($junctionTablesKey);
76-
}
78+
$junctionTables = $this->cache->fetch($junctionTablesKey);
7779
if ($junctionTables === false) {
7880
$junctionTables = array_filter($this->getSchema()->getTables(), [$this, 'isJunctionTable']);
79-
if ($this->cache) {
80-
$this->cache->save($junctionTablesKey, $junctionTables);
81-
}
81+
$this->cache->save($junctionTablesKey, $junctionTables);
8282
}
8383
return $junctionTables;
8484
}
@@ -151,14 +151,10 @@ public function getShortestPath($fromTable, $toTable)
151151
{
152152
$cacheKey = $this->cachePrefix."_shortest_".$fromTable."```".$toTable;
153153
$path = false;
154-
if ($this->cache) {
155-
$path = $this->cache->fetch($cacheKey);
156-
}
154+
$path = $this->cache->fetch($cacheKey);
157155
if ($path === false) {
158156
$path = $this->getShortestPathWithoutCache($fromTable, $toTable);
159-
if ($this->cache) {
160-
$this->cache->save($cacheKey, $path);
161-
}
157+
$this->cache->save($cacheKey, $path);
162158
}
163159
return $path;
164160
}
@@ -256,14 +252,10 @@ private function buildSchemaGraph()
256252
private function getSchema() {
257253
if ($this->schema === null) {
258254
$schemaKey = $this->cachePrefix."_schema";
259-
if ($this->cache) {
260-
$this->schema = $this->cache->fetch($schemaKey);
261-
}
255+
$this->schema = $this->cache->fetch($schemaKey);
262256
if (empty($this->schema)) {
263257
$this->schema = $this->schemaManager->createSchema();
264-
if ($this->cache) {
265-
$this->cache->save($schemaKey, $this->schema);
266-
}
258+
$this->cache->save($schemaKey, $this->schema);
267259
}
268260
}
269261
return $this->schema;

0 commit comments

Comments
 (0)