LOCResource   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 23
dl 0
loc 32
rs 10
c 0
b 0
f 0

1 Method

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