getUF($dateTime); } $class = __CLASS__; throw new EmptyResponse("{$money_symbol} not found in {$class}"); } /** * @param DateTimeInterface|null $dateTime * @return float * @throws EmptyResponse */ protected function getUF(?DateTimeInterface $dateTime = null): float { if ($dateTime === null) { $dateTime = new DateTimeImmutable(); } $year = $this->getUFYear($dateTime); if (!isset($year[$dateTime->format('Y-m-d')])) { throw new EmptyResponse("{$dateTime->format('Y-m-d')} not found"); } return $year[$dateTime->format('Y-m-d')]; } /** * @param DateTimeImmutable|null $dateTime * @return array * @throws EmptyResponse */ protected function getUFYear(?DateTimeImmutable $dateTime = null): array { if ($dateTime === null) { $dateTime = new DateTimeImmutable(); } if ($dateTime->format('Y') > (new DateTimeImmutable())->format('Y')) { throw new EmptyResponse("{$dateTime->format('Y')} not found"); } $request_uri = "uf/uf{$dateTime->format('Y')}.htm"; try { $response = $this->client->get($request_uri); } catch (GuzzleException) { throw new EmptyResponse($request_uri); } if ((int) floor($response->getStatusCode() / 100) !== 2) { throw new EmptyResponse($request_uri); } $body = $response->getBody(); $contents = $body->getContents(); if (trim($contents) === '') { throw new EmptyResponse($request_uri); } $domHandler = HTMLDocument::createFromString($contents); $table = $domHandler->querySelector('div#mes_all'); $tbody = $table->querySelector('tbody'); $trs = $tbody->querySelectorAll('tr'); /** * [th Dia, th Ene, Feb, Mar, Abr, May, Jun, Jul, Ago, Sep, Oct, Nov, Dic] * [th 1, td #, #, #, #, #, #, #, #, #, #, #, #] */ $year = []; $trim = mb_chr(160); foreach ($trs as $i => $tr) { $tds = $tr->querySelectorAll('td'); foreach ($tds as $j => $td) { $value = $td->textContent; if (trim($value, "{$trim} ") === '') { continue; } $m = str_pad($j + 1, 2, '0', STR_PAD_LEFT); $d = str_pad($i + 1, 2, '0', STR_PAD_LEFT); $date = "{$dateTime->format('Y')}-{$m}-{$d}"; $value = $this->clean($value); $year[$date] = $value; } } $this->saveUFs($year); return $year; } protected function saveUFs(array $ufs): void { $dates = array_keys($ufs); $dateString = "'" . implode("', '", $dates) . "'"; $query1 = $this->ufRepository->getConnection()->getQueryBuilder() ->select('DISTINCT fecha') ->from($this->ufRepository->getTable()) ->where("fecha IN ({$dateString})"); try { $statement = $this->ufRepository->getConnection()->query($query1); $results = $statement->fetchAll(PDO::FETCH_ASSOC); foreach ($results as $row) { if (isset($ufs[$row['fecha']])) { unset($ufs[$row['fecha']]); } } } catch (PDOException) { return; } $values = []; foreach ($ufs as $fecha => $value) { $values []= [$fecha, $value]; } $valueString = implode(', ', array_fill(0, count($values), '(?, ?)')); $query2 = "INSERT INTO {$this->ufRepository->getTable()} (fecha, valor) VALUES {$valueString}"; $this->ufRepository->getConnection()->getPDO()->beginTransaction(); try { $this->ufRepository->getConnection()->execute($query2, $values); if ($this->ufRepository->getConnection()->getPDO()->inTransaction()) { $this->ufRepository->getConnection()->getPDO()->commit(); } } catch (PDOException) { if ($this->ufRepository->getConnection()->getPDO()->inTransaction()) { $this->ufRepository->getConnection()->getPDO()->rollBack(); } } } protected function clean(string $value): float { return trim(str_replace(',', '.', str_replace(['.', '$'], '', $value))); } }