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

h4kuna / exchange / 10609676382

29 Aug 2024 06:09AM UTC coverage: 97.552% (-0.3%) from 97.872%
10609676382

push

github

h4kuna
feat(example): real

279 of 286 relevant lines covered (97.55%)

0.98 hits per line

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

95.45
/src/ExchangeFactory.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Exchange;
4

5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\HttpFactory;
7
use h4kuna\CriticalCache\PSR16\CacheLockingFactoryInterface;
8
use h4kuna\CriticalCache\PSR16\Locking\CacheLockingFactory;
9
use h4kuna\Dir\Dir;
10
use h4kuna\Exchange\Download\SourceDownload;
11
use h4kuna\Exchange\Exceptions\MissingDependencyException;
12
use h4kuna\Exchange\RatingList\CacheEntity;
13
use h4kuna\Exchange\RatingList\RatingListCache;
14
use Psr\Http\Client\ClientInterface;
15
use Psr\Http\Message\RequestFactoryInterface;
16

17
final class ExchangeFactory implements ExchangeFactoryInterface
18
{
19
        private RatingListCache $ratingListCache;
20

21
        private CacheEntity $cacheEntity;
22

23

24
        /**
25
         * @param array<string> $allowedCurrencies
26
         */
27
        public function __construct(
1✔
28
                private string $from = 'CZK',
29
                private ?string $to = null,
30
                ?RatingListCache $ratingListCache = null,
31
                array $allowedCurrencies = [],
32
                ?ClientInterface $client = null,
33
                ?RequestFactoryInterface $requestFactory = null,
34
                ?CacheEntity $cacheEntity = null,
35
                string|Dir|CacheLockingFactoryInterface $tempDir = 'exchange',
36
        )
37
        {
38
                $this->cacheEntity = $cacheEntity ?? new CacheEntity();
1✔
39
                $this->ratingListCache = $ratingListCache ?? self::createRatingListCache(Utils::transformCurrencies($allowedCurrencies), $client, $requestFactory, $tempDir);
1✔
40
        }
1✔
41

42

43
        /**
44
         * @param array<string, int> $allowedCurrencies
45
         */
46
        private static function createRatingListCache(
1✔
47
                array $allowedCurrencies,
48
                ?ClientInterface $client,
49
                ?RequestFactoryInterface $requestFactory,
50
                string|Dir|CacheLockingFactoryInterface $tempDir,
51
        ): RatingListCache
52
        {
53
                $cacheLockingFactory = $tempDir instanceof CacheLockingFactoryInterface
1✔
54
                        ? $tempDir
×
55
                        : self::createCacheFactory($tempDir);
1✔
56

57
                return new RatingListCache(
1✔
58
                        $cacheLockingFactory->create(),
1✔
59
                        new SourceDownload($client ?? self::createClient(), $requestFactory ?? self::createRequestFactory(), $allowedCurrencies),
1✔
60
                );
61
        }
62

63

64
        public function create(
1✔
65
                ?string $from = null,
66
                ?string $to = null,
67
                ?CacheEntity $cacheEntity = null,
68
        ): Exchange
69
        {
70
                return new Exchange(
1✔
71
                        $from ?? $this->from,
1✔
72
                        $this->ratingListCache->build($cacheEntity ?? $this->cacheEntity),
1✔
73
                        $to ?? $this->to,
1✔
74
                );
75
        }
76

77

78
        private static function createCacheFactory(string|Dir $tempDir): CacheLockingFactoryInterface
1✔
79
        {
80
                return new CacheLockingFactory($tempDir);
1✔
81
        }
82

83

84
        private static function createClient(): ClientInterface
85
        {
86
                MissingDependencyException::guzzleClient();
1✔
87
                return new Client();
1✔
88
        }
89

90

91
        private static function createRequestFactory(): RequestFactoryInterface
92
        {
93
                MissingDependencyException::guzzleFactory();
1✔
94
                return new HttpFactory();
1✔
95
        }
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