22
33namespace WordPress \ByteReader ;
44
5+ use WordPress \Error \WordPressException ;
6+
57class WP_File_Reader extends WP_Byte_Reader {
68
79 const STATE_STREAMING = '#streaming ' ;
@@ -13,19 +15,14 @@ class WP_File_Reader extends WP_Byte_Reader {
1315 protected $ offset_in_file ;
1416 protected $ output_bytes = '' ;
1517 protected $ last_chunk_size = 0 ;
16- protected $ last_error ;
1718 protected $ state = self ::STATE_STREAMING ;
1819
1920 static public function create ( $ file_path , $ chunk_size = 8096 ) {
2021 if (!file_exists ($ file_path )) {
21- throw new \Exception (sprintf ( 'File %s does not exist ' , $ file_path ));
22- _doing_it_wrong ( __METHOD__ , sprintf ( 'File %s does not exist ' , $ file_path ), '1.0.0 ' );
23- return false ;
22+ throw new WordPressException (sprintf ( 'File %s does not exist ' , $ file_path ));
2423 }
2524 if (!is_file ($ file_path )) {
26- throw new \Exception (sprintf ( '%s is not a file ' , $ file_path ));
27- _doing_it_wrong ( __METHOD__ , sprintf ( '%s is not a file ' , $ file_path ), '1.0.0 ' );
28- return false ;
25+ throw new WordPressException (sprintf ( '%s is not a file ' , $ file_path ));
2926 }
3027 return new self ( $ file_path , $ chunk_size );
3128 }
@@ -48,8 +45,7 @@ public function tell(): int {
4845
4946 public function seek ( $ offset_in_file ): bool {
5047 if ( ! is_int ( $ offset_in_file ) ) {
51- _doing_it_wrong ( __METHOD__ , 'Cannot set a file reader cursor to a non-integer offset. ' , '1.0.0 ' );
52- return false ;
48+ throw new WordPressException ('Cannot set a file reader cursor to a non-integer offset. ' );
5349 }
5450 $ this ->offset_in_file = $ offset_in_file ;
5551 $ this ->last_chunk_size = 0 ;
@@ -64,11 +60,10 @@ public function seek( $offset_in_file ): bool {
6460
6561 public function close (): bool {
6662 if (!$ this ->file_pointer ) {
67- return false ;
63+ throw new WordPressException ( ' File pointer is not open ' ) ;
6864 }
6965 if (!fclose ($ this ->file_pointer )) {
70- $ this ->last_error = 'Failed to close file pointer ' ;
71- return false ;
66+ throw new WordPressException ('Failed to close file pointer ' );
7267 }
7368 $ this ->file_pointer = null ;
7469 $ this ->state = static ::STATE_FINISHED ;
@@ -83,18 +78,17 @@ public function get_bytes(): string {
8378 return $ this ->output_bytes ;
8479 }
8580
86- public function get_last_error (): ?string {
87- return $ this ->last_error ;
88- }
89-
9081 public function next_bytes (): bool {
9182 $ this ->output_bytes = '' ;
9283 $ this ->last_chunk_size = 0 ;
93- if ( $ this ->last_error || $ this -> is_finished () ) {
84+ if ( $ this ->is_finished () ) {
9485 return false ;
9586 }
9687 if ( ! $ this ->file_pointer ) {
9788 $ this ->file_pointer = fopen ( $ this ->file_path , 'r ' );
89+ if (false === $ this ->file_pointer ) {
90+ throw new WordPressException (sprintf ('Failed to open the file: %s ' , $ this ->file_path ));
91+ }
9892 if ( $ this ->offset_in_file ) {
9993 fseek ( $ this ->file_pointer , $ this ->offset_in_file );
10094 }
0 commit comments