Skip to content

Commit

Permalink
Invoice: add methods send, createResponse. generatePreview moved to P…
Browse files Browse the repository at this point in the history
…reviewFactory
  • Loading branch information
MartkCz committed Feb 27, 2019
1 parent 0cd19ab commit 6670823
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 17 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,32 @@ class CustomFormatter implements IFormatter {
```php
$invoice = new WebChemistry\Invoice\Invoice($company);

header('Content-Type: application/pdf');
header('Content-Type: application/pdf; charset=utf-8');
echo $invoice->create($customer, $order);
```

shorter
```php
$invoice = new WebChemistry\Invoice\Invoice($company);

$invoice->send($customer, $order);
```

nette framework way
```php
$invoice = new WebChemistry\Invoice\Invoice($company);

$this->sendResponse($invoice->createResponse($customer, $order));
```

## Generating preview

```php
header('Content-Type: application/pdf');
echo $invoice->generatePreview();
$invoice->send(WebChemistry\Invoice\Preview\PreviewFactory::createCustomer(), WebChemistry\Invoice\Preview\PreviewFactory::createOrder());
```

```php

```

## Nette DI
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"require-dev": {
"nette/di": "^3.0",
"nette/application": "^3.0",
"codeception/codeception": "^2.4"
},
"autoload": {
Expand Down
24 changes: 10 additions & 14 deletions src/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace WebChemistry\Invoice;

use Nette\Application\IResponse;
use Nette\SmartObject;
use WebChemistry\Invoice\Calculators\FloatCalculator;
use WebChemistry\Invoice\Calculators\ICalculator;
Expand All @@ -14,6 +15,7 @@
use WebChemistry\Invoice\Data\PaymentInformation;
use WebChemistry\Invoice\Renderers\IRenderer;
use WebChemistry\Invoice\Renderers\PDFRenderer;
use WebChemistry\Invoice\Responses\PdfResponse;
use WebChemistry\Invoice\Templates\DefaultTemplate;
use WebChemistry\Invoice\Templates\ITemplate;

Expand All @@ -40,24 +42,18 @@ public function __construct(Company $company, ?ITemplate $template = null, ?IRen
$this->calculator = $calculator ?: new FloatCalculator();
}

public function generatePreview(): string {
$tax = $this->company->hasTax() ? 0.21 : null;
$customer = new Customer('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA', '08304431', 'CZ08304431');
$account = new Account('2353462013/0800', 'CZ4808000000002353462013', 'GIGACZPX');
$paymentInfo = new PaymentInformation('$', '0123456789', '1234', $tax);
public function create(Customer $customer, Order $order): string {
return $this->template->build($this->calculator, $this->renderer, $customer, $order, $this->company);
}

$order = new Order(date('Y') . '0001', new \DateTime('+ 7 days'), $account, $paymentInfo);
$order->addItem('Logitech G700s Rechargeable Gaming Mouse', 1790, 4);
$order->addItem('ASUS Z380KL 8" - 16GB, LTE, bílá', 6490, 1);
$order->addItem('Philips 48PFS6909 - 121cm', 13990, 1);
$order->addItem('HP Deskjet 3545 Advantage', 1799, 1);
$order->addItem('LG 105UC9V - 266cm', 11599, 2);
public function send(Customer $customer, Order $order): void {
header('Content-type: application/pdf');

return $this->create($customer, $order);
echo $this->create($customer, $order);
}

public function create(Customer $customer, Order $order): string {
return $this->template->build($this->calculator, $this->renderer, $customer, $order, $this->company);
public function createResponse(Customer $customer, Order $order): IResponse {
return new PdfResponse($this->create($customer, $order));
}

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

namespace WebChemistry\Invoice\Preview;

use DateTime;
use WebChemistry\Invoice\Data\Account;
use WebChemistry\Invoice\Data\Customer;
use WebChemistry\Invoice\Data\Order;
use WebChemistry\Invoice\Data\PaymentInformation;

final class PreviewFactory {

public static function createCustomer(): Customer {
return new Customer('John Doe', 'Los Angeles', 'Cavetown', '720 55', 'USA', '08304431', 'CZ08304431');
}

public static function createOrder(): Order {
$account = new Account('2353462013/0800', 'CZ4808000000002353462013', 'GIGACZPX');
$paymentInfo = new PaymentInformation('$', '0123456789', '1234', 0.21);

$order = new Order(date('Y') . '0001', new DateTime('+ 7 days'), $account, $paymentInfo);
$order->addItem('Logitech G700s Rechargeable Gaming Mouse', 1790, 4);
$order->addItem('ASUS Z380KL 8" - 16GB, LTE, bílá', 6490, 1);
$order->addItem('Philips 48PFS6909 - 121cm', 13990, 1);
$order->addItem('HP Deskjet 3545 Advantage', 1799, 1);
$order->addItem('LG 105UC9V - 266cm', 11599, 2);

return $order;
}

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

namespace WebChemistry\Invoice\Responses;

use Nette\Application\IResponse;
use Nette\Http\IRequest;
use Nette\Http\IResponse as NetteIResponse;

final class PdfResponse implements IResponse {

/** @var string */
private $content;

public function __construct(string $content) {
$this->content = $content;
}

public function send(IRequest $httpRequest, NetteIResponse $httpResponse): void {
$httpResponse->setContentType('application/pdf', 'utf-8');
echo $this->content;
}

}

0 comments on commit 6670823

Please sign in to comment.