@@ -49,13 +49,13 @@ public function convert(string $fromCurrency, string $toCurrency, CarbonInterfac
4949 }
5050 // aliases.
5151 $ strDate = $ date ->format ('Y-m-d ' );
52- // has not rates?
53- if (0 === $ this ->count (['date ' => $ date ])) {
54- $ this ->service ->updateDailyExchangeRates (date: $ date );
55- }
5652 // fromCurrency to USD.
5753 if ($ fromCurrency !== 'USD ' ) {
5854 try {
55+ // has not rates?
56+ if (!$ this ->has ($ fromCurrency , $ date )) {
57+ $ this ->service ->updateDailyExchangeRates (date: $ date );
58+ }
5959 $ fromAmount = (float ) $ this ->createQueryBuilder ('dailyExchangeRate ' )
6060 ->select ('dailyExchangeRate.value ' )
6161 ->innerJoin ('dailyExchangeRate.currency ' , 'currency ' )
@@ -68,14 +68,18 @@ public function convert(string $fromCurrency, string $toCurrency, CarbonInterfac
6868 ->getQuery ()
6969 ->getSingleScalarResult ();
7070 } catch (NoResultException |NonUniqueResultException ) {
71- throw new EntityNotFoundException (' From currency not found. ' );
71+ throw new EntityNotFoundException (" From currency ( $ fromCurrency ) not found." );
7272 }
7373 } else {
7474 $ fromAmount = 1 ;
7575 }
7676 // USD to toCurrency.
7777 if ($ toCurrency !== 'USD ' ) {
7878 try {
79+ // has not rates?
80+ if (!$ this ->has ($ toCurrency , $ date )) {
81+ $ this ->service ->updateDailyExchangeRates (date: $ date );
82+ }
7983 $ toAmount = (float ) $ this ->createQueryBuilder ('dailyExchangeRate ' )
8084 ->select ('dailyExchangeRate.value ' )
8185 ->innerJoin ('dailyExchangeRate.currency ' , 'currency ' )
@@ -88,7 +92,7 @@ public function convert(string $fromCurrency, string $toCurrency, CarbonInterfac
8892 ->getQuery ()
8993 ->getSingleScalarResult ();
9094 } catch (NoResultException |NonUniqueResultException ) {
91- throw new EntityNotFoundException (' To currency not found. ' );
95+ throw new EntityNotFoundException (" To currency ( $ toCurrency ) not found." );
9296 }
9397 } else {
9498 $ toAmount = 1 ;
@@ -112,4 +116,23 @@ public function findOldDate(): ?CarbonInterface
112116
113117 return $ data ? Carbon::createFromFormat ('Y-m-d ' , $ data ) : null ;
114118 }
119+
120+ /**
121+ * @throws NonUniqueResultException
122+ * @throws NoResultException
123+ */
124+ private function has (string $ currency , CarbonInterface $ date ): bool
125+ {
126+ return $ this ->createQueryBuilder ('dailyExchangeRate ' )
127+ ->select ('COUNT(dailyExchangeRate.id) ' )
128+ ->innerJoin ('dailyExchangeRate.currency ' , 'currency ' )
129+ ->where ('currency.code = :currency ' )
130+ ->andWhere ('dailyExchangeRate.date = :date ' )
131+ ->setParameters ([
132+ 'currency ' => $ currency ,
133+ 'date ' => $ date ->format ('Y-m-d ' ),
134+ ])
135+ ->getQuery ()
136+ ->getSingleScalarResult () > 0 ;
137+ }
115138}
0 commit comments