Skip to content

Commit 75c8e7e

Browse files
author
Roman Syroeshko
committed
[NEW] Introduced CopyFileException.
1 parent 369f55a commit 75c8e7e

File tree

5 files changed

+89
-7
lines changed

5 files changed

+89
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ This release added form fields (textinput, checkbox, and dropdown), drawing shap
4444
- PclZip: Remove temporary file after used - @andrew-kzoo GH-265
4545
- Autoloader: Add the ability to set the autoloader options - @bskrtich GH-267
4646
- Element: Refactor elements to move set relation Id from container to element - @ivanlanin
47-
- Introduced CreateTemporaryFileException - @RomanSyroeshko
47+
- Introduced CreateTemporaryFileException, CopyFileException - @RomanSyroeshko
4848

4949
## 0.11.1 - 2 June 2014
5050

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Exception;
19+
20+
/**
21+
* @since 0.12.0
22+
*/
23+
final class CopyFileException extends Exception
24+
{
25+
/**
26+
* @param string $source The fully qualified source file name.
27+
* @param string $destination The fully qualified destination file name.
28+
* @param integer $code The user defined exception code.
29+
* @param \Exception $previous The previous exception used for the exception chaining.
30+
*/
31+
final public function __construct($source, $destination, $code = 0, \Exception $previous = null)
32+
{
33+
parent::__construct(
34+
sprintf('Could not copy \'%s\' file to \'%s\'.', $source, $destination),
35+
$code,
36+
$previous
37+
);
38+
}
39+
}

src/PhpWord/Template.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace PhpOffice\PhpWord;
1919

20+
use PhpOffice\PhpWord\Exception\CopyFileException;
2021
use PhpOffice\PhpWord\Exception\CreateTemporaryFileException;
2122
use PhpOffice\PhpWord\Exception\Exception;
2223
use PhpOffice\PhpWord\Shared\String;
@@ -58,11 +59,11 @@ class Template
5859
/**
5960
* Create a new Template Object
6061
*
61-
* @since 0.12.0 Throws CreateTemporaryFileException instead of Exception.
62+
* @since 0.12.0 Throws CreateTemporaryFileException and CopyFileException instead of Exception.
6263
*
6364
* @param string $strFilename
6465
* @throws \PhpOffice\PhpWord\Exception\CreateTemporaryFileException
65-
* @throws \PhpOffice\PhpWord\Exception\Exception
66+
* @throws \PhpOffice\PhpWord\Exception\CopyFileException
6667
*/
6768
public function __construct($strFilename)
6869
{
@@ -72,8 +73,8 @@ public function __construct($strFilename)
7273
}
7374

7475
// Copy the source File to the temp File
75-
if (!copy($strFilename, $this->tempFileName)) {
76-
throw new Exception("Could not copy the template from {$strFilename} to {$this->tempFileName}.");
76+
if (false === copy($strFilename, $this->tempFileName)) {
77+
throw new CopyFileException($strFilename, $this->tempFileName);
7778
}
7879

7980
$this->zipClass = new ZipArchive();

src/PhpWord/Writer/AbstractWriter.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace PhpOffice\PhpWord\Writer;
1919

20+
use PhpOffice\PhpWord\Exception\CopyFileException;
2021
use PhpOffice\PhpWord\Exception\Exception;
2122
use PhpOffice\PhpWord\PhpWord;
2223
use PhpOffice\PhpWord\Shared\ZipArchive;
@@ -233,14 +234,16 @@ protected function getTempFile($filename)
233234

234235
/**
235236
* Cleanup temporary file
237+
*
238+
* @throws \PhpOffice\PhpWord\Exception\CopyFileException
236239
*/
237240
protected function cleanupTempFile()
238241
{
239242
if ($this->originalFilename != $this->tempFilename) {
240243
// @codeCoverageIgnoreStart
241244
// Can't find any test case. Uncomment when found.
242-
if (copy($this->tempFilename, $this->originalFilename) === false) {
243-
throw new Exception("Could not copy temporary zip file.");
245+
if (false === copy($this->tempFilename, $this->originalFilename)) {
246+
throw new CopyFileException($this->tempFilename, $this->originalFilename);
244247
}
245248
// @codeCoverageIgnoreEnd
246249
@unlink($this->tempFilename);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* This file is part of PHPWord - A pure PHP library for reading and writing
4+
* word processing documents.
5+
*
6+
* PHPWord is free software distributed under the terms of the GNU Lesser
7+
* General Public License version 3 as published by the Free Software Foundation.
8+
*
9+
* For the full copyright and license information, please read the LICENSE
10+
* file that was distributed with this source code. For the full list of
11+
* contributors, visit https://github.com/PHPOffice/PHPWord/contributors.
12+
*
13+
* @link https://github.com/PHPOffice/PHPWord
14+
* @copyright 2010-2014 PHPWord contributors
15+
* @license http://www.gnu.org/licenses/lgpl.txt LGPL version 3
16+
*/
17+
18+
namespace PhpOffice\PhpWord\Tests\Exception;
19+
20+
use PhpOffice\PhpWord\Exception\CopyFileException;
21+
22+
/**
23+
* @covers \PhpOffice\PhpWord\Exception\CopyFileException
24+
* @coversDefaultClass \PhpOffice\PhpWord\Exception\CopyFileException
25+
*/
26+
class CopyFileExceptionTest extends \PHPUnit_Framework_TestCase
27+
{
28+
/**
29+
* CopyFileException can be thrown.
30+
*
31+
* @covers ::__construct()
32+
* @expectedException \PhpOffice\PhpWord\Exception\CopyFileException
33+
* @test
34+
*/
35+
public function testCopyFileExceptionCanBeThrown()
36+
{
37+
throw new CopyFileException('C:\source\dummy.txt', 'C:\destination\dummy.txt');
38+
}
39+
}

0 commit comments

Comments
 (0)