LinkedDataResource::resolve()   A
last analyzed

Complexity

Conditions 2
Paths 6

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 22
rs 9.7998
c 0
b 0
f 0
cc 2
nc 6
nop 1
1
<?php
2
3
class LinkedDataResource extends RemoteResource
4
{
5
    public function resolve(int $timeout): ?EasyRdf\Resource
6
    {
7
        // prevent parsing errors for sources which return invalid JSON (see #447)
8
        // 1. Unregister the legacy RDF/JSON parser, we don't want to use it
9
        EasyRdf\Format::unregister('json');
10
        // 2. Add "application/json" as a possible MIME type for the JSON-LD format
11
        $jsonld = EasyRdf\Format::getFormat('jsonld');
12
        $mimetypes = $jsonld->getMimeTypes();
13
        $mimetypes['application/json'] = 0.5;
14
        $jsonld->setMimeTypes($mimetypes);
15
16
        try {
17
            // change the timeout setting for external requests
18
            $httpclient = EasyRdf\Http::getDefaultHttpClient();
19
            $httpclient->setConfig(array('timeout' => $timeout, 'useragent' => 'Skosmos'));
20
            EasyRdf\Http::setDefaultHttpClient($httpclient);
21
22
            $graph = EasyRdf\Graph::newAndLoad(EasyRdf\Utils::removeFragmentFromUri($this->uri));
23
            return $graph->resource($this->uri);
24
        } catch (Exception $e) {
25
            $this->model->getLogger()->info("LD resolution failed for <{$this->uri}>: $e");
26
            return null;
27
        }
28
29
    }
30
31
}
32