FakeDataSeeder::pickGeneratedMembers()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.4285
cc 3
eloc 9
nc 3
nop 1
1
<?php
2
3
/**
4
 * Storgman - Student Organizations Management
5
 * Copyright (C) 2014-2015, Dejan Angelov <[email protected]>
6
 *
7
 * This file is part of Storgman.
8
 *
9
 * Storgman is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * Storgman is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with Storgman.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 * @package Storgman
23
 * @copyright Copyright (C) 2014-2015, Dejan Angelov <[email protected]>
24
 * @license https://github.com/angelov/storgman/blob/master/LICENSE
25
 * @author Dejan Angelov <[email protected]>
26
 */
27
28
use Angelov\Storgman\Documents\Document;
29
use Angelov\Storgman\Faculties\Faculty;
30
use Angelov\Storgman\Faculties\Repositories\FacultiesRepositoryInterface;
31
use Angelov\Storgman\Membership\Fee;
32
use Angelov\Storgman\Meetings\Meeting;
33
use Angelov\Storgman\Members\Member;
34
use Angelov\Storgman\Documents\Tags\Tag;
35
use Angelov\Storgman\Documents\Repositories\DocumentsRepositoryInterface;
36
use Angelov\Storgman\Meetings\Repositories\MeetingsRepositoryInterface;
37
use Angelov\Storgman\Documents\Tags\Repositories\TagsRepositoryInterface;
38
use Illuminate\Database\Seeder;
39
use Angelov\Storgman\Membership\Repositories\FeesRepositoryInterface;
40
use Angelov\Storgman\Members\Repositories\MembersRepositoryInterface;
41
42
class FakeDataSeeder extends Seeder
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
43
{
44
    protected $members;
45
    protected $fees;
46
    protected $meetings;
47
    protected $documents;
48
    protected $tags;
49
    protected $faker;
50
    protected $faculties;
51
    protected $generatedMembers;
52
    protected $generatedTags;
53
    protected $generatedFaculties;
54
55
    public function __construct(
56
        MembersRepositoryInterface $members,
57
        FeesRepositoryInterface $fees,
58
        MeetingsRepositoryInterface $meetings,
59
        DocumentsRepositoryInterface $documents,
60
        TagsRepositoryInterface $tags,
61
        FacultiesRepositoryInterface $faculties,
62
        Faker\Factory $fakerFactory
63
    ) {
64
        $this->members = $members;
65
        $this->fees = $fees;
66
        $this->meetings = $meetings;
67
        $this->faker = $fakerFactory::create();
68
        $this->generatedMembers = [];
69
        $this->gerneratedFaculties = [];
0 ignored issues
show
Bug introduced by
The property gerneratedFaculties does not seem to exist. Did you mean faculties?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
70
        $this->documents = $documents;
71
        $this->tags = $tags;
72
        $this->faculties = $faculties;
73
    }
74
75
    public function run()
76
    {
77
        $this->generateFaculties();
78
        $this->generateMembers(50);
79
        $this->generateFees();
80
        $this->generateMeetings(15);
81
        $this->generateTags(10);
82
        $this->generateDocuments(5);
83
    }
84
85
    private function generateFaculties()
86
    {
87
        $faculties = [
88
            "Fax 1" => "FAX1",
89
            "Fax 2" => "FAX2",
90
            "Fax 3" => "FAX3"
91
        ];
92
93
        foreach ($faculties as $title => $abbr) {
94
            $faculty = new Faculty();
95
            $faculty->setTitle($title);
96
            $faculty->setAbbreviation($abbr);
97
            $faculty->setUniversity('Example University');
98
99
            $this->faculties->store($faculty);
100
101
            $this->generatedFaculties[] = $faculty;
102
        }
103
    }
104
105
    private function generateMembers($count = 200)
106
    {
107
        print "Generating members started...\n";
108
109
        $fieldOfStudies = ["Computer Science", "Electrical Engineering", "Automation"];
110
        $birthYearFrom = "-27 years";
111
        $birthYearTo = "-19 years";
112
113
        for ($i = 0; $i <= $count; $i++) {
114
            $member = new Member();
115
116
            $member->setEmail($this->faker->email . rand(0, 9123));
117
            $member->setPassword(Hash::make('123456'));
118
            $member->setFirstName($this->faker->firstName);
119
            $member->setLastName($this->faker->lastName);
120
121
            $member->setFaculty($this->faker->randomElement($this->generatedFaculties));
0 ignored issues
show
Unused Code introduced by
The call to Generator::randomElement() has too many arguments starting with $this->generatedFaculties.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Documentation introduced by
$this->faker->randomElem...is->generatedFaculties) is of type string, but the function expects a object<Angelov\Storgman\Faculties\Faculty>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
122
123
            $member->setFieldOfStudy($this->faker->randomElement($fieldOfStudies));
0 ignored issues
show
Unused Code introduced by
The call to Generator::randomElement() has too many arguments starting with $fieldOfStudies.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
124
            $member->setYearOfGraduation($this->faker->numberBetween(2015, 2018));
0 ignored issues
show
Unused Code introduced by
The call to Generator::numberBetween() has too many arguments starting with 2015.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
125
126
            $member->setApproved($this->faker->boolean(90));
0 ignored issues
show
Unused Code introduced by
The call to Generator::boolean() has too many arguments starting with 90.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
127
128
            $social = strtolower($member->getFirstName() . $member->getLastName());
129
130
            if ($this->faker->boolean(60)) {
0 ignored issues
show
Unused Code introduced by
The call to Generator::boolean() has too many arguments starting with 60.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
131
                $member->setFacebook($social);
132
            }
133
134
            if ($this->faker->boolean(60)) {
0 ignored issues
show
Unused Code introduced by
The call to Generator::boolean() has too many arguments starting with 60.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
135
                $member->setTwitter($social);
136
            }
137
138
            if ($this->faker->boolean(60)) {
0 ignored issues
show
Unused Code introduced by
The call to Generator::boolean() has too many arguments starting with 60.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
139
                $member->setGooglePlus($social);
140
            }
141
142
            $member->setPhoneNumber($this->faker->phoneNumber);
143
            $member->setWebsite("http://". $social .".com");
144
145
            $birthday = $this->faker->dateTimeBetween($birthYearFrom, $birthYearTo);
0 ignored issues
show
Unused Code introduced by
The call to Generator::dateTimeBetween() has too many arguments starting with $birthYearFrom.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
146
147
            $member->setBirthday($birthday);
148
            $member->setBoardMember($this->faker->boolean(20));
0 ignored issues
show
Unused Code introduced by
The call to Generator::boolean() has too many arguments starting with 20.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
149
150
            $this->generatedMembers[] = $member;
151
152
            $this->members->store($member);
153
        }
154
155
        print "Generated ". $count ." members.\n";
156
    }
