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

LOCResource   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 28 5
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