ViewDonor::__invoke()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 17
nc 7
nop 0
dl 0
loc 30
rs 8.4444
c 0
b 0
f 0
1
<?php
2
/**
3
 * View donor page controller class file
4
 *
5
 * @package    EBloodBank
6
 * @subpackage Controllers
7
 * @since      1.1
8
 */
9
namespace EBloodBank\Controllers;
10
11
use EBloodBank as EBB;
12
use EBloodBank\Notices;
13
use Psr\Container\ContainerInterface;
14
15
/**
16
 * View donor page controller class
17
 *
18
 * @since 1.1
19
 */
20
class ViewDonor extends Controller
21
{
22
    /**
23
     * @var int
24
     * @since 1.6
25
     */
26
    protected $donorId = 0;
27
28
    /**
29
     * @var \EBloodBank\Models\Donor|nul
0 ignored issues
show
Bug introduced by
The type EBloodBank\Controllers\nul was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
     * @since 1.1
31
     */
32
    protected $donor;
33
34
    /**
35
     * @since 1.1
36
     */
37
    public function __construct(ContainerInterface $container, $donorId)
38
    {
39
        parent::__construct($container);
40
        if (EBB\isValidID($donorId)) {
0 ignored issues
show
Bug introduced by
The function isValidID was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

40
        if (/** @scrutinizer ignore-call */ EBB\isValidID($donorId)) {
Loading history...
41
            $this->donorId = (int) $donorId;
42
        }
43
    }
44
45
    /**
46
     * @return void
47
     * @since 1.1
48
     */
49
    public function __invoke()
50
    {
51
        $isSitePublic = ('on' === EBB\Options::getOption('site_publication'));
52
53
        if (! $isSitePublic && (! $this->hasAuthenticatedUser() || ! $this->getAcl()->isUserAllowed($this->getAuthenticatedUser(), 'Donor', 'read'))) {
54
            $this->viewFactory->displayView('error-403');
55
            return;
56
        }
57
58
        if ($this->donorId) {
59
            $this->donor = $this->getDonorRepository()->find($this->donorId);
60
        }
61
62
        if (! $this->donor) {
63
            $this->viewFactory->displayView('error-404');
64
            return;
65
        }
66
67
        $donor = $this->donor;
68
69
        if ($this->hasAuthenticatedUser() && ! $this->getAcl()->canReadEntity($this->getAuthenticatedUser(), $donor)) {
70
            $this->viewFactory->displayView('error-403');
71
            return;
72
        }
73
74
        $this->addNotices();
75
        $this->viewFactory->displayView(
76
            'view-donor',
77
            [
78
                'donor' => $donor,
79
            ]
80
        );
81
    }
82
83
    /**
84
     * @return void
85
     * @since 1.2
86
     */
87
    protected function addNotices()
88
    {
89
        if ($this->donor && $this->donor->isPending()) {
90
            Notices::addNotice('pending', __('This donor is pendng moderation.'), 'warning');
91
        }
92
    }
93
}
94