157
158
    private function hasGeneratedMembers()
159
    {
160
        return (count($this->generatedMembers) > 0);
161
    }
162
163
    private function generateFees()
164
    {
165
        if (!$this->hasGeneratedMembers()) {
166
            return;
167
        }
168
169
        print "Generating fees started...\n";
170
        $fees = 0;
171
172
        foreach ($this->generatedMembers as $member) {
173
            $from = $this->faker->dateTimeBetween('-10 years', '-1 year');
0 ignored issues
show
Unused Code introduced by
The call to Generator::dateTimeBetween() has too many arguments starting with '-10 years'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
174
175
            for ($i = 0; $i < 3; $i++) {
176
                $fee = new Fee();
177
178
                $fee->setFromDate($from);
179
180
                $to = clone $from;
181
                $to->modify('+1 year');
182
                $fee->setToDate($to);
183
184
                $fee->setMember($member);
185
186
                $this->fees->store($fee);
187
                $fees++;
188
189
                $now = new \DateTime('now');
190
191
                if ($to > $now) {
192
                    break;
193
                }
194
195
                $from->modify('+1 day');
196
                $from->modify('+1 year');
197
            }
198
        }
199
200
        print "Generated ". $fees ." fees.\n";
201
    }
202
203
    private function generateMeetings($count = 50)
