1 | <?php declare(strict_types=1); |
||
11 | class Queue implements IQueue |
||
12 | { |
||
13 | |||
14 | /** @var string[] */ |
||
15 | private static $tokens = []; |
||
16 | |||
17 | /** @var int */ |
||
18 | private $limitLoop = 5; |
||
19 | |||
20 | /** @var bool */ |
||
21 | private $sleep = true; |
||
22 | |||
23 | /** @var array */ |
||
24 | private $downloadOptions = []; |
||
25 | |||
26 | /** @var string */ |
||
27 | private $tempDir; |
||
28 | |||
29 | |||
30 | public function __construct(string $tempDir) |
||
31 | { |
||
32 | $this->tempDir = $tempDir; |
||
33 | } |
||
34 | |||
35 | |||
36 | public function setLimitLoop(int $limitLoop): void |
||
37 | { |
||
38 | $this->limitLoop = $limitLoop; |
||
39 | } |
||
40 | |||
41 | |||
42 | public function setDownloadOptions(iterable $downloadOptions): void |
||
43 | { |
||
44 | foreach ($downloadOptions as $define => $value) { |
||
45 | if (is_string($define) && defined($define)) { |
||
46 | $define = constant($define); |
||
47 | } |
||
48 | $this->downloadOptions[$define] = $value; |
||
49 | } |
||
50 | } |
||
51 | |||
52 | |||
53 | public function setSleep(bool $sleep): void |
||
54 | { |
||
55 | $this->sleep = $sleep; |
||
56 | } |
||
57 | |||
58 | |||
59 | /** |
||
60 | * @throws Exceptions\QueueLimit |
||
61 | * @throws Exceptions\ServiceUnavailable |
||
62 | */ |
||
63 | public function download(string $token, string $url): string |
||
64 | { |
||
65 | $response = $this->request($token, function (GuzzleHttp\ClientInterface $client) use ($url) { |
||
66 | return $client->request('GET', $url, $this->downloadOptions); |
||
67 | }); |
||
68 | $this->detectDownloadResponse($response); |
||
69 | return (string) $response->getBody(); |
||
70 | } |
||
71 | |||
72 | |||
73 | /** |
||
74 | * @throws Exceptions\QueueLimit |
||
75 | * @throws Exceptions\ServiceUnavailable |
||
76 | */ |
||
77 | public function upload(string $url, string $token, array $post, string $filename): Pay\IResponse |
||
78 | { |
||
79 | $newPost = []; |
||
80 | foreach ($post as $name => $value) { |
||
81 | $newPost[] = ['name' => $name, 'contents' => $value]; |
||
82 | } |
||
83 | $newPost[] = ['name' => 'file', 'contents' => fopen($filename, 'r')]; |
||
84 | |||
85 | $response = $this->request($token, function (GuzzleHttp\ClientInterface $client) use ($url, $newPost) { |
||
86 | return $client->request('POST', $url, [GuzzleHttp\RequestOptions::MULTIPART => $newPost]); |
||
87 | }); |
||
88 | return $this->createXmlResponse($response); |
||
89 | } |
||
90 | |||
91 | |||
92 | /** |
||
93 | * @throws Exceptions\QueueLimit |
||
94 | * @throws Exceptions\ServiceUnavailable() |
||
95 | */ |
||
96 | private function request(string $token, callable $fallback): ResponseInterface |
||
97 | { |
||
98 | $client = $this->createClient(); |
||
99 | $tempFile = $this->loadFileName($token); |
||
100 | $file = self::createFileResource($tempFile); |
||
101 | $i = 0; |
||
102 | do { |
||
103 | $next = false; |
||
|
|||
104 | ++$i; |
||
105 | try { |
||
106 | $response = $fallback($client); |
||
107 | fclose($file); |
||
108 | touch($tempFile); |
||
109 | return $response; |
||
110 | } catch (GuzzleHttp\Exception\ClientException $e) { |
||
111 | if ($e->getCode() !== self::HEADER_CONFLICT || !$this->sleep) { |
||
112 | fclose($file); |
||
113 | throw $e; |
||
114 | } elseif ($i >= $this->limitLoop) { |
||
115 | fclose($file); |
||
116 | throw new Exceptions\QueueLimit('You have limit up requests to server ' . $this->limitLoop); |
||
117 | } |
||
118 | self::sleep($tempFile); |
||
119 | $next = true; |
||
120 | } catch (GuzzleHttp\Exception\BadResponseException $e) { |
||
121 | throw new Exceptions\ServiceUnavailable($e->getMessage(), $e->getCode(), $e); |
||
122 | } |
||
123 | } while ($next); |
||
124 | } |
||
125 | |||
126 | |||
127 | private static function createFileResource(string $filePath) |
||
128 | { |
||
129 | $file = fopen(self::safeProtocol($filePath), 'w'); |
||
130 | if ($file === false) { |
||
131 | throw new Exceptions\InvalidState('Open file is failed ' . $filePath); |
||
132 | } |
||
133 | return $file; |
||
134 | } |
||
135 | |||
136 | |||
137 | /** |
||
138 | * @throws Exceptions\ServiceUnavailable() |
||
139 | */ |
||
140 | private function detectDownloadResponse(ResponseInterface $response): void |
||
141 | { |
||
142 | /* @var $contentTypeHeaders array */ |
||
143 | $contentTypeHeaders = $response->getHeader('Content-Type'); |
||
144 | $contentType = array_shift($contentTypeHeaders); |
||
145 | if ($contentType === 'text/xml;charset=UTF-8') { |
||
146 | $xmlResponse = $this->createXmlResponse($response); |
||
147 | if ($xmlResponse->code() !== 0) { |
||
148 | throw new Exceptions\ServiceUnavailable($xmlResponse->status(), $xmlResponse->code()); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | |||
154 | private static function sleep(string $filename): void |
||
161 | |||
162 | |||
163 | private function loadFileName(string $token): string |
||
164 | { |
||
165 | $key = substr($token, 10, -10); |
||
166 | if (!isset(self::$tokens[$key])) { |
||
167 | self::$tokens[$key] = $this->tempDir . DIRECTORY_SEPARATOR . md5($key); |
||
168 | } |
||
169 | |||
170 | return self::$tokens[$key]; |
||
172 | |||
173 | |||
174 | private static function safeProtocol(string $filename): string |
||
178 | |||
179 | |||
180 | protected function createXmlResponse(ResponseInterface $response): Pay\IResponse |
||
184 | |||
185 | |||
186 | protected function createClient(): GuzzleHttp\ClientInterface |
||
190 | |||
191 | } |
||
192 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.