protected function loadQuotes() { if ($this->code === null) { throw new \BadMethodCallException('Initialize code'); } if ($this->count === null) { throw new \BadMethodCallException('Initialize count'); } if ($this->quotes === null) { $client = new YahooFinanceClient(); $start = date_create()->modify('-1 month'); $end = date_create(); $this->quotes = []; foreach (range(0, 23) as $month) { try { $data = $client->getHistoricalData($this->code, $start, $end); $this->quotes[] = $data['query']['results']['quote'][0]; } catch (ApiException $e) { // TODO should be logged? Throws an exception if earlier than certain date break; } $start->modify('-1 month'); $end->modify('-1 month'); } } return $this->quotes; }
/** * Fetch stock data for symbols * @param array $symbols * @param integer $try Number of this try */ private function fetchData($symbols, $try = 0) { try { return $this->api->getQuotes($symbols); } catch (ApiException $e) { //Yahoo Finance API is sometimes slow or doesn't return a result //Maximum of 3 retries if ($try < 3) { return $this->api->getQuotes($symbols, $try + 1); } } return null; }
/** * Search Yahoo Finance stocks * @param string $searchTerm * @return array */ public function search($searchTerm) { $data = $this->api->search($searchTerm); return $this->createResult($data); }