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

mod/blog/classes/Elgg/Blog/Seeder.php (1 issue)

1
<?php
2
3
namespace Elgg\Blog;
4
5
use Elgg\Database\Seeds\Seed;
6
7
/**
8
 * Add blog seed
9
 *
10
 * @access private
11
 */
12
class Seeder extends Seed {
13
14
	private $status = [
15
		'unsaved_draft',
16
		'draft',
17
		'published',
18
	];
19
20
	/**
21
	 * {@inheritdoc}
22
	 */
23
	public function seed() {
24
25
		$count_blogs = function () {
26
			return elgg_get_entities([
27
				'types' => 'object',
28
				'subtypes' => 'blog',
29
				'metadata_names' => '__faker',
30
				'count' => true,
31
			]);
32
		};
33
34
		while ($count_blogs() < $this->limit) {
35
			$metadata = [
36
				'status' => $this->getRandomStatus(),
37
				'comments_on' => $this->faker()->boolean() ? 'On' : 'Off',
38
				'excerpt' => $this->faker()->sentence(),
39
			];
40
41
			$attributes = [
42
				'subtype' => 'blog',
43
			];
44
45
			$blog = $this->createObject($attributes, $metadata);
46
47
			if (!$blog) {
48
				continue;
49
			}
50
51
			$this->createComments($blog);
52
			$this->createLikes($blog);
53
54
			if ($blog->status == 'unsaved_draft' || $blog->status == 'draft') {
55
				$blog->future_access = $blog->access_id;
56
				$blog->access_id = ACCESS_PRIVATE;
57
			}
58
59
			if ($blog->status == 'published') {
60
				elgg_create_river_item([
61
					'view' => 'river/object/blog/create',
62
					'action_type' => 'create',
63
					'subject_guid' => $blog->owner_guid,
64
					'object_guid' => $blog->guid,
65
					'target_guid' => $blog->container_guid,
66
				]);
67
68
				elgg_trigger_event('publish', 'object', $blog);
69
			}
70
71
			if ($this->faker()->boolean()) {
72
				$blog->annotate('blog_auto_save', $this->faker()->text(500), ACCESS_PRIVATE, $blog->owner_guid);
73
			}
74
75
			if ($this->faker()->boolean() && $blog->status != 'unsaved_draft') {
76
				$blog->annotate('blog_revision', $blog->description, ACCESS_PRIVATE, $blog->owner_guid);
77
				$blog->description = $this->faker()->text(500);
78
			}
79
80
			$blog->save();
81
		}
82
	}
83
84
	/**
85
	 * {@inheritdoc}
86
	 */
87
	public function unseed() {
88
89
		$blogs = elgg_get_entities([
90
			'types' => 'object',
91
			'subtypes' => 'blog',
92
			'metadata_names' => '__faker',
93
			'limit' => 0,
94
			'batch' => true,
95
		]);
96
97
		/* @var $blogs \ElggBatch */
98
99
		$blogs->setIncrementOffset(false);
100
101
		foreach ($blogs as $blog) {
102
			if ($this->delete($blog)) {
0 ignored issues
show
The method delete() does not exist on Elgg\Blog\Seeder. ( Ignorable by Annotation )

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

102
			if ($this->/** @scrutinizer ignore-call */ delete($blog)) {

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...
103
				$this->log("Deleted blog $blog->guid");
104
			} else {
105
				$this->log("Failed to delete blog $blog->guid");
106
			}
107
		}
108
	}
109
110
	/**
111
	 * Returns random blog status
112
	 * @return string
113
	 */
114
	public function getRandomStatus() {
115
		$key = array_rand($this->status, 1);
116
117
		return $this->status[$key];
118
	}
119
}
120