Ver código fonte

[ProfilerStorage] Make write() returns a status (Boolean)

Victor Berchet 14 anos atrás
pai
commit
3e8f8ea6af

+ 5 - 5
src/Symfony/Component/HttpKernel/Profiler/Profiler.php

@@ -105,7 +105,7 @@ class Profiler
     public function export()
     {
         $data = base64_encode(serialize(array($this->token, $this->collectors, $this->ip, $this->url, $this->time)));
-        
+
         return $data;
     }
 
@@ -243,10 +243,10 @@ class Profiler
         $this->time = time();
 
         $data = base64_encode(serialize($this->collectors));
-        try {
-            $this->storage->write($this->token, $data, $this->ip, $this->url, $this->time);
-            $this->empty = false;
-        } catch (\Exception $e) {
+
+        if (true === $this->storage->write($this->token, $data, $this->ip, $this->url, $this->time)) {
+            $this->empty =false;
+        } else {
             if (null !== $this->logger) {
                 $this->logger->err(sprintf('Unable to store the profiler information (%s).', $e->getMessage()));
             }

+ 3 - 1
src/Symfony/Component/HttpKernel/Profiler/ProfilerStorageInterface.php

@@ -41,13 +41,15 @@ interface ProfilerStorageInterface
     function read($token);
 
     /**
-     * Reads data associated with the given token.
+     * Write data associated with the given token.
      *
      * @param string  $token A token
      * @param string  $data  The data associated with token
      * @param string  $ip    An IP
      * @param string  $url   An URL
      * @param integer $time  The time of the data
+     *
+     * @return Boolean Write operation successful
      */
     function write($token, $data, $ip, $url, $time);
 

+ 8 - 1
src/Symfony/Component/HttpKernel/Profiler/SQLiteProfilerStorage.php

@@ -92,9 +92,16 @@ class SQLiteProfilerStorage implements ProfilerStorageInterface
             ':time'         => $time,
             ':created_at'   => time(),
         );
-        $this->exec($db, 'INSERT INTO data (token, data, ip, url, time, created_at) VALUES (:token, :data, :ip, :url, :time, :created_at)', $args);
+        try {
+            $this->exec($db, 'INSERT INTO data (token, data, ip, url, time, created_at) VALUES (:token, :data, :ip, :url, :time, :created_at)', $args);
+            $status = true;
+        } catch (\Exception $e) {
+            $status = false;
+        }
         $this->cleanup();
         $this->close($db);
+
+        return $status;
     }
 
     /**

+ 6 - 1
tests/Symfony/Tests/Component/HttpKernel/Profiler/SQLiteProfilerStorageTest.php

@@ -32,7 +32,6 @@ class SQLiteProfilerStorageTest extends \PHPUnit_Framework_TestCase
         @unlink(self::$dbFile);
     }
 
-
     protected function setUp()
     {
         self::$storage->purge();
@@ -59,6 +58,12 @@ class SQLiteProfilerStorageTest extends \PHPUnit_Framework_TestCase
         $this->assertTrue(false !== self::$storage->read('backslash'), '->write() accpets backslash in URL');
     }
 
+    public function testStoreDuplicateToken()
+    {
+        $this->assertTrue(true === self::$storage->write('token', 'data', '127.0.0.1', 'http://foo.bar', time()), '->write() returns true when the token is unique');
+        $this->assertTrue(false === self::$storage->write('token', 'data', '127.0.0.1', 'http://foo.bar', time()), '->write() return false when the token is already present in the DB');
+    }
+
     public function testRetrieveByIp()
     {
         self::$storage->write('token', 'data', '127.0.0.1', 'http://foo.bar', time());