Completed
Push — php7.2-travis ( 83e6ee...46fda5 )
by
unknown
265:12 queued 251:40
created

ar_store_files::exists()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.1576

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 4
nop 2
dl 0
loc 12
ccs 7
cts 12
cp 0.5833
crap 5.1576
rs 9.2
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "this should be more efficient then coping the whole contents of the file in memory"
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "nlsstrings"
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "create an more efficient way for copying with a stream_copy_to_stream call or wrapping t in an CopyFrom/CopyTo api"
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "nlsstrings"
Loading history...
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