ControllerNormalizer   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

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

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