Passed
Push — master ( 057162...5e1705 )
by Brian
02:41
created

Utilities   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 17
eloc 41
c 0
b 0
f 0
dl 0
loc 101
ccs 44
cts 48
cp 0.9167
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fileToXpath() 0 11 2
A clean() 0 3 1
A resolveTagName() 0 11 2
A __set() 0 3 1
A getAnswer() 0 21 5
A resolveTagClass() 0 21 3
A instantiateTag() 0 5 1
A __get() 0 3 1
A sessionExists() 0 5 1
1
<?php
2
3
namespace Bmatovu\Ussd\Traits;
4
5
use Bmatovu\Ussd\Contracts\RenderableTag;
6
use Bmatovu\Ussd\Support\Util;
7
use Illuminate\Container\Container;
8
use Illuminate\Contracts\Config\Repository as ConfigRepository;
9
use Illuminate\Support\Str;
10
11
trait Utilities
12
{
13 3
    public function __get(string $key)
14
    {
15 3
        return $this->{$key};
16
    }
17
18 1
    public function __set(string $key, $value)
19
    {
20 1
        $this->{$key} = $value;
21
    }
22
23 1
    protected function fileToXpath(string $menuFile): \DOMXPath
24
    {
25 1
        if (!file_exists($menuFile)) {
26
            $menuFile = menu_path($menuFile);
27
        }
28
29 1
        $doc = new \DOMDocument();
30
31 1
        $doc->load($menuFile);
32
33 1
        return new \DOMXPath($doc);
34
    }
35
36 9
    protected function sessionExists(string $sessionId): bool
37
    {
38 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...
39
40 9
        return $preSessionId === $sessionId;
41
    }
42
43 2
    protected function clean(string $code = ''): string
44
    {
45 2
        return trim(trim($code, '*'), '#');
46
    }
47
48 9
    protected function getAnswer(?string $userInput): ?string
49
    {
50 9
        if ('' === (string) $userInput) {
51 7
            return '';
52
        }
53
54 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...
55
56 2
        $answer = $this->clean(str_replace($preAnswer, '', $userInput));
57
58 2
        if ('' === (string) $answer) {
59
            return '';
60
        }
61
62 2
        if (!$preAnswer || Str::endsWith($preAnswer, '*')) {
63 1
            $this->store->append('_answer', "{$answer}*");
64
        } else {
65 1
            $this->store->append('_answer', "*{$answer}*");
66
        }
67
68 2
        return $answer;
69
    }
70
71 9
    protected function resolveTagName(\DOMNode $node): string
72
    {
73 9
        $tagName = $node->tagName;
74
75 9
        if ('action' !== strtolower($tagName)) {
76 9
            return Str::studly("{$tagName}Tag");
77
        }
78
79
        $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

79
        $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...
80
81
        return Str::studly("{$tagName}Action");
82
    }
83
84 9
    protected function resolveTagClass(string $tagName): string
85
    {
86 9
        $config = Container::getInstance()->make(ConfigRepository::class);
87 9
        $tagNs = $config->get('ussd.tag-ns', []);
88 9
        $actionNs = $config->get('ussd.action-ns', []);
89
90 9
        $namespaces = array_merge($tagNs, $actionNs);
91
92 9
        $fqcn = $tagName;
93
94 9
        foreach ($namespaces as $ns) {
95 9
            $fqcn = "{$ns}\\{$tagName}";
96 9
            if (class_exists($fqcn)) {
97 8
                return $fqcn;
98
            }
99
        }
100
101 1
        $this->store->put('missing_tag', $tagName);
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...
102 1
        $this->store->put('missing_tag_fqcn', $fqcn);
103
104 1
        throw new \Exception(Util::hydrate($this->store, trans('MissingTag')));
105
    }
106
107 9
    protected function instantiateTag(string $tagName, array $args = []): RenderableTag
108
    {
109 9
        $fqcn = $this->resolveTagClass($tagName);
110
111 8
        return \call_user_func_array([new \ReflectionClass($fqcn), 'newInstance'], $args);
112
    }
113
}
114