Completed
Branch BETA-4.9-messages-queue-fixed (941081)
by
unknown
17:38 queued 10s
created

JobHandlerFile::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 *
4
 * Class JobHandlerFile
5
 *
6
 * Base class for common implementations of JobHandlerInterface, but ones
7
 * which write to a temporary file
8
 *
9
 * @package         Event Espresso
10
 * @subpackage    batch
11
 * @author				Mike Nelson
12
 * @since		 	   4.8.26
13
 *
14
 */
15
namespace EventEspressoBatchRequest\JobHandlerBaseClasses;
16
17
use EventEspressoBatchRequest\Helpers\BatchRequestException;
18
19
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) {
20
	exit( 'No direct script access allowed' );
21
}
22
23
24
25
abstract class JobHandlerFile extends JobHandler {
26
27
	const temp_folder_name = 'batch_temp_folder';
28
29
	/**
30
	 *
31
	 * @var \EEHI_File
32
	 */
33
	protected $_file_helper = null;
34
35
36
37
	/**
38
	 * JobHandlerFile constructor.
39
	 *
40
	 * @param \EEHI_File|null $file_helper
41
	 */
42
	public function __construct( \EEHI_File $file_helper = null ) {
43
		if( ! $file_helper ) {
44
			$this->_file_helper = new \EEH_File();
45
		}
46
	}
47
48
49
50
	/**
51
	 * Creates a file
52
	 *
53
	 * @param string $job_id
54
	 * @param string $filename
55
	 * @param string $filetype
56
	 * @return string
57
	 * @throws \EventEspressoBatchRequest\Helpers\BatchRequestException
58
	 */
59
	public function create_file_from_job_with_name( $job_id, $filename, $filetype = 'application/ms-excel' ) {
60
		$filepath = '';
61
		try{
62
			$success = $this->_file_helper->ensure_folder_exists_and_is_writable(
63
				EVENT_ESPRESSO_UPLOAD_DIR . JobHandlerFile::temp_folder_name
64
			);
65
			if ( $success ) {
66
				$success = $this->_file_helper->ensure_folder_exists_and_is_writable(
67
					EVENT_ESPRESSO_UPLOAD_DIR . JobHandlerFile::temp_folder_name . DS . $job_id
68
				);
69
			}
70
			if( $success ) {
71
				$filepath = EVENT_ESPRESSO_UPLOAD_DIR . JobHandlerFile::temp_folder_name . DS . $job_id . DS. $filename;
72
				$success = $this->_file_helper->ensure_file_exists_and_is_writable( $filepath );
73
			}
74
			//let's add the .htaccess file so safari will open the file properly
75
			if( $success ) {
76
				$extension = \EEH_File::get_file_extension( $filepath );
77
				\EEH_File::write_to_file(
78
					EVENT_ESPRESSO_UPLOAD_DIR . JobHandlerFile::temp_folder_name . DS . $job_id . DS . '.htaccess',
79
					'AddType ' . $filetype . ' ' . $extension,
80
					'.htaccess'
81
				);
82
			}
83
			//those methods normally fail with an exception, but if not, let's do it
84
			if( ! $success ) {
85
				throw new \EE_Error( 'could_not_create_temp_file',
86
				__( 'An unknown error occurred', 'event_espresso' ));
87
			}
88
		} catch( \EE_Error $e ) {
89
			throw new BatchRequestException(
90
				sprintf(
91
					__( 'Could not create temporary file for job %1$s, because: %2$s ', 'event_espresso' ),
92
					$job_id,
93
					$e->getMessage()
94
				),
95
				500,
96
				$e
97
			);
98
		}
99
		return $filepath;
100
	}
101
102
	/**
103
	 * Gets the URL to download the file
104
	 * @param string $filepath
105
	 * @return string url to file
106
	 */
107
	public function get_url_to_file( $filepath ) {
108
		return str_replace( EVENT_ESPRESSO_UPLOAD_DIR, EVENT_ESPRESSO_UPLOAD_URL, $filepath );
109
	}
110
}
111
112