Passed
Push — 5.x ( 71aac6...923942 )
by Jeroen
13:50 queued 13s
created

Seed::getDefaultLimit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Elgg\Database\Seeds;
4
5
use Elgg\Traits\Cli\Progressing;
6
use Elgg\Traits\Seeding;
7
8
/**
9
 * Abstract seed
10
 *
11
 * Plugins should extend this class to create their own seeders,
12
 * add use 'seeds','database' event to add their seed to the sequence.
13
 */
14
abstract class Seed implements Seedable {
15
16
	use Seeding;
17
	use Progressing {
18
		advance as private progressAdvance;
19
	}
20
	
21
	/**
22
	 * @var int Max number of items to be created by the seed
23
	 */
24
	protected int $limit;
25
26
	/**
27
	 * @var bool Create new entities
28
	 */
29
	protected bool $create = false;
30
	
31
	/**
32
	 * @var int Number of seeded entities
33
	 */
34
	protected int $seeded_counter = 0;
35
	
36
	/**
37
	 * Seed constructor.
38
	 *
39
	 * @param array $options seeding options
40
	 *                       - limit: Number of item to seed
41
	 *                       - create: create new entities (default: false)
42
	 *                       - create_since: lower bound creation time (default: now)
43
	 *                       - create_until: upper bound creation time (default: now)
44
	 */
45 3
	public function __construct(array $options = []) {
46 3
		$limit = (int) elgg_extract('limit', $options);
47 3
		if ($limit > 0) {
48 1
			$this->limit = $limit;
49
		} else {
50 2
			$this->limit = static::getDefaultLimit();
51
		}
52
		
53 3
		$this->create = (bool) elgg_extract('create', $options, $this->create);
54 3
		$this->setCreateSince(elgg_extract('create_since', $options, 'now'));
55 3
		$this->setCreateUntil(elgg_extract('create_until', $options, 'now'));
56
	}
57
	
58
	/**
59
	 * Register this class for seeding
60
	 *
61
	 * @param \Elgg\Event $event 'seeds', 'database'
62
	 *
63
	 * @return array
64
	 */
65
	final public static function register(\Elgg\Event $event) {
66
		$seeds = $event->getValue();
67
		
68
		$seeds[] = static::class;
69
		
70
		return $seeds;
71
	}
72
	
73
	/**
74
	 * Get the count of the seeded entities
75
	 *
76
	 * @return int
77
	 */
78 2
	final public function getCount(): int {
79 2
		if ($this->create) {
80
			return $this->seeded_counter;
81
		}
82
		
83 2
		$defaults = [
84 2
			'metadata_names' => '__faker',
85 2
		];
86 2
		$options = array_merge($defaults, $this->getCountOptions());
87
		
88 2
		return elgg_count_entities($options);
89
	}
90
	
91
	/**
92
	 * Advance progressbar
93
	 *
94
	 * @param int $step Step
95
	 *
96
	 * @return void
97
	 */
98
	public function advance(int $step = 1): void {
99
		$this->seeded_counter += $step;
100
		
101
		$this->progressAdvance($step);
102
	}
103
	
104
	/**
105
	 * Get the default number of content to seed
106
	 *
107
	 * @return int
108
	 */
109 2
	public static function getDefaultLimit(): int {
110 2
		return max(elgg_get_config('default_limit'), 20);
111
	}
112
113
	/**
114
	 * Populate database
115
	 *
116
	 * @return mixed
117
	 */
118
	abstract public function seed();
119
120
	/**
121
	 * Removed seeded rows from database
122
	 *
123
	 * @return mixed
124
	 */
125
	abstract public function unseed();
126
127
	/**
128
	 * Get the (un)seeding type of this handler
129
	 *
130
	 * @return string
131
	 */
132
	abstract public static function getType(): string;
133
	
134
	/**
135
	 * Get options for elgg_count_entities()
136
	 *
137
	 * @return array
138
	 * @see self::getCount()
139
	 */
140
	abstract protected function getCountOptions(): array;
141
}
142