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

h4kuna / fio / 10668219380

02 Sep 2024 01:35PM UTC coverage: 90.767% (+0.02%) from 90.75%
10668219380

push

github

h4kuna
feat(command): rename phpstan -> stan

521 of 574 relevant lines covered (90.77%)

0.91 hits per line

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

96.55
/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

27
        public function synchronize(string $token, Closure $callback): ?ResponseInterface
1✔
28
        {
29
                $tempFile = $this->loadFileName($token);
1✔
30
                $file = self::createFileResource($tempFile);
1✔
31
                $sleep = $this->waitTime - (time() - ((int) fgets($file, 30)));
1✔
32
                if ($sleep > 0) {
1✔
33
                        sleep($sleep);
1✔
34
                }
35

36
                try {
37
                        $response = $callback();
1✔
38
                } finally {
1✔
39
                        fseek($file, 0);
1✔
40
                        ftruncate($file, 0);
1✔
41
                        fputs($file, (string) time());
1✔
42
                        fclose($file);
1✔
43
                }
44

45
                return $response;
1✔
46
        }
47

48
        /**
49
         * @return resource
50
         */
51
        private static function createFileResource(string $filePath)
1✔
52
        {
53
                if (is_file($filePath) === false) {
1✔
54
                        touch($filePath);
1✔
55
                }
56
                $file = fopen(self::safeProtocol($filePath), 'r+');
1✔
57
                if ($file === false) {
1✔
58
                        throw new InvalidState('Open file is failed ' . $filePath);
×
59
                }
60

61
                return $file;
1✔
62
        }
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

© 2025 Coveralls, Inc