Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions 11.05/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
APP_NAME=

DB_HOST=
DB_USER=
DB_PASSWORD=
DB_DATABASE=
3 changes: 3 additions & 0 deletions 11.05/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
vendor/
.env
23 changes: 23 additions & 0 deletions 11.05/app/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Controllers;

use App\Services\QuoteService;

class HomeController
{
public function index()
{
$quote = null;

return require_once __DIR__ . '/../Views/IndexView.php';
}

public function search()
{
$service = new QuoteService();
$quote = $service->getQuote($_GET['symbol']);

return require_once __DIR__ . '/../Views/IndexView.php';
}
}
107 changes: 107 additions & 0 deletions 11.05/app/Models/Quote.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace App\Models;

class Quote
{
private string $symbol;
private float $regularPrice;
private float $regularChangePercent;
private float $previousClose;
private float $regularOpen;
private float $regularDayHigh;
private float $regularDayLow;
private float $fiftyTwoWeekHigh;
private float $fiftyTwoWeekLow;
private int $regularVolume;

public function __construct(
string $symbol,
float $regularPrice,
float $regularChangePercent,
float $previousClose,
float $regularOpen,
float $regularDayHigh,
float $regularDayLow,
float $fiftyTwoWeekHigh,
float $fiftyTwoWeekLow,
int $regularVolume
) {
$this->symbol = $symbol;
$this->regularPrice = $regularPrice;
$this->regularChangePercent = $regularChangePercent;
$this->previousClose = $previousClose;
$this->regularOpen = $regularOpen;
$this->regularDayHigh = $regularDayHigh;
$this->regularDayLow = $regularDayLow;
$this->fiftyTwoWeekHigh = $fiftyTwoWeekHigh;
$this->fiftyTwoWeekLow = $fiftyTwoWeekLow;
$this->regularVolume = $regularVolume;
}

public function getSymbol(): string
{
return $this->symbol;
}

public function getRegularPrice(): float
{
return $this->regularPrice;
}

public function getRegularChangePercent(): float
{
return $this->regularChangePercent;
}

public function getPreviousClose(): float
{
return $this->previousClose;
}

public function getRegularOpen(): float
{
return $this->regularOpen;
}

public function getRegularDayHigh(): float
{
return $this->regularDayHigh;
}

public function getRegularDayLow(): float
{
return $this->regularDayLow;
}

public function getFiftyTwoWeekHigh(): float
{
return $this->fiftyTwoWeekHigh;
}

public function getFiftyTwoWeekLow(): float
{
return $this->fiftyTwoWeekLow;
}

public function getRegularVolume(): int
{
return $this->regularVolume;
}

public static function create(array $data): self
{
return new self(
$data['symbol'],
(float) $data['regular_price'],
(float) $data['regular_change_percent'],
(float) $data['regular_previous_close'],
(float) $data['regular_open'],
(float) $data['regular_day_high'],
(float) $data['regular_day_low'],
(float) $data['fifty_two_week_high'],
(float) $data['fifty_two_week_low'],
(int) $data['regular_volume']
);
}
}
103 changes: 103 additions & 0 deletions 11.05/app/Repositories/DatabaseRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace App\Repositories;

use Carbon\Carbon;
use App\Models\Quote;

class DatabaseRepository
{
public function getBySymbol(string $symbol)
{
//Search for records
$data = query()
->select('*')
->from('quotes')
->where('symbol = :symbol')
->setParameter('symbol', $symbol)
->execute()
->fetchAssociative();

//In case the record does not exist, create a record
if (! $data) {
$quote = (new YahooRepository())->getBySymbol($symbol);
$this->createRecord($quote);
return $quote;
}

//In case the record is outdated, update record
if (Carbon::now()->diffInMinutes($data['created_at']) > 10) {
$quote = (new YahooRepository())->getBySymbol($symbol);
$this->updateRecord($quote);

return $quote;
}

return Quote::create($data);
}

public function createRecord(Quote $quote): void
{
query()
->insert('quotes')
->values([
'symbol' => ':symbol',
'regular_price' => ':regular_price',
'regular_change_percent' => ':regular_change_percent',
'regular_previous_close' => ':regular_previous_close',
'regular_open' => ':regular_open',
'regular_day_high' => ':regular_day_high',
'regular_day_low' => ':regular_day_low',
'fifty_two_week_high' => ':fifty_two_week_high',
'fifty_two_week_low' => ':fifty_two_week_low',
'regular_volume' => ':regular_volume',
'created_at' => ':created_at'
])
->setParameters([
'symbol' => $quote->getSymbol(),
'regular_price' => $quote->getRegularPrice(),
'regular_change_percent' => $quote->getRegularChangePercent(),
'regular_previous_close' => $quote->getRegularChangePercent(),
'regular_open' => $quote->getRegularOpen(),
'regular_day_high' => $quote->getRegularDayHigh(),
'regular_day_low' => $quote->getRegularDayLow(),
'fifty_two_week_high' => $quote->getFiftyTwoWeekHigh(),
'fifty_two_week_low' => $quote->getFiftyTwoWeekLow(),
'regular_volume' => $quote->getRegularVolume(),
'created_at' => Carbon::now()->format('Y-m-d h:i:s')
])
->execute();
}

public function updateRecord(Quote $quote): void
{
query()
->update('quotes')
->set('symbol', ':symbol')
->set('regular_price', ':regular_price')
->set('regular_change_percent', ':regular_change_percent')
->set('regular_previous_close', ':regular_previous_close')
->set('regular_open', ':regular_open')
->set('regular_day_high', ':regular_day_high')
->set('regular_day_low', ':regular_day_low')
->set('fifty_two_week_high', ':fifty_two_week_high')
->set('fifty_two_week_low', ':fifty_two_week_low')
->set('regular_volume', ':regular_volume')
->set('created_at', ':created_at')
->setParameters([
'regular_price' => $quote->getRegularPrice(),
'regular_change_percent' => $quote->getRegularChangePercent(),
'regular_previous_close' => $quote->getRegularChangePercent(),
'regular_open' => $quote->getRegularOpen(),
'regular_day_high' => $quote->getRegularDayHigh(),
'regular_day_low' => $quote->getRegularDayLow(),
'fifty_two_week_high' => $quote->getFiftyTwoWeekHigh(),
'fifty_two_week_low' => $quote->getFiftyTwoWeekLow(),
'regular_volume' => $quote->getRegularVolume(),
'created_at' => Carbon::now()->format('Y-m-d h:i:s')
])
->where('symbol = :symbol')
->setParameter('symbol', $quote->getSymbol())
->execute();
}
}
32 changes: 32 additions & 0 deletions 11.05/app/Repositories/YahooRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Repositories;

