Passed
Push — master ( d59e0a...db01b9 )
by Robin
02:53
created

CiteUpdater::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 2
dl 0
loc 17
rs 9.8333
c 0
b 0
f 0
1
<?php
2
namespace TopviewDigital\TranslationHelper\Service;
3
4
use TopviewDigital\TranslationHelper\Model\VocabTerm;
5
use TopviewDigital\TranslationHelper\Model\VocabCite;
6
use TopviewDigital\TranslationHelper\Interfaces\AsyncBrokerInterface;
7
8
class CiteUpdater implements AsyncBrokerInterface
9
{
10
    protected $cite;
11
    protected $term;
12
13
    public function __construct(VocabTerm $vocab, array $tracer)
14
    {
15
        $cite = [];
16
        //Clean Tail Text
17
        $cite['file'] = preg_replace('/(\.php)(.+)$/', '${1}', $tracer[0]['file']);
18
        //Remove Head of Base_Path
19
        $cite['file'] = str_replace(base_path(), '', $cite['file']);
20
        $cite['line'] = $tracer[0]['line'];
21
        array_shift($tracer);
22
        $cite['function'] = $tracer[0]['function'] ?? '';
23
        $cite['class'] = $tracer[0]['class'] ?? '';
24
        $cite = VocabCite::firstOrCreate($cite);
25
        $cite->refresh();
26
        $vocab->cites()->sync([$cite->id], false);
27
        $cite->save();
28
        $this->cite = $cite->id;
29
        $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...
30
    }
31
32
    private function initVocabCite(VocabCite $cite)
33
    {
34
        if (empty($cite->code)) {
35
            $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...
36
            $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...
37
            if (substr($cite->file, -10) != '.blade.php') {
38
                for ($start = $cite->line - 2; $start > -1; $start--) {
39
                    $char = substr(rtrim($lines[$start]), -1);
40
                    if ($char == ';' || $char == '}' || $char == '{') {
41
                        $start++;
42
                        break;
43
                    }
44
                }
45
                $count = count($lines);
46
                for ($end = $cite->line - 1; $end < $count; $end++) {
47
                    $char = substr(rtrim($lines[$end]), -1);
48
                    if ($char == ';') {
49
                        break;
50
                    }
51
                }
52
                $code = array_filter(array_slice($lines, $start, $end - $start + 1, true), 'trim');
53
                $max = strlen($end);
54
                $cite->code = implode("\n", array_map(function ($u, $v) use ($max) {
55
                    return sprintf("%{$max}d    %s  ", $u + 1, rtrim($v));
56
                }, array_keys($code), $code));
57
                $cite->save();
58
            }
59
        }
60
    }
61
62
    public function handle()
63
    {
64
        $this->initVocabCite(VocabCite::find($this->cite));
65
    }
66
}
67