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

h4kuna / exchange / 10609481475

29 Aug 2024 05:50AM UTC coverage: 97.872% (-0.7%) from 98.587%
10609481475

push

github

h4kuna
feat(typo): remove old doc

276 of 282 relevant lines covered (97.87%)

0.98 hits per line

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

96.97
/src/RatingList/RatingListCache.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Exchange\RatingList;
4

5
use h4kuna\CriticalCache\PSR16\CacheLocking;
6
use h4kuna\CriticalCache\Utils\Dependency;
7
use h4kuna\Exchange\Download\SourceDownloadInterface;
8
use h4kuna\Exchange\Exceptions\InvalidStateException;
9
use h4kuna\Exchange\Utils;
10
use Nette\Utils\DateTime;
11
use Psr\Http\Client\ClientExceptionInterface;
12
use Psr\SimpleCache\CacheInterface;
13

14
final class RatingListCache
15
{
16
        public int $floatTtl = Utils::CacheMinutes - DateTime::MINUTE; // 29 minutes
17

18

19
        public function __construct(
1✔
20
                private CacheLocking $cache,
21
                private SourceDownloadInterface $sourceDownload,
22
        )
23
        {
24
        }
1✔
25

26

27
        /**
28
         * @throws ClientExceptionInterface
29
         */
30
        public function build(CacheEntity $cacheEntity): RatingListInterface
1✔
31
        {
32
                $ratingList = null;
1✔
33

34
                $this->cache->load($cacheEntity->cacheKeyTtl, function (
1✔
35
                        Dependency $dependency,
36
                        CacheInterface $cache,
37
                ) use ($cacheEntity, &$ratingList): string {
1✔
38
                        [$ratingList, $ttl] = $this->buildCache($cacheEntity, $cache);
1✔
39
                        $dependency->ttl = $ttl;
1✔
40

41
                        return self::toDate($ratingList);
1✔
42
                });
1✔
43

44
                if ($ratingList === null) {
1✔
45
                        $ratingList = $this->cache->get($cacheEntity->cacheKeyAll);
1✔
46

47
                        if (($ratingList instanceof RatingListInterface) === false) {
1✔
48
                                throw new InvalidStateException('Cache is broken.');
×
49
                        }
50
                }
51

52
                return $ratingList;
1✔
53
        }
54

55

56
        /**
57
         * @return array{RatingListInterface, ?int}
58
         * @throws ClientExceptionInterface
59
         */
60
        private function buildCache(CacheEntity $cacheEntity, CacheInterface $cache): array
1✔
61
        {
62
                try {
63
                        $ratingList = $this->sourceDownload->execute($cacheEntity->source, $cacheEntity->date);
1✔
64
                } catch (ClientExceptionInterface $e) {
1✔
65
                        $ratingList = $cache->get($cacheEntity->cacheKeyAll);
1✔
66
                        if (($ratingList instanceof RatingListInterface) === false) {
1✔
67
                                throw $e;
1✔
68
                        }
69
                        $ratingList->getExpire()?->modify(sprintf('now, +%s seconds', Utils::CacheMinutes));
1✔
70
                }
71

72
                $ttl = $ratingList->getExpire() === null ? null : Utils::countTTL($ratingList->getExpire(), $this->floatTtl);
1✔
73
                $this->cache->set($cacheEntity->cacheKeyAll, $ratingList);
1✔
74

75
                return [$ratingList, $ttl];
1✔
76
        }
77

78

79
        /**
80
         * @throws ClientExceptionInterface
81
         */
82
        public function rebuild(CacheEntity $cacheEntity): bool
1✔
83
        {
84
                $oldValue = $this->cache->get($cacheEntity->cacheKeyTtl);
1✔
85
                [$ratingList, $ttl] = $this->buildCache($cacheEntity, $this->cache);
1✔
86
                $value = self::toDate($ratingList);
1✔
87
                $this->cache->set($cacheEntity->cacheKeyTtl, $value, $ttl);
1✔
88

89
                return $oldValue !== $value;
1✔
90
        }
91

92

93
        private static function toDate(RatingListInterface $ratingList): string
1✔
94
        {
95
                return $ratingList->getDate()->format(DATE_RFC3339);
1✔
96
        }
97
}
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