Test Failed
Push — master ( 135f34...8b25c6 )
by Chris
33:49
created

AdminNoticeLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $notices of type Leonidas\Contracts\Admin...\AdminNoticeInterface[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
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