Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

Seeder::seed()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 29
nc 4
nop 0
dl 0
loc 48
ccs 0
cts 34
cp 0
crap 20
rs 8.7396
c 0
b 0
f 0
1
<?php
2
3
namespace Elgg\File;
4
5
use Elgg\Database\Seeds\Seed;
6
use Elgg\Project\Paths;
7
8
/**
9
 * Add file seed
10
 *
11
 * @access private
12
 */
13
class Seeder extends Seed {
14
15
	/**
16
	 * {@inheritdoc}
17
	 */
18
	public function seed() {
19
20
		$count_files = function () {
21
			return elgg_get_entities([
22
				'types' => 'object',
23
				'subtypes' => 'file',
24
				'metadata_names' => '__faker',
25
				'count' => true,
26
			]);
27
		};
28
29
		while ($count_files() < $this->limit) {
30
			$path = $this->faker()->image();
31
32
			$filename = pathinfo($path, PATHINFO_FILENAME);
33
34
			$attributes = [
35
				'subtype' => 'file',
36
			];
37
38
			$file = $this->createObject($attributes, [], ['save' => false]);
39
			/* @var $file \ElggFile */
40
41
			if (!$file) {
42
				continue;
43
			}
44
45
			$file->setFilename("file/$filename");
46
			$file->open('write');
47
			$file->close();
48
49
			copy($path, $file->getFilenameOnFilestore());
50
51
			if (!$file->save()) {
52
				$file->delete();
53
				continue;
54
			}
55
56
			$file->saveIconFromElggFile($file);
57
58
			$this->createComments($file);
59
			$this->createLikes($file);
60
61
			elgg_create_river_item([
62
				'action_type' => 'create',
63
				'subject_guid' => $file->owner_guid,
64
				'object_guid' => $file->guid,
65
				'target_guid' => $file->container_guid,
66
			]);
67
		}
68
	}
69
70
	/**
71
	 * {@inheritdoc}
72
	 */
73
	public function unseed() {
74
75
		$files = elgg_get_entities([
76
			'types' => 'object',
77
			'subtypes' => 'file',
78
			'metadata_names' => '__faker',
79
			'limit' => 0,
80
			'batch' => true,
81
		]);
82
83
		/* @var $files \ElggBatch */
84
85
		$files->setIncrementOffset(false);
86
87
		foreach ($files as $file) {
88
			if ($this->delete($file)) {
0 ignored issues
show
Bug introduced by Ismayil Khayredinov
The method delete() does not exist on Elgg\File\Seeder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

88
			if ($this->/** @scrutinizer ignore-call */ delete($file)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
				$this->log("Deleted file $file->guid");
90
			} else {
91
				$this->log("Failed to delete file $file->guid");
92
			}
93
		}
94
	}
95
96
}
97