CiteUpdater   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 11

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A handle() 0 3 1
B initVocabCite() 0 26 9
1
<?php
2
3
namespace TopviewDigital\TranslationHelper\Service;
4
5
use TopviewDigital\TranslationHelper\Interfaces\AsyncBrokerInterface;
6
use TopviewDigital\TranslationHelper\Model\VocabCite;
7
use TopviewDigital\TranslationHelper\Model\VocabTerm;
8
9
class CiteUpdater implements AsyncBrokerInterface
10
{
11
    protected $cite;
12
    protected $term;
13
14
    public function __construct(VocabTerm $vocab, array $tracer)
15
    {
16
        $cite = [];
17
        //Clean Tail Text
18
        $cite['file'] = preg_replace('/(\.php)(.+)$/', '${1}', $tracer[0]['file']);
19
        //Remove Head of Base_Path
20
        $cite['file'] = str_replace(base_path(), '', $cite['file']);
21
        $cite['line'] = $tracer[0]['line'];
22
        array_shift($tracer);
23
        $cite['function'] = $tracer[0]['function'] ?? '';
24
        $cite['class'] = $tracer[0]['class'] ?? '';
25
        $cite = VocabCite::firstOrCreate($cite);
26
        $cite->refresh();
27
        $vocab->cites()->sync([$cite->id], false);
28
        $cite->save();
29
        $this->cite = $cite->id;
30
        $this->term = $vocab->term;
0 ignored issues
show
Bug introduced by
The property term does not seem to exist on TopviewDigital\TranslationHelper\Model\VocabTerm. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
31
    }
32
33
    private function initVocabCite(VocabCite $cite)
34
    {
35
        if (empty($cite->code)) {
36
            $lines = explode("\n", file_get_contents(base_path().$cite->file));
0 ignored issues
show
Bug introduced by
The property file does not seem to exist on TopviewDigital\TranslationHelper\Model\VocabCite. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
37
            $cite->code = $lines[$cite->line - 1];
0 ignored issues
show
Bug introduced by
The property line does not seem to exist on TopviewDigital\TranslationHelper\Model\VocabCite. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug introduced by
The property code does not seem to exist on TopviewDigital\TranslationHelper\Model\VocabCite. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
38
            if (substr($cite->file, -10) != '.blade.php') {
39
                for ($start = $cite->line - 2; $start > -1; $start--) {
40
                    $char = substr(rtrim($lines[$start]), -1);
41
                    if ($char == ';' || $char == '}' || $char == '{') {
42
                        $start++;
43
                        break;
44
                    }
45
                }
46
                $count = count($lines);
47
                for ($end = $cite->line - 1; $end < $count; $end++) {
48
                    $char = substr(rtrim($lines[$end]), -1);
49
                    if ($char == ';') {
50
                        break;
51
                    }
52
                }
53
                $code = array_filter(array_slice($lines, $start, $end - $start + 1, true), 'trim');
54
                $max = strlen($end);
55
                $cite->code = implode("\n", array_map(function ($u, $v) use ($max) {
56
                    return sprintf("%{$max}d    %s  ", $u + 1, rtrim($v));
57
                }, array_keys($code), $code));
58
                $cite->save();
59
            }
60
        }
61
    }
62
63
    public function handle()
64
    {
65
        $this->initVocabCite(VocabCite::find($this->cite));
66
    }
67
}
68