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

PresenterNormalizer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 41
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A checkPresentMethodExists() 0 4 2
A normalizePresenterName() 0 15 3
A checkPresenterExists() 0 4 2
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