Issues (590)

bench/_files/BenchData.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Bdf\Prime\Bench;
4
5
use Bdf\Prime\Entity\Model;
6
use Bdf\Prime\ServiceLocator;
7
8
require_once 'bench.php';
9
10
/**
11
 * Permet d'initialiser la base de données
12
 *
13
 * @package Extensions
14
 */
15
class BenchData
16
{
17
    /**
18
     * @var ServiceLocator
19
     */
20
    protected $prime;
21
22
    /**
23
     * @var Model[]
24
     */
25
    protected $data = [];
26
27
    /**
28
     * BenchData constructor.
29
     * @param ServiceLocator $prime
30
     */
31
    public function __construct(ServiceLocator $prime)
32
    {
33
        $this->prime = $prime;
34
    }
35
36
    /**
37
     * Get the entity
38
     *
39
     * @param string $key
40
     *
41
     * @return Model
42
     */
43
    public function get($key)
44
    {
45
        return $this->data[$key];
46
    }
47
48
    /**
49
     * @param array $entityClassNames
50
     */
51
    public function register(array $entityClassNames)
52
    {
53
        foreach ($entityClassNames as $className) {
54
            $this->buildSchema($className);
55
        }
56
    }
57
58
    /**
59
     * Ajoute toutes les données commununes aux tests
60
     */
61
    public function addAllData()
62
    {
63
        $this->register([
64
            User::class, Pack::class, Customer::class, CustomerPack::class
65
        ]);
66
67
        $this->addPacks();
68
        $this->addComfortUsers();
69
        $this->addCommonUser();
70
        $this->addCommunityUsers();
71
        $this->addHistoricConfortUsers();
72
        $this->addReferencingLoaderUsers();
73
        $this->addReferencingLimitedUsers();
74
        $this->addReferencingUsers();
75
        $this->addSimplyUsers();
76
        $this->addSpotUsers();
77
    }
78
79
    public function clear()
80
    {
81
        foreach ($this->data as $entity) {
82
            $this->prime->repository($entity)->delete($entity);
83
        }
84
85
        $this->data = [];
86
    }
87
88
    /**
89
     * @param $entityClassName
90
     */
91
    protected function buildSchema($entityClassName)
92
    {
93
        $this->prime->repository($entityClassName)->schema(true)->migrate();
94
    }
95
96
    /**
97
     * Ajoute uniquement les customers dans le test pack
98
     */
99
    protected function addCustomers()
100
    {
101
        $this->addSimplyCustomer();
102
        $this->addComfortCustomer();
103
        $this->addSpotCustomer();
104
        $this->addReferencingLoaderCustomer();
105
        $this->addReferencingCustomer();
106
        $this->addCommunityCustomer();
107
        $this->addSpotUsers();
108
        $this->addReferencingLimitedCustomer();
109
        $this->addHistoricConfortCustomer();
110
    }
111
112
    /**
113
     * Ajoute le customer avec tous les packs
114
     */
115
    protected function addCommonCustomer()
116
    {
117
        $this->push([
118
            'CUSTOMER_COMMON' => new Customer([
119
                'id'                => '330000',
120
                'name'              => 'customer Common name',
121
            ]),
122
123
            'PACK_CUSTOMER_COMMON_2'  => new CustomerPack(['customerId' => '330000', 'packId' => 2, 'options' => ['autoAnswerNetworkRequest' => false, 'autoAnswerDocumentRequest' => false]]),
124
            'PACK_CUSTOMER_COMMON_3'  => new CustomerPack(['customerId' => '330000', 'packId' => 3]),
125
            'PACK_CUSTOMER_COMMON_7'  => new CustomerPack(['customerId' => '330000', 'packId' => 7]),
126
            'PACK_CUSTOMER_COMMON_5'  => new CustomerPack(['customerId' => '330000', 'packId' => 5]),
127
            'PACK_CUSTOMER_COMMON_15' => new CustomerPack(['customerId' => '330000', 'packId' => 15]),
128
        ]);
129
    }
130
131
    /**
132
     * Ajoute le customer et les users avec tous les packs
133
     */
134
    protected function addCommonUser()
135
    {
136
        $this->addCommonCustomer();
137
138
        $this->push([
139
            'USER_COMMON' => new User([
140
                'id'                => '22000001',
141
                'name'              => 'user Common name',
142
                'roles'             => [1 => UserRole::ADMINISTRATIVE_MANAGER, 2 => UserRole::PROCUREMENT_MANAGER, 3 => UserRole::CHARTERER, 5 => UserRole::MULTISITE_MANAGER],
143
                'customer'          => $this->get('CUSTOMER_COMMON')
144
            ])
145
        ]);
146
    }
147
148
    /**
149
     * Ajoute le customer avec le pack simply
150
     */
151
    protected function addSimplyCustomer()
152
    {
153
        $this->push([
154
            'CUSTOMER_SIMPLY' => new Customer([
155
                'id'                => '331111',
156
                'name'              => 'customer Simply name',
157
            ]),
158
            'PACK_CUSTOMER_SIMPLY' => new CustomerPack(['customerId' => '331111', 'packId' => 1]),
159
        ]);
160
    }
161
162
    /**
163
     * Ajoute le customer et les users avec le pack simply
164
     */
165
    protected function addSimplyUsers()
166
    {
167
        $this->addSimplyCustomer();
168
169
        $this->push([
170
            'USER_EXPLOIT_SIMPLY' => new User([
171
                'id'                => '22111101',
172
                'name'              => 'user Exploit Simply name',
173
                'roles'             => [1 => UserRole::CARRIER],
174
                'customer'          => $this->get('CUSTOMER_SIMPLY'),
175
            ]),
176
            'USER_OFFICE_MANAGER_SIMPLY' => new User([
177
                'id'                => '22111102',
178
                'name'              => 'user Office Manager Simply name',
179
                'roles'             => [1 => UserRole::ADMINISTRATIVE_MANAGER],
180
                'customer'          => $this->get('CUSTOMER_SIMPLY'),
181
            ])
182
        ]);
183
    }
184
185
    /**
186
     * Ajoute le customer avec le pack confort
187
     */
188
    protected function addComfortCustomer()
189
    {
190
        $this->push([
191
            'CUSTOMER_CONFORT' => new Customer([
192
                'id'                => '332222',
193
                'name'              => 'customer Confort name',
194
            ]),
195
            'PACK_CUSTOMER_CONFORT' => new CustomerPack(['customerId' => '332222', 'packId' => 2]),
196
        ]);
197
    }
198
199
    /**
200
     * Ajoute le customer et les users avec le pack confort
201
     */
202
    protected function addComfortUsers()
203
    {
204
        $this->addComfortCustomer();
205
206
        $this->push([
207
            'USER_EXPLOIT_CONFORT' => new User([
208
                'id'                => '22222201',
209
                'name'              => 'user Exploit Confort name',
210
                'roles'             => [1 => UserRole::CARRIER],
211
                'customer'          => $this->get('CUSTOMER_CONFORT'),
212
            ]),
213
            'USER_OFFICE_MANAGER_CONFORT' => new User([
214
                'id'                => '22222202',
215
                'name'              => 'user Office Manager Confort name',
216
                'roles'             => [1 => UserRole::ADMINISTRATIVE_MANAGER],
217
                'customer'          => $this->get('CUSTOMER_CONFORT'),
218
            ])
219
        ]);
220
    }
221
222
    /**
223
     * Ajoute le customer avec le pack spot
224
     */
225
    protected function addSpotCustomer()
226
    {
227
        $this->push([
228
            'CUSTOMER_SPOT' => new Customer([
229
                'id'                => '333333',
230
                'name'              => 'customer Spot name',
231
            ]),
232
            'PACK_CUSTOMER_SPOT' => new CustomerPack(['customerId' => '333333', 'packId' => 3]),
233
        ]);
234
    }
235
236
    /**
237
     * Ajoute le customer et les users avec le pack spot
238
     */
239
    protected function addSpotUsers()
240
    {
241
        $this->addSpotCustomer();
242
243
        $this->push([
244
            'USER_CHARTERER_SPOT' => new User([
245
                'id'                => '22333301',
246
                'name'              => 'user Charterer Spot name',
247
                'roles'             => [1 => UserRole::CHARTERER],
248
                'customer'          => $this->get('CUSTOMER_SPOT'),
249
            ]),
250
            'USER_PURCHASING_MANAGER_SPOT' => new User([
251
                'id'                => '22333302',
252
                'name'              => 'user Purchasing Manager Spot name',
253
                'roles'             => [1 => UserRole::PROCUREMENT_MANAGER],
254
                'customer'          => $this->get('CUSTOMER_SPOT'),
255
            ])
256
        ]);
257
    }
258
259
    /**
260
     * Ajoute le customer avec le pack référencement sans limite
261
     */
262
    protected function addReferencingCustomer()
263
    {
264
        $this->push([
265
            'CUSTOMER_REFERENCING' => new Customer([
266
                'id'                => '334444',
267
                'name'              => 'customer Referencing name',
268
            ]),
269
            'PACK_CUSTOMER_REFERENCING' => new CustomerPack(['customerId' => '334444', 'packId' => 5]),
270
        ]);
271
    }
272
273
    /**
274
     * Ajoute le customer et les users avec le pack référencement sans limite
275
     */
276
    protected function addReferencingUsers()
277
    {
278
        $this->addReferencingCustomer();
279
280
        $this->push([
281
            'USER_CHARTERER_REFERENCING' => new User([
282
                'id'                => '22444401',
283
                'name'              => 'user Charterer Referencing name',
284
                'roles'             => [1 => UserRole::CHARTERER],
285
                'customer'          => $this->get('CUSTOMER_REFERENCING'),
286
            ]),
287
            'USER_PURCHASING_MANAGER_REFERENCING' => new User([
288
                'id'                => '22444402',
289
                'name'              => 'user Purchasing Manager Spot name',
290
                'roles'             => [1 => UserRole::PROCUREMENT_MANAGER],
291
                'customer'          => $this->get('CUSTOMER_REFERENCING'),
292
            ])
293
        ]);
294
    }
295
296
    /**
297
     * Ajoute le customer avec le pack référencement chargeur
298
     */
299
    protected function addReferencingLoaderCustomer()
300
    {
301
        $this->push([
302
            'CUSTOMER_REFERENCING_LOADER' => new Customer([
303
                'id'                => '335555',
304
                'name'              => 'customer Referencing Loader name',
305
            ]),
306
            'PACK_CUSTOMER_REFERENCING_LOADER' => new CustomerPack(['customerId' => '335555', 'packId' => 16]),
307
        ]);
308
    }
309
310
    /**
311
     * Ajoute le customer et les users avec le pack référencement chargeur
312
     */
313
    protected function addReferencingLoaderUsers()
314
    {
315
        $this->addReferencingLoaderCustomer();
316
317
        $this->push([
318
            'USER_CHARTERER_REFERENCING_LOADER' => new User([
319
                'id'                => '22555501',
320
                'name'              => 'user Charterer Referencing Loader name',
321
                'roles'             => [1 => UserRole::CHARTERER],
322
                'customer'          => $this->get('CUSTOMER_REFERENCING_LOADER'),
323
            ]),
324
            'USER_PURCHASING_MANAGER_REFERENCING_LOADER' => new User([
325
                'id'                => '22555502',
326
                'name'              => 'user Purchasing Manager Referencing Loader name',
327
                'roles'             => [1 => UserRole::PROCUREMENT_MANAGER],
328
                'customer'          => $this->get('CUSTOMER_REFERENCING_LOADER'),
329
            ])
330
        ]);
331
    }
332
333
    /**
334
     * Ajoute le customer avec le pack communauté
335
     */
336
    protected function addCommunityCustomer()
337
    {
338
        $this->push([
339
            'CUSTOMER_COMMUNITY' => new Customer([
340
                'id'                => '336666',
341
                'name'              => 'customer Community name',
342
            ]),
343
            'PACK_CUSTOMER_COMMUNITY_SPOT' => new CustomerPack(['customerId' => '336666', 'packId' => 3]),
344
            'PACK_CUSTOMER_COMMUNITY' => new CustomerPack(['customerId' => '336666', 'packId' => 15]),
345
        ]);
346
    }
347
348
    /**
349
     * Ajoute le customer et les users avec le pack communauté
350
     */
351
    protected function addCommunityUsers()
352
    {
353
        $this->addCommunityCustomer();
354
355
        $this->push([
356
            'USER_CHARTERER_COMMUNITY' => new User([
357
                'id'                => '22666601',
358
                'name'              => 'user Charterer Community name',
359
                'roles'             => [1 => UserRole::CHARTERER],
360
                'customer'          => $this->get('CUSTOMER_COMMUNITY'),
361
            ]),
362
            'USER_PURCHASING_MANAGER_COMMUNITY' => new User([
363
                'id'                => '22666602',
364
                'name'              => 'user Purchasing Manager Community name',
365
                'roles'             => [1 => UserRole::PROCUREMENT_MANAGER],
366
                'customer'          => $this->get('CUSTOMER_COMMUNITY'),
367
            ])
368
        ]);
369
    }
370
371
    /**
372
     * Ajoute le customer avec le pack référencement limité
373
     */
374
    protected function addReferencingLimitedCustomer()
375
    {
376
        $this->push([
377
            'CUSTOMER_REFERENCING_LIMIT' => new Customer([
378
                'id'                => '337777',
379
                'name'              => 'customer with Referencing Limit name',
380
            ]),
381
            'PACK_CUSTOMER_REFERENCING_LIMIT_BASE'  => new CustomerPack(['customerId' => '337777', 'packId' => 5]),
382
            'PACK_CUSTOMER_REFERENCING_LIMIT'       => new CustomerPack(['customerId' => '337777', 'packId' => 14, 'options' => ['invitationsLimit' => 25]]),
383
        ]);
384
    }
385
386
    /**
387
     * Ajoute le customer et les users avec le pack référencement limité
388
     */
389
    protected function addReferencingLimitedUsers()
390
    {
391
        $this->addReferencingLimitedCustomer();
392
393
        $this->push([
394
            'USER_REFERENCING_LIMIT' => new User([
395
                'id'                => '22777701',
396
                'name'              => 'user Purchasing Manager Referencing Limit name',
397
                'roles'             => [1 => UserRole::PROCUREMENT_MANAGER],
398
                'customer'          => $this->get('CUSTOMER_REFERENCING_LIMIT'),
399
            ])
400
        ]);
401
    }
402
403
    /**
404
     * Ajoute le customer avec le pack référencement limité
405
     */
406
    protected function addHistoricConfortCustomer()
407
    {
408
        $this->push([
409
            'CUSTOMER_HISTORIC_CONFORT' => new Customer([
410
                'id'                => '338888',
411
                'name'              => 'customer Historic Confort name',
412
            ]),
413
            'PACK_CUSTOMER_HISTORIC_CONFORT_BASE'   => new CustomerPack(['customerId' => '338888', 'packId' => 2]),
414
            'PACK_CUSTOMER_HISTORIC_CONFORT'        => new CustomerPack(['customerId' => '338888', 'packId' => 7]),
415
        ]);
416
    }
417
418
    /**
419
     * Ajoute le customer et les users avec le pack référencement limité
420
     */
421
    protected function addHistoricConfortUsers()
422
    {
423
        $this->addHistoricConfortCustomer();
424
425
        $this->push([
426
            'USER_HISTORIC_EXPLOIT_CONFORT' => new User([
427
                'id'                => '22888801',
428
                'name'              => 'user Exploit Historic Confort name',
429
                'roles'             => [1 => UserRole::CARRIER],
430
                'customer'          => $this->get('CUSTOMER_HISTORIC_CONFORT'),
431
            ]),
432
            'USER_HISTORIC_OFFICE_MANAGER_CONFORT' => new User([
433
                'id'                => '22888802',
434
                'name'              => 'user Office Manager Historic Confort name',
435
                'roles'             => [1 => UserRole::ADMINISTRATIVE_MANAGER],
436
                'customer'          => $this->get('CUSTOMER_HISTORIC_CONFORT'),
437
            ]),
438
        ]);
439
    }
440
441
    /**
442
     * Ajoute les packs dans le test pack
443
     */
444
    protected function addPacks()
445
    {
446
        $this->push([
447
            'PACK_SIMPLY'               => new Pack(['id' => 1, 'label' => 'Simply']),
448
            'PACK_CONFORT'              => new Pack(['id' => 2, 'label' => 'Confort']),
449
            'PACK_SPOT'                 => new Pack(['id' => 3, 'label' => 'Spot']),
450
            'PACK_REFERENCING'          => new Pack(['id' => 5, 'label' => 'Premium']),
451
            'PACK_HISTORIC_CONFORT'     => new Pack(['id' => 7, 'label' => 'Historique Confort']),
452
            'PACK_REFERENCING_LIMIT'    => new Pack(['id' => 14, 'label' => 'Référencement']),
453
            'PACK_COMMUNITY'            => new Pack(['id' => 15, 'label' => 'Communauté']),
454
            'PACK_REFERENCING_LOADER'   => new Pack(['id' => 16, 'label' => 'Référencement chargeur']),
455
        ]);
456
    }
457
458
    /**
459
     * Ajoute des utilisateurs pour tester les query bulk
460
     */
461
    public function addBulkUsers($addCustomer = true)
462
    {
463
        $this->register([User::class]);
464
465
        if ($addCustomer) {
466
            $this->register([Pack::class, Customer::class, CustomerPack::class]);
467
468
            $this->addCommonCustomer();
469
            $customer = $this->get('CUSTOMER_COMMON');
470
        } else {
471
            $customer = new Customer(['id' => '330000']);
472
        }
473
474
        $users = [];
475
476
        for ($i = 1; $i < 500; ++$i) {
477
            $users[] = new User([
478
                'id'                => '23000'.str_pad($i, 3, '0', STR_PAD_LEFT),
479
                'name'              => 'user Common '.$i,
480
                'roles'             => [1 => UserRole::ADMINISTRATIVE_MANAGER, 2 => UserRole::PROCUREMENT_MANAGER, 3 => UserRole::CHARTERER, 5 => UserRole::MULTISITE_MANAGER],
481
                'customer'          => $customer
482
            ]);
483
        }
484
485
        $this->push($users);
486
    }
487
488
    /**
489
     * @param array $entities
490
     */
491
    public function push($entities = [])
492
    {
493
        foreach ($entities as $key => $entity) {
494
            $this->prime->repository($entity)->replace($entity);
0 ignored issues
show
The method replace() does not exist on Bdf\Prime\Repository\RepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Repository\RepositoryInterface. ( Ignorable by Annotation )

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

494
            $this->prime->repository($entity)->/** @scrutinizer ignore-call */ replace($entity);
Loading history...
495
            $this->data[$key] = $entity;
496
        }
497
    }
498
}