Passed
Push — master ( ca00a0...7e5282 )
by Iman
04:25
created

ControllerNormalizer   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 24
dl 0
loc 70
ccs 28
cts 30
cp 0.9333
rs 10
c 3
b 2
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 10 2
A checkControllerExists() 0 4 2
A checkDataMethodExists() 0 9 3
A determineDataMethod() 0 22 6
1
<?php
2
3
namespace Imanghafoori\Widgets\Utils\Normalizers;
4
5
use Imanghafoori\Widgets\Utils\NormalizerContract;
6
7
class ControllerNormalizer implements NormalizerContract
8
{
9
    /**
10
     * Figures out which method should be called as the controller.
11
     * @param object $widget
12
     * @return void
13
     */
14 22
    public function normalize($widget): void
15
    {
16 22
        [$controllerMethod, $ctrlClass] = $this->determineDataMethod($widget);
17
18 22
        if ($ctrlClass !== null) {
19 2
            $this->checkControllerExists($ctrlClass);
20 2
            $this->checkDataMethodExists($controllerMethod);
21
        }
22
23 22
        $widget->controller = $controllerMethod;
24 22
    }
25
26
    /**
27
     * @param string $ctrlClass
28
     */
29 2
    private function checkControllerExists(string $ctrlClass): void
30
    {
31 2
        if (! class_exists($ctrlClass)) {
32
            throw new \InvalidArgumentException("Controller class: [{$ctrlClass}] not found.");
33
        }
34 2
    }
35
36
    /**
37
     * @param $ctrlClass
38
     */
39 2
    private function checkDataMethodExists($ctrlClass): void
40
    {
41 2
        if (is_string($ctrlClass)) {
42 2
            $ctrlClass = explode('@', $ctrlClass);
43
        }
44
45 2
        [$ctrlClass, $method] = $ctrlClass;
46 2
        if (! method_exists($ctrlClass, $method)) {
47
            throw new \InvalidArgumentException("'data' method not found on ".$ctrlClass);
48
        }
49 2
    }
50
51
    /**
52
     * @param object $widget
53
     * @return array [$controllerMethod, $ctrlClass]
54
     */
55 22
    private function determineDataMethod($widget): array
56
    {
57
        // We decide to call data method on the widget object by default.
58
        // If the user has explicitly declared controller class path on
59
        // widget then we decide to call data method on that instead.
60 22
        if (! property_exists($widget, 'controller')) {
61 19
            return method_exists($widget, 'data') ? [[$widget, 'data'], null] : [null, null];
62
        }
63
64 3
        if (is_string($widget->controller)) {
65 2
            if (! str_contains($widget->controller, '@')) {
66 1
                return [$widget->controller.'@data', $widget->controller];
67
            }
68 1
            $widget->controller = explode('@', $widget->controller);
69 1
        } elseif (is_callable($widget->controller)) {
70 1
            return [$widget->controller, null];
71
        }
72
73 1
        $ctrlClass = $widget->controller[0];
74 1
        $controllerMethod = implode('@', $widget->controller);
75
76 1
        return [$controllerMethod, $ctrlClass];
77
    }
78
}
79