Passed
Push — main ( 46d5a4...f60044 )
by Alex
01:08
created

TraitTranslate::file()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 24
nc 18
nop 1
dl 0
loc 30
rs 8.9137
c 0
b 0
f 0
1
<?php
2
3
namespace Erykai\Translate;
4
5
use JsonSchema\Exception\RuntimeException;
0 ignored issues
show
Bug introduced by
The type JsonSchema\Exception\RuntimeException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
7
/**
8
 *
9
 */
10
trait TraitTranslate
11
{
12
    /**
13
     * @param string $dir
14
     * create dir
15
     */
16
    private function create(string $dir): void
17
    {
18
        if (!is_dir($dir) && !mkdir($dir, 0755, true) && !is_dir($dir)) {
19
            throw new RuntimeException(sprintf('Directory "%s" was not created', $dir));
20
        }
21
    }
22
23
    /**
24
     * create dir defaults
25
     */
26
    protected function dir(): void
27
    {
28
        $this->create($this->getPath());
0 ignored issues
show
Bug introduced by
It seems like getPath() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

28
        $this->create($this->/** @scrutinizer ignore-call */ getPath());
Loading history...
29
        $this->create("{$this->getPath()}/{$this->getSource()}");
0 ignored issues
show
Bug introduced by
It seems like getSource() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

29
        $this->create("{$this->getPath()}/{$this->/** @scrutinizer ignore-call */ getSource()}");
Loading history...
30
        $this->create("{$this->getPath()}/{$this->getSource()}/public");
31
        $this->create("{$this->getPath()}/{$this->getTarget()}");
0 ignored issues
show
Bug introduced by
It seems like getTarget() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

31
        $this->create("{$this->getPath()}/{$this->/** @scrutinizer ignore-call */ getTarget()}");
Loading history...
32
        $this->create("{$this->getPath()}/{$this->getTarget()}/public");
33
    }
34
35
    /**
36
     * create files .translate
37
     */
38
    protected function file(string $module = null): void
39
    {
40
        if ($module) {
41
            $modulePath = dirname(__DIR__, 4)."/modules/{$module}/translate";
42
            $this->create("{$modulePath}/{$this->getSource()}");
43
            $this->create("{$modulePath}/{$this->getSource()}/public");
44
            $this->create("{$modulePath}/{$this->getTarget()}");
45
            $this->create("{$modulePath}/{$this->getTarget()}/public");
46
            $this->setSourceFile("{$modulePath}/{$this->getSource()}/{$this->getData()->file}.translate");
0 ignored issues
show
Bug introduced by
It seems like setSourceFile() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
            $this->/** @scrutinizer ignore-call */ 
47
                   setSourceFile("{$modulePath}/{$this->getSource()}/{$this->getData()->file}.translate");
Loading history...
Bug introduced by
It seems like getData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
            $this->setSourceFile("{$modulePath}/{$this->getSource()}/{$this->/** @scrutinizer ignore-call */ getData()->file}.translate");
Loading history...
47
            $this->setTargetFile("{$modulePath}/{$this->getTarget()}/{$this->getData()->file}.translate");
0 ignored issues
show
Bug introduced by
It seems like setTargetFile() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

47
            $this->/** @scrutinizer ignore-call */ 
48
                   setTargetFile("{$modulePath}/{$this->getTarget()}/{$this->getData()->file}.translate");
Loading history...
48
        } else {
49
            $this->setSourceFile("{$this->getPath()}/{$this->getSource()}/{$this->getData()->file}.translate");
50
            $this->setTargetFile("{$this->getPath()}/{$this->getTarget()}/{$this->getData()->file}.translate");
51
        }
52
53
        if (!is_file($this->getSourceFile())) {
0 ignored issues
show
Bug introduced by
It seems like getSourceFile() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

53
        if (!is_file($this->/** @scrutinizer ignore-call */ getSourceFile())) {
Loading history...
54
            file_put_contents($this->getSourceFile(), $this->getData()->text . PHP_EOL);
55
        } else if(!in_array($this->getData()->text . PHP_EOL, array_filter(file($this->getSourceFile())), true)){
56
            file_put_contents($this->getSourceFile(), $this->getData()->text . PHP_EOL, FILE_APPEND);
57
        }
58
59
        if (!is_file($this->getTargetFile())) {
0 ignored issues
show
Bug introduced by
It seems like getTargetFile() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

59
        if (!is_file($this->/** @scrutinizer ignore-call */ getTargetFile())) {
Loading history...
60
            file_put_contents($this->getTargetFile(), $this->translate(file_get_contents($this->getSourceFile())). PHP_EOL);
61
        } else {
62
            $source = array_filter(file($this->getSourceFile()));
63
            $target = array_filter(file($this->getTargetFile()));
64
            $result = array_diff_key($source, $target);
65
            $implode = implode("", $result);
66
            if(count(array_filter(file($this->getSourceFile()))) > count(array_filter(file($this->getTargetFile())))){
67
                file_put_contents($this->getTargetFile(), $this->translate($implode) . PHP_EOL, FILE_APPEND);
68
            }
69
70
        }
71
72
    }
73
74
    /**
75
     * @param string $text
76
     * @return mixed
77
     * send server translate
78
     */
79
    private function translate(string $text): mixed
80
    {
81
        $url = TRANSLATE_API_URL;
82
        $ch = curl_init($url);
83
        curl_setopt_array($ch, [
84
            CURLOPT_FOLLOWLOCATION => 1,
85
            CURLOPT_RETURNTRANSFER => 1,
86
            CURLOPT_POSTFIELDS => http_build_query([
87
                "key" => TRANSLATE_API_KEY,
0 ignored issues
show
Bug introduced by
The constant Erykai\Translate\TRANSLATE_API_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
88
                "source" => "en",
89
                "target" => $this->getTarget(),
90
                "text" => $text,
91
                "route" => $this->getData()->file
92
            ])
93
        ]);
94
        $response = curl_exec($ch);
95
        curl_close($ch);
96
        if (!$response) {
97
            return $text;
98
        }
99
        $data = json_decode($response);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

99
        $data = json_decode(/** @scrutinizer ignore-type */ $response);
Loading history...
100
        if ($data->status === "success") {
101
            return $data->translate;
102
        }
103
        return $text;
104
    }
105
106
}