---
title: "DI a akcie — Money for Laravel | Roundly"
description: "Namiesto fasád injektujte ExchangeManager alebo kontrakt CurrencyRegistry, prípadne volajte tri akcie pre kurzy priamo s ich DTO."
url: https://roundly-consulting.com/sk/open-source/navody/money-for-laravel/bez-fasady
language: sk
---

[Všetky balíky](https://roundly-consulting.com/sk/open-source.md)

[Money for Laravel](https://roundly-consulting.com/sk/open-source/navody/money-for-laravel.md)

# DI a akcie

Fasády sú odporúčaná predvoľba, nie jediný vstup. K tomu istému kódu vedú tri rovnocenné vstupné body:

- Fasáda Exchange — najkratší zápis, ktorý používa celá táto dokumentácia.
- Manažér — RoundlyConsulting\\Money\\Exchange\\ExchangeManager, singleton v kontajneri a koreň fasády Exchange. Injektujte ho: rovnaké API, explicitná závislosť a žiadne statické volania.
- Akcie — RefreshExchangeRatesAction, StoreExchangeRatesAction a PruneExchangeRatesAction: jednoúčelové triedy s metódou execute(), ktoré skladáte do vlastných akcií, jobov a príkazov.

## Injektovanie manažéra

```php
use RoundlyConsulting\Money\Exchange\ExchangeManager;

final class RecordCzkRate
{
    public function __construct(private ExchangeManager $exchange) {}

    public function __invoke(string $rate): void
    {
        $this->exchange->rates()->manual('EUR', 'CZK', $rate); // same call as Exchange::rates()->manual()
    }
}
```

Exchange::fake() nahradí aj injektovanú inštanciu — fake dedí z ExchangeManager, takže trieda s type-hintom na manažér ho v testoch dostane namiesto TypeError. Pre meny injektujte kontrakt RoundlyConsulting\\Money\\Contracts\\CurrencyRegistry:

```php
use RoundlyConsulting\Money\Contracts\CurrencyRegistry;

public function __construct(private CurrencyRegistry $currencies) {}

$this->currencies->get('EUR')->exponent;   // 2 — the same singleton Currencies resolves
```

## Volanie akcie

```php
use RoundlyConsulting\Money\Actions\StoreExchangeRatesAction;
use RoundlyConsulting\Money\Exchange\ExchangeRate;

app(StoreExchangeRatesAction::class)->execute([
    ExchangeRate::fromDecimal('EUR', 'CZK', '25.10', now(), source: 'manual'),
]);
```

```php
use Carbon\CarbonImmutable;
use RoundlyConsulting\Money\Actions\PruneExchangeRatesAction;
use RoundlyConsulting\Money\Actions\RefreshExchangeRatesAction;
use RoundlyConsulting\Money\DataTransferObjects\PruneExchangeRatesData;
use RoundlyConsulting\Money\DataTransferObjects\RefreshExchangeRatesData;
use RoundlyConsulting\Money\Enums\EcbFeed;

$result = app(RefreshExchangeRatesAction::class)->execute(
    new RefreshExchangeRatesData('ecb', EcbFeed::Recent),
);
$result->stored;                   // rows written
$result->skippedManual;            // manual rows left untouched
$result->skippedUnknownCurrency;   // feed currencies the registry does not know
$result->latestDate;               // ?CarbonImmutable

$deleted = app(PruneExchangeRatesAction::class)->execute(
    new PruneExchangeRatesData(CarbonImmutable::now()->subYear()),
);
```

## Metóda fasády → akcia

| Metóda fasády | Akcia | Prijíma | Poznámka |
| --- | --- | --- | --- |
| `Exchange::rates()->refresh()` | `RefreshExchangeRatesAction` | `RefreshExchangeRatesData` | Beží hneď. |
| `Exchange::rates()->refreshLater()` | `RefreshExchangeRatesAction` | `RefreshExchangeRatesData` | Cez job RefreshExchangeRates vo fronte. |
| `Exchange::rates()->store(...$rates)` | `StoreExchangeRatesAction` | `iterable<ExchangeRate>` | Jediná cesta zápisu. |
| `Exchange::rates()->manual(…)` | `StoreExchangeRatesAction` | `iterable<ExchangeRate>` | Jeden kurz so zdrojom manual. |
| `Exchange::rates()->prune(…)` | `PruneExchangeRatesAction` | `PruneExchangeRatesData` | Vráti počet zmazaných alebo spočítaných riadkov. |

- RefreshExchangeRatesData prijíma source = 'ecb', feed = EcbFeed::Daily a voliteľné dátumy from a to; PruneExchangeRatesData prijíma before, includeManual = false a pretend = false.
- Aktualizácia aj uloženie vrátia RefreshResult (source, stored, skippedManual, skippedUnknownCurrency, latestDate); prune vráti int.
- Príkazy aj job RefreshExchangeRates vo fronte spúšťajú ten istý kód.
- Exchange::fake() zaznamená všetko, čo ide cez Exchange::rates() — na fasáde aj na injektovanom manažéri. Akcia, ktorú zavoláte priamo, sa vykoná naozaj.

## Prejavte lásku k open source

Tento balík je zadarmo pod licenciou MIT. Ak vám šetrí čas, jednorazový príspevok alebo členstvo na Patreone nám pomôže ho ďalej udržiavať, testovať a dokumentovať.

[Ďalšie spôsoby podpory vrátane kryptomien](https://roundly-consulting.com/sk/podporte-nas.md)

Odoslaním daru súhlasíte s našimi [podmienkami prijímania darov](https://roundly-consulting.com/sk/podmienky-darov.md).

[Podporte našu open-source prácu (otvorí sa v novej karte)](https://donate.stripe.com/dRmeVe8FX5PF1Qd9pXcEw00) [Pridajte sa k nám na Patreone (otvorí sa v novej karte)](https://www.patreon.com/cw/roundly)

## Chcete to zabudovať do svojho produktu?

Naše balíky integrujeme do zákazkových Laravel a AI riešení. Napíšte nám, na čom pracujete, a ozveme sa do 48 hodín.

[Ponuka do 48 hodín](https://roundly-consulting.com/sk/kontakt.md) [Prezrieť všetky balíky](https://roundly-consulting.com/sk/open-source/navody/money-for-laravel.md)
