PresenterNormalizer::normalize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils\Normalizers;
4
5
use Imanghafoori\Widgets\Utils\NormalizerContract;
6
7
class PresenterNormalizer implements NormalizerContract
8
{
9
    /**
10
     * Figures out which method should be called as the presenter.
11
     *
12
     * @param  object  $widget
13
     * @return void
14
     */
15 24
    public function normalize($widget): void
16
    {
17 24
        $presenter = $this->figureOutPresenterClass($widget);
18
19 24
        if ($widget->presenter) {
20 1
            $this->checkPresentMethodExists($presenter);
21 1
            $widget->presenter = $presenter.'@present';
0 ignored issues
show
Bug introduced by
Are you sure $presenter of type object can be used in concatenation? ( Ignorable by Annotation )

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

21
            $widget->presenter = /** @scrutinizer ignore-type */ $presenter.'@present';
Loading history...
22
        }
23 24
    }
24
25
    /**
26
     * @param  string  $presenter
27
     */
28 1
    private function checkPresentMethodExists($presenter): void
29
    {
30 1
        if (! method_exists($presenter, 'present')) {
31
            throw new \InvalidArgumentException("'present' method not found on : ".$presenter);
32
        }
33 1
    }
34
35
    /**
36
     * @param  string  $presenter
37
     */
38 1
    private function checkPresenterExists($presenter): void
39
    {
40 1
        if (! class_exists($presenter)) {
41
            throw new \InvalidArgumentException("Presenter Class [{$presenter}] not found.");
42
        }
43 1
    }
44
45
    /**
46
     * @param $widget
47
     * @return string
48
     */
49 24
    private function figureOutPresenterClass($widget): string
50
    {
51 24
        if (property_exists($widget, 'presenter')) {
52 1
            $presenter = $widget->presenter;
53 1
            $this->checkPresenterExists($presenter);
54
        } else {
55 23
            $presenter = get_class($widget).'Presenter';
56 23
            if (! class_exists($presenter)) {
57 23
                $widget->presenter = null;
58
            }
59
        }
60
61 24
        return $presenter;
62
    }
63
}
64