Passed
Push — master ( ff3cde...45279d )
by Brian
02:44
created

Utilities::getAnswer()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 1
crap 5.0187
1
<?php
2
3
namespace Bmatovu\Ussd\Traits;
4
5
use Bmatovu\Ussd\Contracts\RenderableTag;
6
use Illuminate\Container\Container;
7
use Illuminate\Contracts\Config\Repository as ConfigRepository;
8
use Illuminate\Support\Str;
9
10
trait Utilities
11
{
12 3
    public function __get(string $key)
13
    {
14 3
        return $this->{$key};
15
    }
16
17 1
    public function __set(string $key, $value)
18
    {
19 1
        $this->{$key} = $value;
20
    }
21
22 1
    protected function fileToXpath(string $menuFile): \DOMXPath
23
    {
24 1
        if (! file_exists($menuFile)) {
25
            $menuFile = menus_path($menuFile);
26
        }
27
28 1
        $doc = new \DOMDocument();
29
30 1
        $doc->load($menuFile);
31
32 1
        return new \DOMXPath($doc);
33
    }
34
35 9
    protected function sessionExists(string $sessionId): bool
36
    {
37 9
        $preSessionId = $this->store->get('_session_id', '');
0 ignored issues
show
Bug Best Practice introduced by
The property store does not exist on Bmatovu\Ussd\Traits\Utilities. Since you implemented __get, consider adding a @property annotation.
Loading history...
38
39 9
        return $preSessionId === $sessionId;
40
    }
41
42 2
    protected function clean(string $code = ''): string
43
    {
44 2
        return trim(trim($code, '*'), '#');
45
    }
46
47 9
    protected function getAnswer(?string $userInput): ?string
48
    {
49 9
        if ('' === (string) $userInput) {
50 7
            return '';
51
        }
52
53 2
        $preAnswer = $this->store->get('_answer', '');
0 ignored issues
show
Bug Best Practice introduced by
The property store does not exist on Bmatovu\Ussd\Traits\Utilities. Since you implemented __get, consider adding a @property annotation.
Loading history...
54
55 2
        $answer = $this->clean(str_replace($preAnswer, '', $userInput));
56
57 2
        if ('' === (string) $answer) {
58
            return '';
59
        }
60
61 2
        if (! $preAnswer || Str::endsWith($preAnswer, '*')) {
62 1
            $this->store->append('_answer', "{$answer}*");
63
        } else {
64 1
            $this->store->append('_answer', "*{$answer}*");
65
        }
66
67 2
        return $answer;
68
    }
69
70 9
    protected function resolveTagName(\DOMNode $node): string
71
    {
72 9
        $tagName = $node->tagName;
73
74 9
        if ('action' !== strtolower($tagName)) {
75 9
            return Str::studly("{$tagName}Tag");
76
        }
77
78
        $tagName = $node->attributes->getNamedItem('name')->nodeValue;
0 ignored issues
show
Bug introduced by
The method getNamedItem() does not exist on null. ( Ignorable by Annotation )

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

78
        $tagName = $node->attributes->/** @scrutinizer ignore-call */ getNamedItem('name')->nodeValue;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
80
        return Str::studly("{$tagName}Action");
81
    }
82
83 9
    protected function resolveTagClass(string $tagName): string
84
    {
85 9
        $config = Container::getInstance()->make(ConfigRepository::class);
86 9
        $tagNs = $config->get('ussd.tag-ns', []);
87 9
        $actionNs = $config->get('ussd.action-ns', []);
88
89 9
        $namespaces = array_merge($tagNs, $actionNs);
90
91 9
        $fqcn = $tagName;
0 ignored issues
show
Unused Code introduced by
The assignment to $fqcn is dead and can be removed.
Loading history...
92
93 9
        foreach ($namespaces as $ns) {
94 9
            $fqcn = "{$ns}\\{$tagName}";
95 9
            if (class_exists($fqcn)) {
96 8
                return $fqcn;
97
            }
98
        }
99
100 1
        throw new \Exception("Missing tag {$tagName}.");
101
    }
102
103 9
    protected function instantiateTag(string $tagName, array $args = []): RenderableTag
104
    {
105 9
        $fqcn = $this->resolveTagClass($tagName);
106
107 8
        return \call_user_func_array([new \ReflectionClass($fqcn), 'newInstance'], $args);
108
    }
109
}
110