Passed
Pull Request — master (#123)
by
unknown
04:16
created

AnnotationRequest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAll() 0 19 4
A requestAnnotions() 0 13 3
A __construct() 0 3 1
1
<?php
2
3
namespace Kitodo\Dlf\Common;
4
5
/**
6
 * (c) Kitodo. Key to digital objects e.V. <[email protected]>
7
 *
8
 * This file is part of the Kitodo and TYPO3 projects.
9
 *
10
 * @license GNU General Public License version 3 or later.
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 */
14
15
class AnnotationRequest
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $apiUrl = '';
21
22
    /**
23
     * @param string $apiUrl The url of the annotation server api.
24
     */
25
    public function __construct($apiUrl)
26
    {
27
        $this->apiUrl =  trim($apiUrl, "/ ");
28
    }
29
30
31
    /**
32
     * Requests the annotation server
33
     *
34
     * @param string $url The annotation request url.
35
     * @return array
36
     */
37
    protected function requestAnnotions($url) : array
38
    {
39
        $jsonld = Helper::getUrl($url);
40
41
        if ($jsonld) {
42
            $annotationData = json_decode($jsonld, true);
43
44
            if ($annotationData) {
45
                return $annotationData;
46
            }
47
        }
48
49
        return [];
50
    }
51
52
    /**
53
     * Returns all annotations of a document.
54
     *
55
     * @param string $id Document id (purl)
56
     * @return array
57
     */
58
    public function getAll($id)
59
    {
60
        $annotations = [];
61
62
        $annotationData = $this->requestAnnotions($this->apiUrl . '?target=' . urlencode($id . '/*'));
63
64
        if (array_key_exists('first', $annotationData)) {
65
            $annotationPageData = $annotationData['first'];
66
            $annotations = array_merge($annotations, $annotationPageData["items"]);
67
68
            while (array_key_exists('next', $annotationPageData)) {
69
                $annotationPageData = $this->requestAnnotions($annotationPageData['next']);
70
                if (array_key_exists('items', $annotationPageData)) {
71
                    $annotations = array_merge($annotations, $annotationPageData["items"]);
72
                }
73
            }
74
        }
75
76
        return $annotations;
77
    }
78
}
79