|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Library\Admin\Loader; |
|
4
|
|
|
|
|
5
|
|
|
use Leonidas\Contracts\Admin\Component\AdminNoticeLoaderInterface; |
|
6
|
|
|
use Leonidas\Contracts\Admin\Component\AdminNoticePrinterInterface; |
|
7
|
|
|
use Leonidas\Contracts\Admin\Component\AdminNoticeRepositoryInterface; |
|
8
|
|
|
use Leonidas\Library\Admin\Printer\DeferrableAdminNoticePrinter; |
|
9
|
|
|
use Leonidas\Library\Admin\Repository\AdminNoticeRepository; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
|
|
12
|
|
|
class AdminNoticeLoader implements AdminNoticeLoaderInterface |
|
13
|
|
|
{ |
|
14
|
|
|
protected AdminNoticeRepository $repository; |
|
15
|
|
|
|
|
16
|
|
|
protected ?AdminNoticePrinterInterface $printer = null; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(AdminNoticeRepositoryInterface $repository, ?AdminNoticePrinterInterface $printer = null) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->repository = $repository; |
|
21
|
|
|
$this->printer = $printer; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function print(ServerRequestInterface $request): string |
|
25
|
|
|
{ |
|
26
|
|
|
$notices = $this->repository->all() |
|
27
|
|
|
->getForScreen($request->getAttribute('screen')) |
|
28
|
|
|
->getForUser($request->getAttribute('user')) |
|
29
|
|
|
->toArray(); |
|
30
|
|
|
|
|
31
|
|
|
if (!$notices) { |
|
|
|
|
|
|
32
|
|
|
return ''; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$printer = new DeferrableAdminNoticePrinter($this->printer); |
|
36
|
|
|
$output = ''; |
|
37
|
|
|
|
|
38
|
|
|
foreach ($notices as $notice) { |
|
39
|
|
|
$output .= $printer->print($notice, $request); |
|
40
|
|
|
|
|
41
|
|
|
$this->repository->remove($notice->getId()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->repository->persist($request); |
|
45
|
|
|
|
|
46
|
|
|
return $output; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function printOne(string $notice, ServerRequestInterface $request): string |
|
50
|
|
|
{ |
|
51
|
|
|
$printer = new DeferrableAdminNoticePrinter($this->printer); |
|
52
|
|
|
|
|
53
|
|
|
return $printer->print($this->repository->get($notice), $request); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.