Add PDF engine

This commit is contained in:
philipp lang 2021-07-15 21:17:48 +02:00
parent 7eeb88ce98
commit 618411d2e3
6 changed files with 161 additions and 6 deletions

34
app/Pdf/BillType.php Normal file
View File

@ -0,0 +1,34 @@
<?php
namespace App\Pdf;
use Illuminate\Support\Collection;
class BillType implements PdfRepository
{
public string $filename;
public function __construct(Collection $pages)
{
$this->pages = $pages;
}
public function getSubject(): string
{
return 'Rechnung';
}
public function setFilename(string $filename): self
{
$this->filename = $filename;
return $this;
}
public function getFilename(): string
{
return $this->filename;
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Pdf;
use App\Http\Controllers\Controller;
use App\Member\Member;
use Illuminate\Http\Request;
class MemberPdfController extends Controller
{
public function __invoke(Request $request, Member $member)
{
$repo = app(PdfRepositoryFactory::class)->fromSingleRequest($request->type, $member);
return $repo === null
? response()->noContent()
: app(PdfGenerator::class)->render($repo);
}
}

31
app/Pdf/PdfGenerator.php Normal file
View File

@ -0,0 +1,31 @@
<?php
namespace App\Pdf;
use Illuminate\Contracts\Support\Responsable;
use Storage;
class PdfGenerator implements Responsable
{
private ?string $filename = null;
public function render(PdfRepository $repo): self
{
$content = view()->make('pdf.pdf', [
'data' => $repo,
]);
$filename = $repo->getFilename();
dd($filename);
Storage::disk('temp')->put($repo->getBasename(), $content);
}
public function toResponse($request)
{
return response()->file($this->filename);
}
}

16
app/Pdf/PdfRepository.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace App\Pdf;
use Illuminate\Support\Collection;
interface PdfRepository
{
public function getSubject(): string;
public function setFilename(string $filename): self;
public function getFilename(): string;
}

View File

@ -0,0 +1,51 @@
<?php
namespace App\Pdf;
use App\Member\Member;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
class PdfRepositoryFactory
{
public function fromSingleRequest(string $type, Member $member): ?PdfRepository
{
$members = $this->singleMemberCollection($member);
if ($members->isEmpty()) {
return null;
}
$repo = $this->resolve($type, $members);
$firstMember = $members->first()->first();
return $repo->setFilename(
Str::slug("{$repo->getSubject()} für {$firstMember->firstname} {$firstMember->lastname}"),
);
}
public function singleMemberCollection(Member $member): Collection
{
$members = Member::where($member->only(['firstname', 'lastname', 'address', 'zip', 'location']))
->whereHas('payments', fn ($q) => $q->whereNeedsPayment())
->get();
return $this->toMemberGroup($members);
}
private function resolve(string $kind, Collection $members): PdfRepository
{
return new $kind($members);
}
private function toMemberGroup(Collection $members): Collection
{
return $members->groupBy(
fn ($member) => Str::slug(
"{$member->firstname}{$member->lastname}{$member->address}{$member->zip}{$member->location}",
),
);
}
}

View File

@ -1,12 +1,13 @@
<?php
use App\Member\MemberController;
use App\Payment\PaymentController;
use App\Payment\AllpaymentController;
use App\Payment\SubscriptionController;
use App\Member\MemberConfirmController;
use App\Http\Controllers\HomeController;
use App\Initialize\InitializeController;
use App\Member\MemberConfirmController;
use App\Member\MemberController;
use App\Payment\AllpaymentController;
use App\Payment\PaymentController;
use App\Payment\SubscriptionController;
use App\Pdf\MemberPdfController;
Route::group(['namespace' => 'App\\Http\\Controllers'], function() {
Auth::routes(['register' => false]);
@ -20,5 +21,6 @@ Route::group(['middleware' => 'auth:web'], function () {
Route::resource('allpayment', AllpaymentController::class);
Route::resource('subscription', SubscriptionController::class);
Route::post('/member/{member}/confirm', MemberConfirmController::class);
Route::post('/member/{member}/pdf', MemberPdfController::class)->name('member.singlepdf');
});