Completed
Push — master ( 743575...aa2450 )
by Stefano
20s queued 11s
created

ThumbsComponent   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 68
rs 10
c 1
b 0
f 0
wmc 11

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getThumbs() 0 17 2
B urls() 0 33 9
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2021 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 BEdita\SDK\BEditaClientException;
16
use BEdita\WebTools\ApiClientProvider;
17
use Cake\Controller\Component;
18
use Cake\Utility\Hash;
19
20
/**
21
 * Handles thumbs.
22
 */
23
class ThumbsComponent extends Component
24
{
25
    /**
26
     * Retrieve thumbnails URL of related objects in `meta.url` if present.
27
     *
28
     * @param array|null $response Related objects response.
29
     * @return void
30
     */
31
    public function urls(?array &$response): void
32
    {
33
        if (empty($response) || empty($response['data'])) {
34
            return;
35
        }
36
37
        // extract ids of objects
38
        $ids = (array)Hash::extract($response, 'data.{n}[type=/images|videos/].id');
39
        if (empty($ids)) {
40
            return;
41
        }
42
43
        $thumbs = $this->getThumbs($ids);
44
        if ($thumbs === null) {
45
            // An error happened: let's try again by generating one thumbnail at a time.
46
            $thumbs = [];
47
            foreach ($ids as $id) {
48
                $thumbs += (array)$this->getThumbs([$id]);
49
            }
50
        }
51
52
        foreach ($response['data'] as &$object) {
53
            $thumbnail = Hash::get($object, 'attributes.provider_thumbnail');
54
            // if provider_thumbnail is found there's no need to extract it from thumbsResponse
55
            if ($thumbnail) {
56
                $object['meta']['thumb_url'] = $thumbnail;
57
                continue;
58
            }
59
60
            // extract url of the matching objectid's thumb
61
            $thumbnail = Hash::get($thumbs, $object['id']);
62
            if ($thumbnail !== null) {
63
                $object['meta']['thumb_url'] = $thumbnail;
64
            }
65
        }
66
    }
67
68
    /**
69
     * Get thumbs by IDs
70
     *
71
     * @param array $ids The IDs
72
     * @return array|null
73
     */
74
    protected function getThumbs(array $ids): ?array
75
    {
76
        try {
77
            $params = $this->getController()->getRequest()->getQueryParams();
78
            $query = $this->getController()->Query->prepare($params);
79
            $url = sprintf('/media/thumbs?%s', http_build_query([
80
                'ids' => implode(',', $ids),
81
                'options' => ['w' => 400],
82
            ]));
83
            $apiClient = ApiClientProvider::getApiClient();
84
            $res = $apiClient->get($url, $query);
85
86
            return (array)Hash::combine($res, 'meta.thumbnails.{*}.id', 'meta.thumbnails.{*}.url');
0 ignored issues
show
Bug introduced by
It seems like $res can also be of type null; however, parameter $data of Cake\Utility\Hash::combine() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

86
            return (array)Hash::combine(/** @scrutinizer ignore-type */ $res, 'meta.thumbnails.{*}.id', 'meta.thumbnails.{*}.url');
Loading history...
87
        } catch (BEditaClientException $e) {
88
            $this->getController()->log($e, 'error');
89
90
            return null;
91
        }
92
    }
93
}
94