Passed
Push — master ( 2f2795...71165f )
by Iman
06:01
created

PresenterNormalizer::normalizePresenterName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils\Normalizers;
4
5
class PresenterNormalizer
6
{
7
    /**
8
     * Figures out which method should be called as the presenter.
9
     * @param object $widget
10
     * @return null
11
     */
12 14
    public function normalizePresenterName($widget)
13
    {
14 14
        if (property_exists($widget, 'presenter')) {
15 1
            $presenter = $widget->presenter;
16 1
            $this->checkPresenterExists($presenter);
17
        } else {
18 13
            $presenter = get_class($widget).'Presenter';
19 13
            if (! class_exists($presenter)) {
20 13
                return $widget->presenter = null;
21
            }
22
        }
23
24 1
        $this->checkPresentMethodExists($presenter);
25
26 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

26
        $widget->presenter = /** @scrutinizer ignore-type */ $presenter.'@present';
Loading history...
27 1
    }
28
29
    /**
30
     * @param string $presenter
31
     */
32 1
    private function checkPresentMethodExists($presenter)
33
    {
34 1
        if (! method_exists($presenter, 'present')) {
35
            throw new \InvalidArgumentException("'present' method not found on : ".$presenter);
36
        }
37 1
    }
38
39
    /**
40
     * @param string $presenter
41
     */
42 1
    private function checkPresenterExists($presenter)
43
    {
44 1
        if (! class_exists($presenter)) {
45
            throw new \InvalidArgumentException("Presenter Class [{$presenter}] not found.");
46
        }
47 1
    }
48
}
49