Passed
Pull Request — master (#1311)
by Dante
01:32
created

ModulesComponent::upload()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 63
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 6
eloc 37
c 1
b 1
f 0
nc 5
nop 1
dl 0
loc 63
rs 8.7057

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2022 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace App\Controller\Component;
14
15
use App\Core\Exception\UploadException;
16
use App\Utility\DateRangesTools;
17
use App\Utility\OEmbed;
18
use App\Utility\RelationsTools;
19
use App\Utility\SchemaTrait;
20
use BEdita\WebTools\ApiClientProvider;
21
use Cake\Cache\Cache;
22
use Cake\Controller\Component;
23
use Cake\Core\Configure;
24
use Cake\Event\Event;
25
use Cake\Event\EventInterface;
26
use Cake\Http\Client\Response;
27
use Cake\Http\Exception\BadRequestException;
28
use Cake\Http\Exception\InternalErrorException;
29
use Cake\I18n\I18n;
30
use Cake\Utility\Hash;
31
32
/**
33
 * Component to load available modules.
34
 *
35
 * @property \Authentication\Controller\Component\AuthenticationComponent $Authentication
36
 * @property \App\Controller\Component\ChildrenComponent $Children
37
 * @property \App\Controller\Component\ConfigComponent $Config
38
 * @property \App\Controller\Component\ParentsComponent $Parents
39
 * @property \App\Controller\Component\SchemaComponent $Schema
40
 */
41
class ModulesComponent extends Component
42
{
43
    use SchemaTrait;
44
45
    /**
46
     * Fixed relationships to be loaded for each object
47
     *
48
     * @var array
49
     */
50
    public const FIXED_RELATIONSHIPS = [
51
        'parent',
52
        'children',
53
        'parents',
54
        'translations',
55
        'streams',
56
        'roles',
57
    ];
58
59
    /**
60
     * @inheritDoc
61
     */
62
    public $components = ['Authentication', 'Children', 'Config', 'Parents', 'Schema'];
63
64
    /**
65
     * @inheritDoc
66
     */
67
    protected $_defaultConfig = [
68
        'currentModuleName' => null,
69
        'clearHomeCache' => false,
70
    ];
71
72
    /**
73
     * Project modules for a user from `/home` endpoint
74
     *
75
     * @var array
76
     */
77
    protected $modules = [];
78
79
    /**
80
     * Other "logic" modules, non objects
81
     *
82
     * @var array
83
     */
84
    protected $otherModules = [
85
        'tags' => [
86
            'name' => 'tags',
87
            'hints' => ['allow' => ['GET', 'POST', 'PATCH', 'DELETE']],
88
        ],
89
    ];
90
91
    /**
92
     * @inheritDoc
93
     */
94
    public function beforeFilter(EventInterface $event): ?Response
95
    {
96
        /** @var \Authentication\Identity|null $user */
97
        $user = $this->Authentication->getIdentity();
98
        if (!empty($user)) {
99
            $this->getController()->set('modules', $this->getModules());
100
        }
101
102
        return null;
103
    }
104
105
    /**
106
     * Read modules and project info from `/home' endpoint.
107
     *
108
     * @return void
109
     */
110
    public function startup(): void
111
    {
112
        /** @var \Authentication\Identity|null $user */
113
        $user = $this->Authentication->getIdentity();
114
        if (empty($user) || !$user->get('id')) {
115
            $this->getController()->set(['modules' => [], 'project' => []]);
116
117
            return;
118
        }
119
120
        if ($this->getConfig('clearHomeCache')) {
121
            Cache::delete(sprintf('home_%d', $user->get('id')));
122
        }
123
124
        $project = $this->getProject();
125
        $uploadable = (array)Hash::get($this->Schema->objectTypesFeatures(), 'uploadable');
126
        $this->getController()->set(compact('project', 'uploadable'));
127
128
        $currentModuleName = $this->getConfig('currentModuleName');
129
        $modules = (array)$this->getController()->viewBuilder()->getVar('modules');
130
        if (!empty($currentModuleName)) {
131
            $currentModule = Hash::get($modules, $currentModuleName);
132
        }
133
134
        if (!empty($currentModule)) {
135
            $this->getController()->set(compact('currentModule'));
136
        }
137
    }
138
139
    /**
140
     * Create internal list of available modules in `$this->modules` as an array with `name` as key
141
     * and return it.
142
     * Modules are created from configuration and merged with information read from `/home` endpoint
143
     *
144
     * @return array
145
     */
146
    public function getModules(): array
147
    {
148
        $modules = (array)Configure::read('Modules');
149
        $pluginModules = array_filter($modules, function ($item) {
150
            return !empty($item['route']);
151
        });
152
        $metaModules = $this->modulesFromMeta() + $this->otherModules;
153
        $modules = array_intersect_key($modules, $metaModules);
154
        array_walk(
155
            $modules,
156
            function (&$data, $key) use ($metaModules) {
157
                $data = array_merge((array)Hash::get($metaModules, $key), $data);
158
            }
159
        );
160
        $this->modules = array_merge(
161
            $modules,
162
            array_diff_key($metaModules, $modules),
163
            $pluginModules
164
        );
165
        $this->modulesByAccessControl();
166
        if (!$this->Schema->tagsInUse()) {
167
            unset($this->modules['tags']);
168
        }
169
170
        return $this->modules;
171
    }
172
173
    /**
174
     * This filters modules and apply 'AccessControl' config by user role, if any.
175
     * Module can be "hidden": remove from modules.
176
     * Module can be "readonly": adjust "hints.allow" for module.
177
     *
178
     * @return void
179
     */
180
    protected function modulesByAccessControl(): void
181
    {
182
        $accessControl = (array)Configure::read('AccessControl');
183
        if (empty($accessControl)) {
184
            return;
185
        }
186
        /** @var \Authentication\Identity|null $user */
187
        $user = $this->Authentication->getIdentity();
188
        $userRoles = (array)$user->get('roles');
189
        if (empty($user) || empty($user->getOriginalData()) || in_array('admin', $userRoles)) {
190
            return;
191
        }
192
        $roles = array_intersect(array_keys($accessControl), $userRoles);
193
        $modules = (array)array_keys($this->modules);
194
        $hidden = [];
195
        $readonly = [];
196
        $write = [];
197
        foreach ($roles as $role) {
198
            $h = (array)Hash::get($accessControl, sprintf('%s.hidden', $role));
199
            $hidden = empty($hidden) ? $h : array_intersect($hidden, $h);
200
            $r = (array)Hash::get($accessControl, sprintf('%s.readonly', $role));
201
            $readonly = empty($readonly) ? $r : array_intersect($readonly, $r);
202
            $write = array_unique(array_merge($write, array_diff($modules, $hidden, $readonly)));
203
        }
204
        // Note: https://github.com/bedita/manager/issues/969 Accesses priority is "write" > "read" > "hidden"
205
        $readonly = array_diff($readonly, $write);
206
        $hidden = array_diff($hidden, $readonly, $write);
207
        if (empty($hidden) && empty($readonly)) {
208
            return;
209
        }
210
        // remove "hidden"
211
        $this->modules = array_diff_key($this->modules, array_flip($hidden));
212
        // make sure $readonly contains valid module names
213
        $readonly = array_intersect($readonly, array_keys($this->modules));
214
        foreach ($readonly as $key) {
215
            $path = sprintf('%s.hints.allow', $key);
216
            $allow = (array)Hash::get($this->modules, $path);
217
            $this->modules[$key]['hints']['allow'] = array_diff($allow, ['POST', 'PATCH', 'DELETE']);
218
        }
219
    }
220
221
    /**
222
     * Modules data from `/home` endpoint 'meta' response.
223
     * Modules are object endpoints from BE4 API
224
     *
225
     * @return array
226
     */
227
    protected function modulesFromMeta(): array
228
    {
229
        /** @var \Authentication\Identity $user */
230
        $user = $this->Authentication->getIdentity();
231
        $meta = $this->getMeta($user);
232
        $modules = collection(Hash::get($meta, 'resources', []))
233
            ->map(function (array $data, $endpoint) {
234
                $name = substr($endpoint, 1);
235
236
                return $data + compact('name');
237
            })
238
            ->reject(function (array $data) {
239
                return Hash::get($data, 'hints.object_type') !== true && !in_array(Hash::get($data, 'name'), ['trash', 'translations']);
240
            })
241
            ->toList();
242
243
        return Hash::combine($modules, '{n}.name', '{n}');
244
    }
245
246
    /**
247
     * Get information about current project.
248
     *
249
     * @return array
250
     */
251
    public function getProject(): array
252
    {
253
        /** @var \Authentication\Identity $user */
254
        $user = $this->Authentication->getIdentity();
255
        $api = $this->getMeta($user);
256
        $apiName = (string)Hash::get($api, 'project.name');
257
        $apiName = str_replace('API', '', $apiName);
258
        $api['project']['name'] = $apiName;
259
260
        return [
261
            'api' => (array)Hash::get($api, 'project'),
262
            'beditaApi' => [
263
                'name' => (string)Hash::get(
264
                    (array)Configure::read('Project'),
265
                    'name',
266
                    (string)Hash::get($api, 'project.name')
267
                ),
268
                'version' => (string)Hash::get($api, 'version'),
269
            ],
270
        ];
271
    }
272
273
    /**
274
     * Check if an object type is abstract or concrete.
275
     * This method MUST NOT be called from `beforeRender` since `$this->modules` array is still not initialized.
276
     *
277
     * @param string $name Name of object type.
278
     * @return bool True if abstract, false if concrete
279
     */
280
    public function isAbstract(string $name): bool
281
    {
282
        return (bool)Hash::get($this->modules, sprintf('%s.hints.multiple_types', $name), false);
283
    }
284
285
    /**
286
     * Get list of object types
287
     * This method MUST NOT be called from `beforeRender` since `$this->modules` array is still not initialized.
288
     *
289
     * @param bool|null $abstract Only abstract or concrete types.
290
     * @return array Type names list
291
     */
292
    public function objectTypes(?bool $abstract = null): array
293
    {
294
        $types = [];
295
        foreach ($this->modules as $name => $data) {
296
            if (empty($data['hints']['object_type'])) {
297
                continue;
298
            }
299
            if ($abstract === null || $data['hints']['multiple_types'] === $abstract) {
300
                $types[] = $name;
301
            }
302
        }
303
304
        return $types;
305
    }
306
307
    /**
308
     * Read oEmbed metadata
309
     *
310
     * @param string $url Remote URL
311
     * @return array|null
312
     * @codeCoverageIgnore
313
     */
314
    protected function oEmbedMeta(string $url): ?array
315
    {
316
        return (new OEmbed())->readMetadata($url);
317
    }
318
319
    /**
320
     * Upload a file and store it in a media stream
321
     * Or create a remote media trying to get some metadata via oEmbed
322
     *
323
     * @param array $requestData The request data from form
324
     * @return void
325
     */
326
    public function upload(array &$requestData): void
327
    {
328
        $uploadBehavior = Hash::get($requestData, 'upload_behavior', 'file');
329
330
        if ($uploadBehavior === 'embed' && !empty($requestData['remote_url'])) {
331
            $data = $this->oEmbedMeta($requestData['remote_url']);
332
            $requestData = array_filter($requestData) + $data;
333
334
            return;
335
        }
336
        if (empty($requestData['file'])) {
337
            return;
338
        }
339
340
        // verify upload form data
341
        if ($this->checkRequestForUpload($requestData)) {
342
            /** @var \Laminas\Diactoros\UploadedFile $file */
343
            $file = $requestData['file'];
344
            $filepath = $file->getStream()->getMetadata('uri');
345
            $content = file_get_contents($filepath);
346
347
            // upload file
348
            $filename = basename($file->getClientFileName());
349
            $headers = ['Content-Type' => $file->getClientMediaType()];
350
            $apiClient = ApiClientProvider::getApiClient();
351
            $type = $this->getController()->getRequest()->getParam('object_type');
352
353
            if (empty($requestData['id'])) {
354
                $response = $apiClient->post(
355
                    sprintf('/%s/upload/%s', $type, $filename),
356
                    $content,
357
                    $headers
358
                );
359
                $requestData['id'] = Hash::get($response, 'data.id');
360
                unset($requestData['file'], $requestData['remote_url']);
361
362
                return;
363
            }
364
365
            // remove stream
366
            $this->removeStream($requestData);
367
368
            // create stream
369
            $response = $apiClient->post(
370
                sprintf('/streams/upload/%s', $filename),
371
                $content,
372
                $headers
373
            );
374
375
            // link stream to media
376
            $streamUuid = Hash::get($response, 'data.id');
377
            $mediaId = (string)$requestData['id'];
378
            $response = $apiClient->patch(
379
                sprintf('/streams/%s/relationships/object', $streamUuid),
380
                json_encode([
381
                    'data' => [
382
                        'id' => $mediaId,
383
                        'type' => $type,
384
                    ],
385
                ])
386
            );
387
        }
388
        unset($requestData['file'], $requestData['remote_url']);
389
    }
390
391
    /**
392
     * Remove a stream from a media, if any
393
     *
394
     * @param array $requestData The request data from form
395
     * @return bool
396
     */
397
    public function removeStream(array $requestData): bool
398
    {
399
        if (empty($requestData['id'])) {
400
            return false;
401
        }
402
403
        $apiClient = ApiClientProvider::getApiClient();
404
        $response = $apiClient->get(sprintf('/%s/%s/streams', $requestData['model-type'], $requestData['id']));
405
        if (empty($response['data'])) { // no streams for media
406
            return false;
407
        }
408
        $streamId = Hash::get($response, 'data.0.id');
409
        $apiClient->deleteObject($streamId, 'streams');
410
411
        return true;
412
    }
413
414
    /**
415
     * Associate a stream to a media using API
416
     * If $requestData['id'] is null, create media from stream.
417
     * If $requestData['id'] is not null, replace properly related stream.
418
     *
419
     * @param string $streamId The stream ID
420
     * @param array $requestData The request data
421
     * @param string $defaultTitle The default title for media
422
     * @return string The media ID
423
     * @deprecated 5.15.4 This method is no longer used and will be removed in a future version.
424
     * @codeCoverageIgnore
425
     */
426
    public function assocStreamToMedia(string $streamId, array &$requestData, string $defaultTitle): string
427
    {
428
        $apiClient = ApiClientProvider::getApiClient();
429
        $type = $requestData['model-type'];
430
        if (empty($requestData['id'])) {
431
            // create media from stream
432
            // save only `title` (filename if not set) and `status` in new media object
433
            $attributes = array_filter([
434
                'title' => !empty($requestData['title']) ? $requestData['title'] : $defaultTitle,
435
                'status' => Hash::get($requestData, 'status'),
436
            ]);
437
            $data = compact('type', 'attributes');
438
            $body = compact('data');
439
            $response = $apiClient->createMediaFromStream($streamId, $type, $body);
440
            // `title` and `status` saved here, remove from next save
441
            unset($requestData['title'], $requestData['status']);
442
443
            return (string)Hash::get($response, 'data.id');
444
        }
445
446
        // assoc existing media to stream
447
        $id = (string)Hash::get($requestData, 'id');
448
        $data = compact('id', 'type');
449
        $apiClient->replaceRelated($streamId, 'streams', 'object', $data);
450
451
        return $id;
452
    }
453
454
    /**
455
     * Check request data for upload and return true if upload is boht possible and needed
456
     *
457
     * @param array $requestData The request data
458
     * @return bool true if upload is possible and needed
459
     */
460
    public function checkRequestForUpload(array $requestData): bool
461
    {
462
        /** @var \Laminas\Diactoros\UploadedFile $file */
463
        $file = $requestData['file'];
464
        $error = $file->getError();
465
        // check if change file is empty
466
        if ($error === UPLOAD_ERR_NO_FILE) {
467
            return false;
468
        }
469
470
        // if upload error, throw exception
471
        if ($error !== UPLOAD_ERR_OK) {
472
            throw new UploadException(null, $error);
473
        }
474
475
        // verify presence and value of 'name', 'tmp_name', 'type'
476
        $name = $file->getClientFileName();
477
        if (empty($name)) {
478
            throw new InternalErrorException('Invalid form data: file.name');
479
        }
480
        $uri = $file->getStream()->getMetadata('uri');
481
        if (empty($uri)) {
482
            throw new InternalErrorException('Invalid form data: file.tmp_name');
483
        }
484
485
        // verify 'model-type'
486
        if (empty($requestData['model-type']) || !is_string($requestData['model-type'])) {
487
            throw new InternalErrorException('Invalid form data: model-type');
488
        }
489
490
        return true;
491
    }
492
493
    /**
494
     * Check if save can be skipped.
495
     * This is used to avoid saving object with no changes.
496
     *
497
     * @param string $id The object ID
498
     * @param array $requestData The request data
499
     * @return bool True if save can be skipped, false otherwise
500
     */
501
    public function skipSaveObject(string $id, array &$requestData): bool
502
    {
503
        if (empty($id)) {
504
            return false;
505
        }
506
        if (isset($requestData['date_ranges'])) {
507
            // check if date_ranges has changed
508
            $type = $this->getController()->getRequest()->getParam('object_type');
509
            $response = ApiClientProvider::getApiClient()->getObject($id, $type, ['fields' => 'date_ranges']);
510
            $actualDateRanges = (array)Hash::get($response, 'data.attributes.date_ranges');
511
            $dr1 = DateRangesTools::toString($actualDateRanges);
512
            $requestDateRanges = (array)Hash::get($requestData, 'date_ranges');
513
            $dr2 = DateRangesTools::toString($requestDateRanges);
514
            if ($dr1 === $dr2) {
515
                unset($requestData['date_ranges']);
516
            } else {
517
                return false;
518
            }
519
        }
520
        $data = array_filter($requestData, function ($key) {
521
            return !in_array($key, ['id', 'date_ranges', 'permissions']);
522
        }, ARRAY_FILTER_USE_KEY);
523
524
        return empty($data);
525
    }
526
527
    /**
528
     * Check if save related can be skipped.
529
     * This is used to avoid saving object relations with no changes.
530
     *
531
     * @param string $id The object ID
532
     * @param array $relatedData The related data
533
     * @return bool True if save related can be skipped, false otherwise
534
     */
535
    public function skipSaveRelated(string $id, array &$relatedData): bool
536
    {
537
        if (empty($relatedData)) {
538
            return true;
539
        }
540
        $methods = (array)Hash::extract($relatedData, '{n}.method');
541
        if (in_array('addRelated', $methods) || in_array('removeRelated', $methods)) {
542
            return false;
543
        }
544
        // check replaceRelated
545
        $type = $this->getController()->getRequest()->getParam('object_type');
546
        $rr = $relatedData;
547
        foreach ($rr as $method => $data) {
548
            $actualRelated = (array)ApiClientProvider::getApiClient()->getRelated($id, $type, $data['relation']);
549
            $actualRelated = (array)Hash::get($actualRelated, 'data');
550
            $actualRelated = RelationsTools::toString($actualRelated);
551
            $requestRelated = (array)Hash::get($data, 'relatedIds', []);
552
            $requestRelated = RelationsTools::toString($requestRelated);
553
            if ($actualRelated === $requestRelated) {
554
                unset($relatedData[$method]);
555
                continue;
556
            }
557
558
            return false;
559
        }
560
561
        return empty($relatedData);
562
    }
563
564
    /**
565
     * Check if save permissions can be skipped.
566
     * This is used to avoid saving object permissions with no changes.
567
     *
568
     * @param string $id The object ID
569
     * @param array $requestPermissions The request permissions
570
     * @param array $schema The object type schema
571
     * @return bool True if save permissions can be skipped, false otherwise
572
     */
573
    public function skipSavePermissions(string $id, array $requestPermissions, array $schema): bool
574
    {
575
        if (!in_array('Permissions', (array)Hash::get($schema, 'associations'))) {
576
            return true;
577
        }
578
        $requestPermissions = array_map(
579
            function ($role) {
580
                return (int)$role;
581
            },
582
            $requestPermissions
583
        );
584
        sort($requestPermissions);
585
        $query = ['filter' => ['object_id' => $id], 'page_size' => 100];
586
        $objectPermissions = (array)ApiClientProvider::getApiClient()->getObjects('object_permissions', $query);
587
        $actualPermissions = (array)Hash::extract($objectPermissions, 'data.{n}.attributes.role_id');
588
        sort($actualPermissions);
589
590
        return $actualPermissions === $requestPermissions;
591
    }
592
593
    /**
594
     * Set current attributes from loaded $object data in `currentAttributes`.
595
     *
596
     * @param array $object The object.
597
     * @return void
598
     */
599
    public function setupAttributes(array &$object): void
600
    {
601
        $currentAttributes = json_encode((array)Hash::get($object, 'attributes'));
602
        $this->getController()->set(compact('currentAttributes'));
603
    }
604
605
    /**
606
     * Setup relations information metadata.
607
     *
608
     * @param array $schema Relations schema.
609
     * @param array $relationships Object relationships.
610
     * @param array $order Ordered names inside 'main' and 'aside' keys.
611
     * @param array $hidden List of hidden relations.
612
     * @param array $readonly List of readonly relations.
613
     * @return void
614
     */
615
    public function setupRelationsMeta(array $schema, array $relationships, array $order = [], array $hidden = [], array $readonly = []): void
616
    {
617
        // relations between objects
618
        $relationsSchema = $this->relationsSchema($schema, $relationships, $hidden, $readonly);
619
        // relations between objects and resources
620
        $resourceRelations = array_diff(array_keys($relationships), array_keys($relationsSchema), $hidden, self::FIXED_RELATIONSHIPS);
621
        // set objectRelations array with name as key and label as value
622
        $relationNames = array_keys($relationsSchema);
623
624
        // define 'main' and 'aside' relation groups
625
        $aside = array_intersect((array)Hash::get($order, 'aside'), $relationNames);
626
        $relationNames = array_diff($relationNames, $aside);
627
        $main = array_intersect((array)Hash::get($order, 'main'), $relationNames);
628
        $main = array_unique(array_merge($main, $relationNames));
629
630
        $objectRelations = [
631
            'main' => $this->relationLabels($relationsSchema, $main),
632
            'aside' => $this->relationLabels($relationsSchema, $aside),
633
        ];
634
635
        $this->getController()->set(compact('relationsSchema', 'resourceRelations', 'objectRelations'));
636
    }
637
638
    /**
639
     * Relations schema by schema and relationships.
640
     *
641
     * @param array $schema The schema
642
     * @param array $relationships The relationships
643
     * @param array $hidden Hidden relationships
644
     * @param array $readonly Readonly relationships
645
     * @return array
646
     */
647
    protected function relationsSchema(array $schema, array $relationships, array $hidden = [], array $readonly = []): array
648
    {
649
        $types = $this->objectTypes(false);
650
        sort($types);
651
        $relationsSchema = array_diff_key(array_intersect_key($schema, $relationships), array_flip($hidden));
652
653
        foreach ($relationsSchema as $relName => &$relSchema) {
654
            if (in_array('objects', (array)Hash::get($relSchema, 'right'))) {
655
                $relSchema['right'] = $types;
656
            }
657
            if (!empty($relationships[$relName]['readonly']) || in_array($relName, $readonly)) {
658
                $relSchema['readonly'] = true;
659
            }
660
        }
661
662
        return $relationsSchema;
663
    }
664
665
    /**
666
     * Retrieve associative array with names as keys and labels as values.
667
     *
668
     * @param array $relationsSchema Relations schema.
669
     * @param array $names Relation names.
670
     * @return array
671
     */
672
    protected function relationLabels(array &$relationsSchema, array $names): array
673
    {
674
        return (array)array_combine(
675
            $names,
676
            array_map(
677
                function ($r) use ($relationsSchema) {
678
                    // return 'label' or 'inverse_label' looking at 'name'
679
                    $attributes = $relationsSchema[$r]['attributes'];
680
                    if ($r === $attributes['name']) {
681
                        return $attributes['label'];
682
                    }
683
684
                    return $attributes['inverse_label'];
685
                },
686
                $names
687
            )
688
        );
689
    }
690
691
    /**
692
     * Get related types from relation name.
693
     *
694
     * @param array $schema Relations schema.
695
     * @param string $relation Relation name.
696
     * @return array
697
     */
698
    public function relatedTypes(array $schema, string $relation): array
699
    {
700
        $relationsSchema = (array)Hash::get($schema, $relation);
701
702
        return (array)Hash::get($relationsSchema, 'right');
703
    }
704
705
    /**
706
     * Save related objects.
707
     *
708
     * @param string $id Object ID
709
     * @param string $type Object type
710
     * @param array $relatedData Related objects data
711
     * @return void
712
     */
713
    public function saveRelated(string $id, string $type, array $relatedData): void
714
    {
715
        foreach ($relatedData as $data) {
716
            $this->saveRelatedObjects($id, $type, $data);
717
            $event = new Event('Controller.afterSaveRelated', $this, compact('id', 'type', 'data'));
718
            $this->getController()->getEventManager()->dispatch($event);
719
        }
720
    }
721
722
    /**
723
     * Save related objects per object by ID.
724
     *
725
     * @param string $id Object ID
726
     * @param string $type Object type
727
     * @param array $data Related object data
728
     * @return array|null
729
     * @throws \Cake\Http\Exception\BadRequestException
730
     */
731
    public function saveRelatedObjects(string $id, string $type, array $data): ?array
732
    {
733
        $method = (string)Hash::get($data, 'method');
734
        if (!in_array($method, ['addRelated', 'removeRelated', 'replaceRelated'])) {
735
            throw new BadRequestException(__('Bad related data method'));
736
        }
737
        $relation = (string)Hash::get($data, 'relation');
738
        $related = $this->getRelated($data);
739
        if ($relation === 'parent' && $type === 'folders') {
740
            return $this->Parents->{$method}($id, $related);
741
        }
742
        if ($relation === 'children' && $type === 'folders') {
743
            return $this->Children->{$method}($id, $related);
744
        }
745
        $lang = I18n::getLocale();
746
        $headers = ['Accept-Language' => $lang];
747
748
        return ApiClientProvider::getApiClient()->{$method}($id, $type, $relation, $related, $headers);
749
    }
750
751
    /**
752
     * Get related objects.
753
     * If related object has no ID, it will be created.
754
     *
755
     * @param array $data Related object data
756
     * @return array
757
     */
758
    public function getRelated(array $data): array
759
    {
760
        $related = (array)Hash::get($data, 'relatedIds');
761
        if (empty($related)) {
762
            return [];
763
        }
764
        $relatedObjects = [];
765
        foreach ($related as $obj) {
766
            if (!empty($obj['id'])) {
767
                $relatedObjects[] = [
768
                    'id' => $obj['id'],
769
                    'type' => $obj['type'],
770
                    'meta' => (array)Hash::get($obj, 'meta'),
771
                ];
772
                continue;
773
            }
774
            $response = ApiClientProvider::getApiClient()->save(
775
                (string)Hash::get($obj, 'type'),
776
                (array)Hash::get($obj, 'attributes')
777
            );
778
            $relatedObjects[] = [
779
                'id' => Hash::get($response, 'data.id'),
780
                'type' => Hash::get($response, 'data.type'),
781
                'meta' => (array)Hash::get($response, 'data.meta'),
782
            ];
783
        }
784
785
        return $relatedObjects;
786
    }
787
}
788