Skip to content

Commit

Permalink
split up data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed May 4, 2022
1 parent a2a7fe6 commit 75dc993
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 40 deletions.
19 changes: 13 additions & 6 deletions src/Bridge/Nette/DI/InvoiceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Contributte\Invoice\Data\Account;
use Contributte\Invoice\Data\Company;
use Contributte\Invoice\Provider\InvoiceDataProvider;
use Contributte\Invoice\Provider\InvoiceAccountsProvider;
use Contributte\Invoice\Provider\InvoiceCompanyProvider;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Statement;
use Nette\Schema\Expect;
Expand Down Expand Up @@ -36,11 +37,17 @@ public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();

$builder->addDefinition($this->prefix('dataProvider'))
->setFactory(InvoiceDataProvider::class, [
$this->getCompany(),
$this->getAccounts(),
]);
$company = $this->getCompany();
if ($company) {
$builder->addDefinition($this->prefix('companyProvider'))
->setFactory(InvoiceCompanyProvider::class, [$company]);
}

$accounts = $this->getAccounts();
if ($accounts) {
$builder->addDefinition($this->prefix('accountsProvider'))
->setFactory(InvoiceAccountsProvider::class, [$accounts]);
}
}

private function getCompany(): ?Statement
Expand Down
41 changes: 41 additions & 0 deletions src/Provider/InvoiceAccountsProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php declare(strict_types = 1);

namespace Contributte\Invoice\Provider;

use BadMethodCallException;
use Contributte\Invoice\Data\Account;
use LogicException;
use Nette\Utils\Arrays;

final class InvoiceAccountsProvider
{

/** @var Account[] */
private array $accounts;

/**
* @param Account[] $accounts
*/
public function __construct(array $accounts)
{
if (!$accounts) {
throw new BadMethodCallException('Accounts array cannot be empty.');
}

$this->accounts = $accounts;
}

/**
* @return Account[]
*/
public function getAccounts(): array
{
return $this->accounts;
}

public function getAccount(): Account
{
return Arrays::first($this->accounts) ?? throw new LogicException('Unexpected error.');
}

}
21 changes: 21 additions & 0 deletions src/Provider/InvoiceCompanyProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace Contributte\Invoice\Provider;

use Contributte\Invoice\Data\ICompany;

final class InvoiceCompanyProvider
{

public function __construct(
private ICompany $company
)
{
}

public function getCompany(): ICompany
{
return $this->company;
}

}
34 changes: 0 additions & 34 deletions src/Provider/InvoiceDataProvider.php

This file was deleted.

0 comments on commit 75dc993

Please # to comment.