Browse code

Allow for statistics to be fetched without getting commits previously

Niklas Modess authored on 24/05/2014 15:03:59
Showing 2 changed files
... ...
@@ -21,6 +21,8 @@ class Repository
21 21
 {
22 22
     protected $path;
23 23
     protected $client;
24
+    protected $commitsHaveBeenParsed = false;
25
+
24 26
     protected $statistics = array();
25 27
 
26 28
     public function __construct($path, Client $client)
... ...
@@ -30,6 +32,23 @@ class Repository
30 30
     }
31 31
 
32 32
     /**
33
+     * @param  bool $value
34
+     * @return void
35
+     */
36
+    public function setCommitsHaveBeenParsed($value)
37
+    {
38
+        $this->commitsHaveBeenParsed = $value;
39
+    }
40
+
41
+    /**
42
+     * @return boolean
43
+     */
44
+    public function getCommitsHaveBeenParsed()
45
+    {
46
+        return $this->commitsHaveBeenParsed;
47
+    }
48
+
49
+    /**
33 50
      * Create a new git repository
34 51
      */
35 52
     public function create($bare = null)
... ...
@@ -95,6 +114,10 @@ class Repository
95 95
      */
96 96
     public function getStatistics ()
97 97
     {
98
+        if ($this->getCommitsHaveBeenParsed() === false) {
99
+            $this->getCommits();
100
+        }
101
+
98 102
         foreach ($this->statistics as $statistic) {
99 103
             $statistic->sortCommits();
100 104
         }
... ...
@@ -348,11 +371,13 @@ class Repository
348 348
             $commit->importData($log);
349 349
             $commits[] = $commit;
350 350
 
351
-            foreach ($this->getStatistics() as $statistic) {
351
+            foreach ($this->statistics as $statistic) {
352 352
                 $statistic->addCommit($commit);
353 353
             }
354 354
         }
355 355
 
356
+        $this->setCommitsHaveBeenParsed(true);
357
+
356 358
         return $commits;
357 359
     }
358 360
 
... ...
@@ -492,9 +492,11 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase
492 492
     public function testIsAddingSingleStatistics ()
493 493
     {
494 494
         $statisticsMock = \Mockery::mock('Gitter\Statistics\StatiticsInterface');
495
-
495
+        $statisticsMock->shouldReceive('sortCommits')->once();
496
+        
496 497
         $repo = $this->client->createRepository(self::$tmpdir . '/teststatsrepo');
497 498
         $repo->addStatistics($statisticsMock);
499
+        $repo->setCommitsHaveBeenParsed(true);
498 500
 
499 501
         $this->assertEquals(
500 502
             array(strtolower(get_class($statisticsMock)) => $statisticsMock),