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

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

11 Oct 2024 06:34PM UTC coverage: 81.41%. Remained the same
#7

push

php-coveralls

zozlak
Provide tests and fixes for the ResourceCache::getResponse()

50 of 74 new or added lines in 3 files covered. (67.57%)

2 existing lines in 1 file now uncovered.

127 of 156 relevant lines covered (81.41%)

3.47 hits per line

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

63.33
/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\exception\NotFound;
37

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

45
    private bool $checkModDate;
46
    private array $guzzleOpts;
47
    private Client $client;
48
    private array $repos = [];
49

50
    public function __construct(bool $checkModificationDate = false,
51
                                array $guzzleOpts = []) {
52
        $this->checkModDate                               = $checkModificationDate;
3✔
53
        $this->guzzleOpts                                 = $guzzleOpts;
3✔
54
        $guzzleOpts['allow_redirects']                    ??= [];
3✔
55
        $guzzleOpts['allow_redirects']['track_redirects'] = true;
3✔
56
        $guzzleOpts['http_errors']                        = false;
3✔
57
        $this->client                                     = new Client($guzzleOpts);
3✔
58
    }
59

60
    public function getResourceById(string $id, ?SearchConfig $config = null): RepoResource {
61
        $uri  = $this->resolve($id);
4✔
62
        $repo = $this->getRepo($uri);
3✔
63
        return $repo->getResourceById($uri, $config);
3✔
64
    }
65

66
    public function getModificationTimestamp(string $id): int {
NEW
67
        if (!$this->checkModDate) {
×
NEW
68
            return PHP_INT_MAX;
×
69
        }
70
        
NEW
71
        $uri                        = $this->resolve($id);
×
NEW
72
        $repo                       = $this->getRepo($uri);
×
NEW
73
        $config                     = new SearchConfig();
×
NEW
74
        $config->metadataMode       = RepoResource::META_RESOURCE;
×
NEW
75
        $modDateProp                = $repo->getSchema()->modificationDate;
×
NEW
76
        $config->resourceProperties = [(string) $modDateProp];
×
NEW
77
        $res                        = $repo->getResourceById($uri, $config);
×
NEW
78
        $modDate                    = $res->getGraph()->getObjectValue(new PT($modDateProp));
×
NEW
79
        return (new DateTimeImmutable($modDate))->getTimestamp();
×
80
    }
81

82
    private function resolve(string $id): string {
83
        $resp = $this->client->send(new Request('head', $id));
4✔
84
        if ($resp->getStatusCode() !== 200) {
4✔
85
            throw new NotFound("$id can not be resolved (HTTP status code " . $resp->getStatusCode() . ")");
1✔
86
        }
87
        $redirects = $resp->getHeader('X-Guzzle-Redirect-History');
3✔
88
        $url       = end($redirects) ?: $id;
3✔
89
        $uri       = preg_replace('`/metadata$`', '', $url);
3✔
90
        return $uri;
3✔
91
    }
92

93
    private function getRepo(string $uri): Repo {
94
        $baseUrl               = preg_replace('`[0-9]+$`', '', $uri);
3✔
95
        $this->repos[$baseUrl] ??= new Repo($baseUrl, $this->guzzleOpts);
3✔
96
        return $this->repos[$baseUrl];
3✔
97
    }
98
}
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