Completed
Push — master ( 751599...c13dee )
by Osma
02:10 queued 10s
created

LOCResource::resolve()   A

Complexity

Conditions 5
Paths 36

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 36
nop 1
dl 0
loc 28
rs 9.1608
c 0
b 0
f 0
1
<?php
2
3
class LOCResource extends RemoteResource
4
{
5
    public function resolve(int $timeout) : ?EasyRdf\Resource {
6
        $graph = new EasyRdf\Graph($this->uri);
7
        // guess the concept scheme based on the URI
8
        if (preg_match('|(http://id.loc.gov/[^/]+/[^/]+)/.*|', $this->uri, $matches)) {
9
            $graph->addResource($this->uri, 'skos:inScheme', $matches[1]);
10
        }
11
12
        try {
13
            $opts = array('http' => array('method'=>'HEAD',
14
                                          'user_agent' => 'Skosmos',
15
                                          'timeout' => $timeout));
16
            $context  = stream_context_create($opts);
17
            $fd = fopen($this->uri, 'rb', false, $context);
18
            $headers = stream_get_meta_data($fd)['wrapper_data'];
19
            foreach ($headers as $header) {
20
                if (strpos($header, 'X-PrefLabel:') === 0) {
21
                    $elems = explode(' ', $header, 2);
22
                    $prefLabel = $elems[1];
23
                    $graph->addLiteral($this->uri, 'skos:prefLabel', $prefLabel, 'en');
24
                }
25
            }
26
            fclose($fd);
27
            return $graph->resource($this->uri);
28
        } catch (Exception $e) {
29
            $this->model->getLogger()->info("LOC resolution failed for <{$this->uri}>: $e");
30
            return null;
31
        }
32
    }
33
}
34