Browse code

Add and get statistics for repository

Niklas Modess authored on 19/05/2014 18:37:15
Showing 3 changed files
... ...
@@ -15,11 +15,13 @@ use Gitter\Model\Commit\Commit;
15 15
 use Gitter\Model\Tree;
16 16
 use Gitter\Model\Blob;
17 17
 use Gitter\Model\Commit\Diff;
18
+use Gitter\Statistics\StatisticsInterface;
18 19
 
19 20
 class Repository
20 21
 {
21 22
     protected $path;
22 23
     protected $client;
24
+    protected $statitics = array();
23 25
 
24 26
     public function __construct($path, Client $client)
25 27
     {
... ...
@@ -70,6 +72,33 @@ class Repository
70 70
     }
71 71
 
72 72
     /**
73
+     * Add statistic aggregator
74
+     *
75
+     * @param StatisticsInterface|array $statitics
76
+     */
77
+    public function addStatistics ($statitics)
78
+    {
79
+        if (!is_array($statitics)) {
80
+            $statitics = array($statitics);
81
+        }
82
+
83
+        foreach ($statitics as $statistic) {
84
+            $className = get_class($statistic);
85
+            $this->statistics[strtolower($className)] = $statistic;
86
+        }
87
+    }
88
+
89
+    /**
90
+     * Get statistic aggregators
91
+     *
92
+     * @return array
93
+     */
94
+    public function getStatistics ()
95
+    {
96
+        return $this->statistics;
97
+    }
98
+
99
+    /**
73 100
      * Add untracked files
74 101
      *
75 102
      * @param mixed $files Files to be added to the repository
76 103
new file mode 100644
... ...
@@ -0,0 +1,6 @@
0
+<?php
1
+namespace Gitter\Statitics;
2
+
3
+interface StatisticsInterface
4
+{
5
+}
... ...
@@ -5,6 +5,7 @@ namespace Gitter\Tests;
5 5
 use Gitter\Client;
6 6
 use Gitter\Repository;
7 7
 use Gitter\Model\Symlink;
8
+use Gitter\Statitics\StatiticsInterface;
8 9
 use Symfony\Component\Filesystem\Filesystem;
9 10
 
10 11
 class RepositoryTest extends \PHPUnit_Framework_TestCase
... ...
@@ -43,6 +44,11 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase
43 43
         $this->client = new Client($path);
44 44
     }
45 45
 
46
+    public function tearDown ()
47
+    {
48
+        \Mockery::close();
49
+    }
50
+
46 51
     public function testIsCreatingRepositoryFixtures()
47 52
     {
48 53
         $a = $this->client->createRepository(self::$tmpdir . '/testrepo');
... ...
@@ -483,6 +489,20 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase
483 483
         $this->assertEquals('testfile.txt', $diffs[1]->getFile(), 'Old file name');
484 484
 	}
485 485
 
486
+    public function testIsAddingSingleStatistics ()
487
+    {
488
+        $statisticsMock = \Mockery::mock('StatiticsInterface');
489
+
490
+        $repo = $this->client->createRepository(self::$tmpdir . '/teststatsrepo');
491
+        $repo->addStatistics($statisticsMock);
492
+
493
+        $this->assertEquals(
494
+            array(strtolower(get_class($statisticsMock)) => $statisticsMock),
495
+            $repo->getStatistics(),
496
+            'Failed to add single statistics'
497
+        );
498
+    }
499
+
486 500
     public static function tearDownAfterClass()
487 501
     {
488 502
         $fs = new Filesystem();