Completed
Push — php7.2-travis ( caaf61...ae643e )
by
unknown
06:33
created

ar_store_files::getStore()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1.0156

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 4
cp 0.75
crap 1.0156
1
<?php
2
3 1
	ar_pinp::allow( 'ar_store_files', array( 'ls', 'get', 'save', 'delete', 'touch', 'exists' ) );
4
5
	/*
6
	 * prevent mess detector from warning for the private static fields
7
	 * @SuppressWarnings(PHPMD.UnusedPrivateField)
8
	 */
9
	class ar_store_files extends arBase {
10
11 15
		protected static function parseName( $fname ) {
12 15
			list( $nls, $name ) = explode('_', $fname, 2);
13
			return array(
14 15
				'nls' => $nls,
15
				'name' => $name
16 15
			);
17
		}
18
19 15
		protected static function compileName( $name, $nls ) {
20 15
			if ( !$nls ) {
21 1
				$ob = ar::context()->getObject();
22 1
				$nls = $ob->nls;
23 1
			}
24 15
			return $nls.'_'.$name;
25
		}
26
27 16
		protected static function getStore() {
28 16
			$ob = ar::context()->getObject();
29 16
			return array( $ob, $ob->store->get_filestore("files") );
30
		}
31
32 2
		public static function ls( $nls=null ) {
33 2
			list($ob, $fstore) = static::getStore();
34 2
			$files = $fstore->ls($ob->id);
35 2
			if ( !$files ) {
36 1
				$files = array();
37 1
			}
38 2
			$files = array_map( array('self','parseName'), $files );
39 2
			if ( isset($nls) ) {
40
				$files = array_filter( $files, function($f) use($nls) {
41
					return ( $f['nls'] == $nls );
42
				} );
43
			}
44 2
			return $files;
45
		}
46
47 14
		public static function get( $name, $nls=null ) {
48 14
			$info = static::exists($name, $nls);
49 14
			if ( !$info ) {
50
				return ar_error::raiseError('File not found: '.$name.' - '.$nls, 404);
51
			}
52 14
			list( $ob, $fstore ) = static::getStore();
53 14
			$fname = static::compileName($info['name'], $info['nls']);
54 14
			$stream = $fstore->get_stream($ob->id, $fname);
55 14
			return new ar_content_filesFile( $stream );
56
		}
57
58 5
		public static function save( $name, $contents, $nls=null ) {
59 5
			list( $ob, $fstore ) = static::getStore();
60 5
			$fname = static::compileName($name, $nls);
61 5
			if( $contents instanceof ar_content_filesFile ){
62
				// FIXME: this should be more efficient then coping the whole contents of the file in memory
63
				// should be fixed with a copyFrom/copyTo function call on ar_content_filesFile
64
				$contents = $contents->getContents();
65
			}
66 5
			if ( is_resource($contents) && get_resource_type($contents)==='stream' ) {
67
				return $fstore->copy_stream_to_store( $contents, $ob->id, $fname );
68
			} else {
69 5
				return $fstore->write( (string) $contents, $ob->id, $fname );
70
			}
71
		}
72
73 1
		public static function delete( $name, $nls=null ) {
74 1
			list( $ob, $fstore ) = static::getStore();
75 1
			$fname = static::compileName($name, $nls);
76 1
			if ( $fstore->exists( $ob->id, $fname ) ) {
77 1
				return $fstore->remove( $ob->id, $fname );
78
			}
79
			return false;
80
		}
81
82
		public static function touch( $name, $time=null, $nls=null ) {
83
			list( $ob, $fstore ) = static::getStore();
84
			$fname = static::compileName($name, $nls);
85
			return $fstore->touch( $ob->id, $fname, $time );
86
		}
87
88 3
		public static function temp( $contents=null ) {
89 3
			list( $ob, $fstore ) = static::getStore();
0 ignored issues
show
Unused Code introduced by
The assignment to $fstore is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
90 3
			$tmpsrc = tempnam($ob->store->get_config("files")."temp", "tmp");
91 3
			if ( !$tmpsrc ) {
92
				// FIXME: nlsstrings
93
				return ar_error::raiseError( 'Could not create temp file', 501 );
94
			}
95 3
			$fp = fopen( $tmpsrc, 'w+' );
96 3
			if (is_resource($fp)) {
97 3
				$res = new ar_content_filesFile($fp);
98 3
				if( $contents instanceof ar_content_filesFile ){
99
					// FIXME: create an more efficient way for copying with a stream_copy_to_stream call or wrapping t in an CopyFrom/CopyTo api
100
					// or even an temp function on a ar_content_filesFile object which creates an temp file from the ar_content_filesFile
101 3
					$contents = $contents->getContents();
102 3
					fwrite($fp, $contents);
103 3
					rewind($fp);
104 3
				} else if ( is_resource($contents) && get_resource_type($contents)==='stream' ) {
105
					stream_copy_to_stream( $contents, $fp );
106
					rewind($fp);
107
				} else if (isset($contents)) {
108
					fwrite($fp, $contents);
109
					rewind($fp);
110
				}
111 3
				return $res;
112
			} else {
113
				// FIXME: nlsstrings
114
				return ar_error::raiseError('Could not create temp file', 502);
115
			}
116
		}
117
118 14
		public static function exists( $name, $nls=null ) {
119 14
			list( $ob, $fstore ) = static::getStore();
120 14
			$fname = static::compileName($name, $nls);
121 14
			if ( !$fstore->exists($ob->id, $fname) && !isset($nls) ) {
122
				$nls = $ob->data->nls->default;
123
				$fname = static::compileName($name, $nls);
124
			}
125 14
			if ( !$fstore->exists($ob->id, $fname ) ) {
126 6
				return false;
127
			}
128 14
			return static::parseName($fname);
129
		}
130
131
	}
132