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
3 changes: 3 additions & 0 deletions 11.04/currencies/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
vendor/
.env
24 changes: 24 additions & 0 deletions 11.04/currencies/app/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Controllers;

use App\Services\CurrencyService;

class HomeController
{
public function index()
{
$service = new CurrencyService();
$currencies = $service->getAll();

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

public function update()
{
$service = new CurrencyService();
$service->updateAll();

return header('Location: /');
}
}
38 changes: 38 additions & 0 deletions 11.04/currencies/app/Models/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Models;

abstract class Collection
{
protected $model;
protected array $models = [];

public function __construct(array $models)
{
foreach ($models as $model) {
$this->add($this->model::create($model));
}
}

public function all(): array
{
return $this->models;
}

public function add(ModelInterface $model): void
{
$this->models[] = $model;
}

public function pluck(string $parameter): array
{
$parameters = [];

foreach ($this->all() as $model) {
$parameters[] = $model->$parameter();
}

return $parameters;
}

}
33 changes: 33 additions & 0 deletions 11.04/currencies/app/Models/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Models;

class Currency implements ModelInterface
{
private string $currency;
private float $rate;

public function __construct(string $currency, float $rate)
{
$this->currency = $currency;
$this->rate = $rate;
}

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

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

public static function create(array $data): Currency
{
return new self(
$data['currency'],
(float) $data['rate']
);
}
}
8 changes: 8 additions & 0 deletions 11.04/currencies/app/Models/CurrencyCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Models;

class CurrencyCollection extends Collection
{
protected $model = Currency::class;
}
8 changes: 8 additions & 0 deletions 11.04/currencies/app/Models/ModelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Models;

interface ModelInterface
{
public static function create(array $data): ModelInterface;
}
88 changes: 88 additions & 0 deletions 11.04/currencies/app/Repositories/CurrencyRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace App\Repositories;

use App\Models\Currency;
use App\Models\CurrencyCollection;
use Sabre\Xml\Service;

class CurrencyRepository
{
public function getCurrencies(): CurrencyCollection
{
if ($this->isEmpty()) $this->createDB($this->getCurrenciesFromBank());

$query = query()
->select('*')
->from('currencies')
->execute()
->fetchAllAssociative();

return new CurrencyCollection($query);
}

public function updateCurrencies(): void
{
$currencies = $this->getCurrenciesFromBank();

/** @var Currency $currency */
foreach ($currencies->all() as $currency) {
query()
->update('currencies')
->set('rate', $currency->getRate())
->where('currency = :currency')
->setParameter('currency', $currency->getCurrency())
->execute();
}
}

private function getCurrenciesFromBank(): CurrencyCollection
{
$xml = file_get_contents('https://www.bank.lv/vk/ecb.xml');

$service = new Service();
$result = $service->parse($xml);

$currenciesRaw = $result[1]['value'];

$data = [];

foreach ($currenciesRaw as $currencyRaw) {
$data[] = [
'currency' => $currencyRaw['value'][0]['value'],
'rate' => (float) $currencyRaw['value'][1]['value']
];
}

return new CurrencyCollection($data);
}

private function isEmpty(): bool
{
$query = query()
->select('*')
->from('currencies')
->execute()
->fetchAllAssociative();

return count($query) === 0;
}

private function createDB(CurrencyCollection $currencies): void
{
/** @var Currency $currency */
foreach ($currencies->all() as $currency) {
query()
->insert('currencies')
->values([
'currency' => ':currency',
'rate' => ':rate'
])
->setParameters([
'currency' => $currency->getCurrency(),
'rate' => $currency->getRate(),
])
->execute();
}
}
}
23 changes: 23 additions & 0 deletions 11.04/currencies/app/Services/CurrencyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Services;

use App\Models\CurrencyCollection;
use App\Repositories\CurrencyRepository;

class CurrencyService
{
public function getAll(): CurrencyCollection
{
$repository = new CurrencyRepository();

return $repository->getCurrencies();
}

public function updateAll(): void
{
$repository = new CurrencyRepository();

$repository->updateCurrencies();
}
}
3 changes: 3 additions & 0 deletions 11.04/currencies/app/Views/FooterView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

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

</head>
<body>
15 changes: 15 additions & 0 deletions 11.04/currencies/app/Views/HomeIndexView.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php require_once __DIR__ . '/../Views/HeaderView.php' ?>

<h1>Currencies</h1>

<form action="/currencies" method="POST">
<button type="submit">UPDATE CURRENCIES!</button>
</form>

<?php foreach ($currencies->all() as $currency): ?>
<h3>
<?php echo $currency->getCurrency() . ': ' . $currency->getRate(); ?>
</h3>
<?php endforeach; ?>

<?php require_once __DIR__ . '/../Views/FooterView.php' ?>
18 changes: 18 additions & 0 deletions 11.04/currencies/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "kem4/codelex",
"autoload": {
"psr-4": {
"App\\": "app/"
}
},
"require": {
"nikic/fast-route": "^1.3",
"doctrine/dbal": "^2.12",
"vlucas/phpdotenv": "^5.2",
"respect/validation": "^2.1",
"imangazaliev/didom": "^1.16",
"sabre/xml": "^2.2",
"nesbot/carbon": "^2.41"
},
"minimum-stability": "stable"
}
Loading