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

h4kuna / fio / 9697545364

27 Jun 2024 01:30PM UTC coverage: 90.75% (+0.4%) from 90.391%
9697545364

push

github

h4kuna
feat(RequestBlockingServiceContract): extract blocking to class

42 of 44 new or added lines in 3 files covered. (95.45%)

520 of 573 relevant lines covered (90.75%)

0.91 hits per line

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

96.43
/src/Utils/FileRequestBlockingService.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Fio\Utils;
4

5
use Closure;
6
use h4kuna\Dir\Dir;
7
use h4kuna\Fio\Contracts\RequestBlockingServiceContract;
8
use h4kuna\Fio\Exceptions\InvalidState;
9
use Nette\SafeStream\Wrapper;
10
use Psr\Http\Message\ResponseInterface;
11

12
final class FileRequestBlockingService implements RequestBlockingServiceContract
13
{
14
        /** @var array<string, string> */
15
        private static array $tokens = [];
16

17
        /**
18
         * @param int $waitTime - seconds
19
         */
20
        public function __construct(
1✔
21
                private Dir $tempDir,
22
                private int $waitTime = 31,
23
        ) {
24
        }
1✔
25

26
        public function synchronize(string $token, Closure $callback): ?ResponseInterface
1✔
27
        {
28
                $tempFile = $this->loadFileName($token);
1✔
29
                $file = self::createFileResource($tempFile);
1✔
30
                $this->sleep($tempFile);
1✔
31

32
                try {
33
                        $response = $callback();
1✔
34
                } finally {
1✔
35
                        fclose($file);
1✔
36
                        touch($tempFile);
1✔
37
                }
38

39
                return $response;
1✔
40
        }
41

42
        /**
43
         * @return resource
44
         */
45
        private static function createFileResource(string $filePath)
1✔
46
        {
47
                $file = fopen(self::safeProtocol($filePath), 'w');
1✔
48
                if ($file === false) {
1✔
NEW
49
                        throw new InvalidState('Open file is failed ' . $filePath);
×
50
                }
51

52
                return $file;
1✔
53
        }
54

55

56
        private function sleep(string $filename): void
1✔
57
        {
58
                $criticalTime = time() - intval(filemtime($filename));
1✔
59
                if ($criticalTime < $this->waitTime) {
1✔
60
                        sleep($this->waitTime - $criticalTime);
1✔
61
                }
62
        }
1✔
63

64

65
        private function loadFileName(string $token): string
1✔
66
        {
67
                $key = substr($token, 10, -10);
1✔
68
                if (!isset(self::$tokens[$key])) {
1✔
69
                        self::$tokens[$key] = $this->tempDir->filename(md5($key));
1✔
70
                }
71

72
                return self::$tokens[$key];
1✔
73
        }
74

75

76
        private static function safeProtocol(string $filename): string
1✔
77
        {
78
                return Wrapper::Protocol . "://$filename";
1✔
79
        }
80
}
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

© 2026 Coveralls, Inc