Newer
Older

Fran Garcia-Linares
committed
<?php

Fran Garcia-Linares
committed
namespace Drupal\project_browser_devel\Plugin\ProjectBrowserSource;

Fran Garcia-Linares
committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use Drupal\Component\Utility\Random;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\project_browser\DrupalOrg\DrupalOrgReleases;
use Drupal\project_browser\Plugin\ProjectBrowserSourceInterface;
use Drupal\project_browser\ProjectBrowser\Project;
use Drupal\project_browser\ProjectBrowser\ProjectsResultsPage;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Random data plugin. Used mostly for testing.
*
* To enable this source:
* - `drush config:set project_browser.admin_settings enabled_source random_data`
*
* @ProjectBrowserSource(
* id = "random_data",
* label = @Translation("Random data"),
* description = @Translation("Gets random project and release information"),
* )
*/
class RandomDataPlugin extends PluginBase implements ProjectBrowserSourceInterface, ContainerFactoryPluginInterface {
/**
* Utility to create random data.
*
* @var \Drupal\Component\Utility\Random
*/
protected $randomGenerator;
/**
* Constructs a MockDrupalDotOrg object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->randomGenerator = new Random();
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition
);
}
/**
* Generate random IDs and labels.
*
* @param int $array_length
* Length of the array to generate.
*
* @return array
* Array of random IDs and names.
*/
protected function getRandomIdsAndNames($array_length = 4): array {
$data = [];
for ($i = 0; $i < $array_length; $i++) {
$data[] = [
'id' => uniqid(),
'name' => $this->randomGenerator->word(rand(6, 10)),
];
}
return $data;
}
/**
* Returns a random date.
*
* @return int
* Random timestamp.
*/
protected function getRandomDate() {
return rand(strtotime('2 years ago'), strtotime('today'));
}
/**
* {@inheritdoc}
*/
public function getDevelopmentStatuses(): array {
return $this->getRandomIdsAndNames();
}
/**
* {@inheritdoc}
*/
public function getMaintenanceStatuses(): array {
$data = $this->getRandomIdsAndNames(6);
// To test a special case in the front end.
$data[] = [
'id' => uniqid(),
'name' => 'Actively maintained',
];
return $data;
}
/**
* {@inheritdoc}
*/
public function getSecurityCoverages(): array {
$data = $this->getRandomIdsAndNames(2);
// To test a special case in the front end.
$data[] = [
'id' => 'covered',
'name' => 'Covered',
];
return $data;
}
/**
* {@inheritdoc}
*/
public function getCategories(): array {
return $this->getRandomIdsAndNames(20);
}
/**
* {@inheritdoc}
*/
public function getProjectReleases(string $project): DrupalOrgReleases {
$releases = [];
$number_of_releases = rand(1, 10);
for ($i = 1; $i < $number_of_releases; $i++) {
$releases[] = [
'name' => $project . ' ' . $this->randomGenerator->word(8),
'version' => $i . '.x',
'tag' => $this->randomGenerator->word(4),
'release_link' => '',
'download_link' => '',
'date' => $this->getRandomDate(),
'files' => [],
'terms' => [],
'security' => '',
'core_compatibility' => rand(6, 10),
];
}
return new DrupalOrgReleases($releases);
}
/**
* {@inheritdoc}
*/
public function getProjects(array $query = []) : ProjectsResultsPage {
$projects = [];

Fran Garcia-Linares
committed
$number_of_projects = rand(12, 36);

Fran Garcia-Linares
committed
$categories = $this->getCategories();
$security_values = $this->getSecurityCoverages();
$maintenance_values = $this->getMaintenanceStatuses();

Fran Garcia-Linares
committed
$broken_image = 'https://image.not/found' . uniqid() . '.jpg';
$good_image = 'https://picsum.photos/600/400';

Fran Garcia-Linares
committed
for ($i = 0; $i < $number_of_projects; $i++) {
$machine_name = strtolower($this->randomGenerator->word(10));
$category = array_rand($categories);
$security = array_rand($security_values);
$maintenance = array_rand($maintenance_values);
$project = [
'author' => [
'name' => $this->randomGenerator->word(10),
],
'created' => $this->getRandomDate(),
'changed' => $this->getRandomDate(),
'status' => rand(0, 1),
'releases' => $this->getProjectReleases($machine_name),
'title' => ucwords($machine_name),
'nid' => uniqid(),
'body' => [
'summary' => $this->randomGenerator->paragraphs(1),
],
'field_project_images' => [
[
'file' => [

Fran Garcia-Linares
committed
'uri' => ($i % 3) ? $good_image : $broken_image,

Fran Garcia-Linares
committed
'resource' => 'image',
],
'alt' => $machine_name . ' logo',
],
],
'field_maintenance_status' => $maintenance_values[$maintenance],
'field_module_categories' => [$categories[$category]],
'field_security_advisory_coverage' => $security_values[$security]['id'],
'field_project_machine_name' => $machine_name,
'project_usage' => [],
'project_usage_total' => rand(0, 100000),
'flag_project_star_user_count' => rand(0, 100),
];
$projects[] = new Project($project);
}
return new ProjectsResultsPage(count($projects), $projects);
}
}