Completed
Branch BUG-9140-has-billing-form-igno... (f963e1)
by
unknown
257:50 queued 242:55
created

JobHandlerFile::get_url_to_file()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
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
	 *
28
	 * @var EEHI_File
29
	 */
30
	protected $_file_helper = null;
31
	const temp_folder_name = 'batch_temp_folder';
32
	public function __construct( EEHI_File $file_helper = null ) {
33
		if( ! $file_helper ) {
34
			$this->_file_helper = new \EEH_File();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \EEH_File() of type object<EEH_File> is incompatible with the declared type object<EventEspressoBatc...rBaseClasses\EEHI_File> of property $_file_helper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
		}
36
	}
37
38
39
40
	/**
41
	 * Creates a file
42
	 *
43
	 * @param string $job_id
44
	 * @param string $filename
45
	 * @return string
46
	 * @throws BatchRequestException
47
	 */
48
	public function create_file_from_job_with_name( $job_id, $filename ) {
49
		$filepath = '';
50
		try{
51
			$success = $this->_file_helper->ensure_folder_exists_and_is_writable(
52
				EVENT_ESPRESSO_UPLOAD_DIR . JobHandlerFile::temp_folder_name
53
			);
54
			if ( $success ) {
55
				$success = $this->_file_helper->ensure_folder_exists_and_is_writable(
56
					EVENT_ESPRESSO_UPLOAD_DIR . JobHandlerFile::temp_folder_name . DS . $job_id
57
				);
58
			}
59
			if( $success ) {
60
				$filepath = EVENT_ESPRESSO_UPLOAD_DIR . JobHandlerFile::temp_folder_name . DS . $job_id . DS. $filename;
61
				$success = $this->_file_helper->ensure_file_exists_and_is_writable( $filepath );
62
			}
63
			//those methods normally fail with an exception, but if not, let's do it
64
			if( ! $success ) {
65
				throw new \EE_Error( 'could_not_create_temp_file',
66
				__( 'An unknown error occurred', 'event_espresso' ));
67
			}
68
		} catch( \EE_Error $e ) {
69
			throw new BatchRequestException(
70
				sprintf(
71
					__( 'Could not create temporary file for job %1$s, because: %2$s ', 'event_espresso' ),
72
					$job_id,
73
					$e->getMessage()
74
				),
75
				500,
76
				$e
77
			);
78
		}
79
		return $filepath;
80
	}
81
82
	/**
83
	 * Gets the URL to download the file
84
	 * @param string $filepath
85
	 * @return string url to file
86
	 */
87
	public function get_url_to_file( $filepath ) {
88
		return str_replace( EVENT_ESPRESSO_UPLOAD_DIR, EVENT_ESPRESSO_UPLOAD_URL, $filepath );
89
	}
90
}
91
92