204
    {
205
        if (!$this->hasGeneratedMembers()) {
206
            return;
207
        }
208
209
        print "Generating meetings started...\n";
210
        $attendings = 0;
211
212
        // Calculate the date for the first meeting..
213
        // There will be one meeting per week.
214
        $ago = $count * 7;
215
        $date = new \DateTime('-' . $ago . ' days');
216
217
        for ($i = 0; $i < $count; $i++) {
218
            $meeting = new Meeting();
219
220
            $meeting->setDate($date);
221
            $meeting->setLocation($this->faker->streetAddress);
222
            $meeting->setInfo("<p>This is fake meeting report with randomly generated information.</p>");
223
224
            $creator = $this->generatedMembers[0];
225
            $attendants = $this->pickGeneratedMembers($this->calculateNeededAttendants());
226
            $attendings += count($attendants);
227
228
            $meeting->setCreator($creator);
229
            $meeting->addAttendants($attendants);
230
231
            $this->meetings->store($meeting);
232
233
            $date->modify('+1 week');
234
        }
235
236
        print "Generated ". $count ." meetings with ". $attendings ." total attendings\n";
237
    }
238
239
    private function pickGeneratedMembers($count = 0)
240
    {
241
        $pickedIndexes = [];
242
        $pickedMembers = [];
243
244
        for ($i = 0; $i < $count; $i++) {
245
            $index = $this->faker->numberBetween(0, count($this->generatedMembers) - 1);
0 ignored issues
show
Unused Code introduced by
The call to Generator::numberBetween() has too many arguments starting with 0.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
246
247
            if (!in_array($index, $pickedIndexes)) {
248
                $pickedIndexes[] = $index;
249
                $pickedMembers[] = $this->generatedMembers[$index];
250
            }
251
        }
252
253
        return $pickedMembers;
254
    }
255
256
    private function pickGeneratedTags($count = 0)
257
    {
258
        $tags = [];
259
260
        for ($i=0; $i < $count; $i++) {
261
            $tags[] = $this->generatedTags[array_rand($this->generatedTags)];
262
        }
263
264
        return $tags;
265
    }
266
267
    private function calculateNeededAttendants()
268
    {
269
        $count = count($this->generatedMembers);
270
271
        if ($count == 0) {
272
            return 0;
273
        }
274
275
        if ($count < 50) {
276
            return $this->faker->numberBetween(1, $count);
0 ignored issues
show
Unused Code introduced by
The call to Generator::numberBetween() has too many arguments starting with 1.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
277
        }
278
279
        if ($count < 100) {
280
            return $this->faker->numberBetween(50, $count);
0 ignored issues
show
Unused Code introduced by
The call to Generator::numberBetween() has too many arguments starting with 50.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
281
        }
282
283
        return $this->faker->numberBetween(50, 100);
0 ignored issues
show
Unused Code introduced by
The call to Generator::numberBetween() has too many arguments starting with 50.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
284
    }
285
286
    public function generateDocuments($count = 100)
287
    {
288
        if (!$this->hasGeneratedMembers()) {
289
            return;
290
        }
291
292
        print "Generating documents started...\n";
293
294
        $countOpenings = 0;
295
        $countOpeners = 0;
296
297
        for ($i = 0; $i < $count; $i++) {
298
            $document = new Document();
299
300
            $title = $this->faker->sentence(rand(6, 15));
0 ignored issues
show
Unused Code introduced by
The call to Generator::sentence() has too many arguments starting with rand(6, 15).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
301
            $document->setTitle($title);
302
            $document->setDescription($this->faker->realText());
303
            $document->setUrl("http://storgman.local");
304
305
            $submitter = $this->generatedMembers[rand(0, count($this->generatedMembers) - 1)];
306
307
            $document->setSubmitter($submitter);
308
309
            $openers = $this->pickGeneratedMembers(rand(0, count($this->generatedMembers) - 1));
310
311
            foreach ($openers as $opener) {
312
                for ($j=0; $j < rand(1, 10); $j++) {
313
                    $document->addOpener($opener);
314
                    $countOpenings++;
315
                }
316
317
                $countOpeners++;
318
            }
319
320
            $tags = $this->pickGeneratedTags(rand(0, 5));
321
322
            foreach ($tags as $tag) {
323
                $document->addTag($tag);
324
            }
325
326
            $this->documents->store($document);
327
        }
328
329
        printf("Generated %d documents with total %d openings\n", $count, $countOpenings);
330
    }
331
332
    public function generateTags($count = 10)
333
    {
334
        print "Generating documents' tags started...\n";
335
336
        for ($i=0; $i < $count; $i++) {
337
            $tag = new Tag();
338
            $tag->setName($this->faker->domainWord);
339
            $this->tags->store($tag);
340
341
            $this->generatedTags[] = $tag;
342
        }
343
344
        print "Generated ". $count ." tags.\n";
345
    }
346
}
347