use App\Models\Quote;
use Scheb\YahooFinanceApi\ApiClientFactory;

class YahooRepository
{
public function getBySymbol(string $symbol): ?Quote
{
$client = ApiClientFactory::createApiClient();
$quoteAPI = $client->getQuote($symbol);

if($quoteAPI->getSymbol() === $symbol) {
return new Quote(
$quoteAPI->getSymbol(),
$quoteAPI->getRegularMarketPrice(),
$quoteAPI->getRegularMarketChangePercent(),
$quoteAPI->getRegularMarketPreviousClose(),
$quoteAPI->getRegularMarketOpen(),
$quoteAPI->getRegularMarketDayHigh(),
$quoteAPI->getRegularMarketDayLow(),
$quoteAPI->getFiftyTwoWeekHigh(),
$quoteAPI->getFiftyTwoWeekLow(),
$quoteAPI->getRegularMarketVolume()
);
} else {
return null;
}
}
}
14 changes: 14 additions & 0 deletions 11.05/app/Services/QuoteService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Services;

use App\Repositories\DatabaseRepository;

class QuoteService
{
public function getQuote(string $symbol)
{
$repository = new DatabaseRepository();
return $repository->getBySymbol($symbol);
}
}
3 changes: 3 additions & 0 deletions 11.05/app/Views/FooterView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

</body>
</html>
9 changes: 9 additions & 0 deletions 11.05/app/Views/HeaderView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>

</head>
<body>
<header>
<nav>
</nav>
</header>
45 changes: 45 additions & 0 deletions 11.05/app/Views/IndexView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php require_once __DIR__ . '/../Views/HeaderView.php' ?>

<h1>Yahoo Finance API</h1>

<form action="/search" method="GET">
<label for="symbol">Search</label>
<input type="text" id="symbol" name="symbol" required>

<button type="submit">Search</button>
</form>

<?php if ($quote): ?>
<div>
<h2>Stock for <?php echo $quote->getSymbol() ?></h2>
<div>
Regular price <?php echo $quote->getRegularPrice() ?>
</div>
<div>
Regular change percent <?php echo $quote->getRegularChangePercent() ?>%
</div>
<div>
Market Previous close <?php echo $quote->getPreviousClose() ?>
</div>
<div>
Market Regular Open <?php echo $quote->getRegularOpen() ?>
</div>
<div>
Market Regular Day High <?php echo $quote->getRegularDayHigh() ?>
</div>
<div>
Market Regular Day Low <?php echo $quote->getRegularDayLow() ?>
</div>
<div>
Market 52 week High <?php echo $quote->getFiftyTwoWeekHigh() ?>
</div>
<div>
Market 52 week Low <?php echo $quote->getFiftyTwoWeekLow() ?>
</div>
<div>
Market Regular Day Volume <?php echo $quote->getRegularVolume() ?>
</div>
</div>
<?php endif; ?>

<?php require_once __DIR__ . '/../Views/FooterView.php' ?>
15 changes: 15 additions & 0 deletions 11.05/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
"require": {
"nikic/fast-route": "^1.3",
"doctrine/dbal": "^2.12",
"vlucas/phpdotenv": "^5.2",
"scheb/yahoo-finance-api": "^4.0",
"nesbot/carbon": "^2.41"
},
"minimum-stability": "stable"
}
Loading