• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

acdh-oeaw / arche-diss-cache / #57

24 Oct 2024 10:11PM UTC coverage: 80.841%. Remained the same
#57

push

php-coveralls

zozlak
Service::setCallback() introduced

6 of 6 new or added lines in 1 file covered. (100.0%)

173 of 214 relevant lines covered (80.84%)

3.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.64
/src/acdhOeaw/arche/lib/dissCache/RepoWrapperGuzzle.php
1
<?php
2

3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2024 zozlak.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26

27
namespace acdhOeaw\arche\lib\dissCache;
28

29
use DateTimeImmutable;
30
use GuzzleHttp\Client;
31
use GuzzleHttp\Psr7\Request;
32
use termTemplates\PredicateTemplate as PT;
33
use acdhOeaw\arche\lib\SearchConfig;
34
use acdhOeaw\arche\lib\Repo;
35
use acdhOeaw\arche\lib\RepoResource;
36
use acdhOeaw\arche\lib\RepoResourceInterface;
37
use acdhOeaw\arche\lib\exception\NotFound;
38

39
/**
40
 * Description of RepoCurl
41
 *
42
 * @author zozlak
43
 */
44
class RepoWrapperGuzzle implements RepoWrapperInterface {
45

46
    private bool $checkModDate;
47
    /**
48
     * 
49
     * @var array<string, mixed>
50
     */
51
    private array $guzzleOpts;
52
    private Client $client;
53
    /**
54
     * 
55
     * @var array<Repo>
56
     */
57
    private array $repos = [];
58

59
    /**
60
     * 
61
     * @param bool $checkModificationDate
62
     * @param array<string, mixed> $guzzleOpts
63
     */
64
    public function __construct(bool $checkModificationDate = false,
65
                                array $guzzleOpts = []) {
66
        $this->checkModDate                               = $checkModificationDate;
4✔
67
        $this->guzzleOpts                                 = $guzzleOpts;
4✔
68
        $guzzleOpts['allow_redirects']                    ??= [];
4✔
69
        $guzzleOpts['allow_redirects']['track_redirects'] = true;
4✔
70
        $guzzleOpts['http_errors']                        = false;
4✔
71
        $this->client                                     = new Client($guzzleOpts);
4✔
72
    }
73

74
    public function getResourceById(string $id, ?SearchConfig $config = null): RepoResourceInterface {
75
        $uri  = $this->resolve($id);
4✔
76
        $repo = $this->getRepo($uri);
3✔
77
        return $repo->getResourceById($uri, $config);
3✔
78
    }
79

80
    public function getModificationTimestamp(string $id): int {
81
        if (!$this->checkModDate) {
×
82
            return PHP_INT_MAX;
×
83
        }
84
        
85
        $uri                        = $this->resolve($id);
×
86
        $repo                       = $this->getRepo($uri);
×
87
        $config                     = new SearchConfig();
×
88
        $config->metadataMode       = RepoResource::META_RESOURCE;
×
89
        $modDateProp                = $repo->getSchema()->modificationDate;
×
90
        $config->resourceProperties = [(string) $modDateProp];
×
91
        $res                        = $repo->getResourceById($uri, $config);
×
92
        $modDate                    = $res->getGraph()->getObjectValue(new PT($modDateProp));
×
93
        return (new DateTimeImmutable($modDate))->getTimestamp();
×
94
    }
95

96
    private function resolve(string $id): string {
97
        $resp = $this->client->send(new Request('head', $id));
4✔
98
        $code = $resp->getStatusCode();
4✔
99
        if ($code === 401) {
4✔
100
            throw new UnauthorizedException();
×
101
        } elseif ($code !== 200) {
4✔
102
            throw new NotFound("$id can not be resolved (HTTP status code $code)", $code);
1✔
103
        }
104
        $redirects = $resp->getHeader('X-Guzzle-Redirect-History');
3✔
105
        $url       = end($redirects) ?: $id;
3✔
106
        $uri       = preg_replace('`/metadata$`', '', $url);
3✔
107
        return $uri;
3✔
108
    }
109

110
    private function getRepo(string $uri): Repo {
111
        $baseUrl               = preg_replace('`[0-9]+$`', '', $uri);
3✔
112
        $this->repos[$baseUrl] ??= new Repo($baseUrl, $this->guzzleOpts);
3✔
113
        return $this->repos[$baseUrl];
3✔
114
    }
115
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc