Share blob size loading code
Share blob size loading code

--- a/include/git/GitObjectManager.class.php
+++ b/include/git/GitObjectManager.class.php
@@ -323,7 +323,7 @@
 			if ($this->compat) {
 				$strategy = new GitPHP_BlobLoad_Git($this->exe);
 			} else {
-				$strategy = new GitPHP_BlobLoad_Raw($this->objectLoader);
+				$strategy = new GitPHP_BlobLoad_Raw($this->objectLoader, $this->exe);
 			}
 
 			if ($blob) {

--- /dev/null
+++ b/include/git/blob/BlobLoad_Base.class.php
@@ -1,1 +1,50 @@
+<?php
+/**
+ * Base blob load strategoy
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2013 Christopher Han
+ * @package GitPHP
+ * @subpackage Git\Blob
+ */
+abstract class GitPHP_BlobLoad_Base implements GitPHP_BlobLoadStrategy_Interface
+{
+	/**
+	 * Executable
+	 *
+	 * @var GitPHP_GitExe
+	 */
+	protected $exe;
 
+	/**
+	 * Constructor
+	 *
+	 * @param GitPHP_GitExe $exe executable
+	 */
+	public function __construct($exe)
+	{
+		if (!$exe)
+			throw new Exception('Git exe is required');
+
+		$this->exe = $exe;
+	}
+
+	/**
+	 * Load blob size using git
+	 *
+	 * @param GitPHP_Blob $blob blob
+	 * @return int blob size
+	 */
+	protected function LoadSize($blob)
+	{
+		if (!$blob)
+			return;
+
+		$args = array();
+		$args[] = '-s';
+		$args[] = $blob->GetHash();
+
+		return $this->exe->Execute($blob->GetProject()->GetPath(), GIT_CAT_FILE, $args);
+	}
+}
+

--- a/include/git/blob/BlobLoad_Git.class.php
+++ b/include/git/blob/BlobLoad_Git.class.php
@@ -7,28 +7,8 @@
  * @package GitPHP
  * @subpackage Git\Blob
  */
-class GitPHP_BlobLoad_Git implements GitPHP_BlobLoadStrategy_Interface
+class GitPHP_BlobLoad_Git extends GitPHP_BlobLoad_Base
 {
-	/**
-	 * Executable
-	 *
-	 * @var GitPHP_GitExe
-	 */
-	protected $exe;
-
-	/**
-	 * Constructor
-	 *
-	 * @param GitPHP_GitExe $exe executable
-	 */
-	public function __construct($exe)
-	{
-		if (!$exe)
-			throw new Exception('Git exe is required');
-
-		$this->exe = $exe;
-	}
-
 	/**
 	 * Gets the data for a blob
 	 *
@@ -55,14 +35,7 @@
 	 */
 	public function Size($blob)
 	{
-		if (!$blob)
-			return;
-
-		$args = array();
-		$args[] = '-s';
-		$args[] = $blob->GetHash();
-
-		return $this->exe->Execute($blob->GetProject()->GetPath(), GIT_CAT_FILE, $args);
+		return $this->LoadSize($blob);
 	}
 }
 

--- a/include/git/blob/BlobLoad_Raw.class.php
+++ b/include/git/blob/BlobLoad_Raw.class.php
@@ -7,7 +7,7 @@
  * @package GitPHP
  * @subpackage Git\Blob
  */
-class GitPHP_BlobLoad_Raw implements GitPHP_BlobLoadStrategy_Interface
+class GitPHP_BlobLoad_Raw extends GitPHP_BlobLoad_Base
 {
 	/**
 	 * Object loader
@@ -20,13 +20,16 @@
 	 * Constructor
 	 *
 	 * @param GitPHP_GitObjectLoader $objectLoader object loader
+	 * @param GitPHP_GitExe $exe git exe
 	 */
-	public function __construct($objectLoader)
+	public function __construct($objectLoader, $exe)
 	{
 		if (!$objectLoader)
 			throw new Exception('Git object loader is required');
 
 		$this->objectLoader = $objectLoader;
+
+		parent::__construct($exe);
 	}
 
 	/**

--- a/test/git/blob/BlobLoad_RawTest.php
+++ b/test/git/blob/BlobLoad_RawTest.php
@@ -15,8 +15,9 @@
 		$blobmock->expects($this->any())->method('GetHash')->will($this->returnValue('1234567890abcdef1234567890ABCDEF12345678'));
 		$loadermock = $this->getMockBuilder('GitPHP_GitObjectLoader')->disableOriginalConstructor()->getMock();
 		$loadermock->expects($this->once())->method('GetObject')->with($this->equalTo('1234567890abcdef1234567890ABCDEF12345678'))->will($this->returnValue("blob line 1\nblob line 2"));
+		$exemock = $this->getMock('GitPHP_GitExe');
 
-		$strategy = new GitPHP_BlobLoad_Raw($loadermock);
+		$strategy = new GitPHP_BlobLoad_Raw($loadermock, $exemock);
 		$this->assertEquals("blob line 1\nblob line 2", $strategy->Load($blobmock));
 	}
 }

comments