1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @copyright 2014 Mautic Contributors. All rights reserved |
5
|
|
|
* @author Mautic |
6
|
|
|
* |
7
|
|
|
* @link http://mautic.org |
8
|
|
|
* |
9
|
|
|
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mautic\ReportBundle\Model; |
13
|
|
|
|
14
|
|
|
use Mautic\CoreBundle\Exception\FilePathException; |
15
|
|
|
use Mautic\CoreBundle\Helper\CoreParametersHelper; |
16
|
|
|
use Mautic\CoreBundle\Helper\FilePathResolver; |
17
|
|
|
use Mautic\ReportBundle\Exception\FileIOException; |
18
|
|
|
|
19
|
|
|
class ExportHandler |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $dir; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var FilePathResolver |
28
|
|
|
*/ |
29
|
|
|
private $filePathResolver; |
30
|
|
|
|
31
|
|
|
public function __construct(CoreParametersHelper $coreParametersHelper, FilePathResolver $filePathResolver) |
32
|
|
|
{ |
33
|
|
|
$this->dir = $coreParametersHelper->get('report_temp_dir'); |
34
|
|
|
$this->filePathResolver = $filePathResolver; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $fileName |
39
|
|
|
* |
40
|
|
|
* @return bool|resource |
41
|
|
|
* |
42
|
|
|
* @throws FileIOException |
43
|
|
|
*/ |
44
|
|
|
public function getHandler($fileName) |
45
|
|
|
{ |
46
|
|
|
$path = $this->getPath($fileName); |
47
|
|
|
|
48
|
|
|
if (false === ($handler = @fopen($path, 'a'))) { |
49
|
|
|
throw new FileIOException('Could not open file '.$path); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $handler; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param resource $handler |
57
|
|
|
*/ |
58
|
|
|
public function closeHandler($handler) |
59
|
|
|
{ |
60
|
|
|
fclose($handler); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $fileName |
65
|
|
|
*/ |
66
|
|
|
public function removeFile($fileName) |
67
|
|
|
{ |
68
|
|
|
try { |
69
|
|
|
$path = $this->getPath($fileName); |
70
|
|
|
$this->filePathResolver->delete($path); |
71
|
|
|
} catch (FileIOException $e) { |
|
|
|
|
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $fileName |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
* |
80
|
|
|
* @throws FileIOException |
81
|
|
|
*/ |
82
|
|
|
public function getPath($fileName) |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
$this->filePathResolver->createDirectory($this->dir); |
86
|
|
|
} catch (FilePathException $e) { |
87
|
|
|
throw new FileIOException('Could not create directory '.$this->dir, 0, $e); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->dir.'/'.$fileName.'.csv'; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|