CorpusAdapter::addNewElementToMemoryCorpus()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 5
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 20
rs 10
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Infrastructure;
11
12
use App\Domain\InfrastructurePorts\CorpusInterface;
13
use DomainException;
14
use Exception;
15
16
/**
17
 * @unused
18
 * Dirty todo refac with FileManager and league/flysystem
19
 * todo : deleteElementFromCorpus, setCorpusFromFilename ???
20
 * Class CorpusAdapter.
21
 */
22
class CorpusAdapter extends FileManager implements CorpusInterface
23
{
24
    private array $storage = [];
25
26
    /**
27
     * ugly memory // todo refac Generator.
28
     */
29
    public function inCorpus(string $element, string $corpusName): bool
30
    {
31
        // corpus as array variable
32
        if (isset($this->storage[$corpusName])) {
33
            $corpData = $this->storage[$corpusName];
34
            return is_array($corpData) && in_array($element, $corpData);
35
        }
36
37
        // corpus as text
38
        $filename = $this->getCorpusFilename($corpusName);
39
40
        return $this->isStringInCSV($filename, $element);
41
    }
42
43
    private function getCorpusFilename(string $corpusName): string
44
    {
45
        // Corpus as text file
46
        if ('firstname' === $corpusName) {
47
            return __DIR__.'/../Domain/resources/corpus_firstname.txt';
48
        }
49
        if ('all-titles' === $corpusName) {
50
            return __DIR__.'/frwiki-latest-all-titles-in-ns0.txt';
51
        }
52
53
        throw new DomainException("corpus $corpusName not defined");
54
    }
55
56
    public function setCorpusInStorage(string $corpusName, array $arrayContent): void
57
    {
58
        $this->storage[$corpusName] = $arrayContent;
59
    }
60
61
    /**
62
     * dirty TODO.
63
     */
64
    public function addNewElementToCorpus(string $corpusName, string $element): bool
65
    {
66
        if (empty($element)) {
67
            return false;
68
        }
69
70
        //corpus as variable
71
        if (isset($this->storage[$corpusName])) {
72
            return $this->addNewElementToMemoryCorpus($corpusName, $element);
73
        }
74
75
        // else : corpus as file
76
        return $this->addNewElementToFileCorpus($corpusName, $element);
77
    }
78
79
    private function addNewElementToFileCorpus(string $corpusName, string $element): bool
80
    {
81
        if (empty($element)) {
82
            return false;
83
        }
84
85
        // strip "/"
86
        $sanitizeCorpusName = preg_replace('#[^0-9a-z_]#i', '', $corpusName);
87
        $filename = __DIR__.'/../Domain/resources/'.$sanitizeCorpusName.'.txt';
88
89
        // hack: create file or not ?
90
        if (!file_exists($filename)) {
91
            $newFile = @fopen($filename, 'w');
92
            if ($newFile !== false) {
93
                fclose($newFile);
94
            }
95
            if (!file_exists($filename)) {
96
                throw new Exception('corpus filename does not exist'.$filename);
97
            }
98
        }
99
100
        // check if the element already in the corpus
101
        if ($this->isStringInCSV($filename, $element)) {
102
            return false;
103
        }
104
105
        $write = file_put_contents(
106
            $filename,
107
            utf8_encode($element)."\n",
108
            FILE_APPEND | LOCK_EX
109
        );
110
111
        return (bool) $write;
112
    }
113
114
    private function addNewElementToMemoryCorpus(string $corpusName, string $element): bool
115
    {
116
        if (isset($this->storage[$corpusName]) && is_array($this->storage[$corpusName])) {
117
            if (!in_array($element, $this->storage[$corpusName])) {
118
                $this->storage[$corpusName][] = $element;
119
            }
120
121
            return true;
122
        }
123
124
        return false;
125
    }
126
}
127