Test Failed
Push — master ( aa40d3...766a39 )
by Dispositif
07:56
created

CorpusAdapter::addNewElementToMemoryCorpus()   A

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
4
 * 2019 : Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Infrastructure;
11
12
use App\Domain\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 $storage = [];
25
26
    /**
27
     * ugly memory // todo refac Generator.
28
     *
29
     * @param string $element
30
     * @param string $corpusName
31
     *
32
     * @return bool
33
     * @throws Exception
34
     */
35
    public function inCorpus(string $element, string $corpusName): bool
36
    {
37
        // corpus as array variable
38
        if (isset($this->storage[$corpusName])) {
39
            $corpData = $this->storage[$corpusName];
40
            return is_array($corpData) && in_array($element, $corpData);
41
        }
42
43
        // corpus as text
44
        $filename = $this->getCorpusFilename($corpusName);
45
46
        return $this->isStringInCSV($filename, $element);
47
    }
48
49
    private function getCorpusFilename(string $corpusName)
50
    {
51
        // Corpus as text file
52
        if ('firstname' === $corpusName) {
53
            return __DIR__.'/../Domain/resources/corpus_firstname.txt';
54
        }
55
        if ('all-titles' === $corpusName) {
56
            return __DIR__.'/frwiki-latest-all-titles-in-ns0.txt';
57
        }
58
59
        throw new DomainException("corpus $corpusName not defined");
60
    }
61
62
    public function setCorpusInStorage(string $corpusName, array $arrayContent): void
63
    {
64
        $this->storage[$corpusName] = $arrayContent;
65
    }
66
67
    /**
68
     * dirty TODO.
69
     *
70
     * @param string $corpusName
71
     * @param string $element
72
     *
73
     * @return bool
74
     * @throws Exception
75
     */
76
    public function addNewElementToCorpus(string $corpusName, string $element): bool
77
    {
78
        if (empty($element)) {
79
            return false;
80
        }
81
82
        //corpus as variable
83
        if (isset($this->storage[$corpusName])) {
84
            return $this->addNewElementToMemoryCorpus($corpusName, $element);
85
        }
86
87
        // else : corpus as file
88
        return $this->addNewElementToFileCorpus($corpusName, $element);
89
    }
90
91
    private function addNewElementToFileCorpus(string $corpusName, string $element): bool
92
    {
93
        if (empty($element)) {
94
            return false;
95
        }
96
97
        // strip "/"
98
        $sanitizeCorpusName = preg_replace('#[^0-9a-z_]#i', '', $corpusName);
99
        $filename = __DIR__.'/../Domain/resources/'.$sanitizeCorpusName.'.txt';
100
101
        // hack: create file or not ?
102
        if (!file_exists($filename)) {
103
            $newFile = @fopen($filename, 'w');
104
            if ($newFile !== false) {
105
                fclose($newFile);
106
            }
107
            if (!file_exists($filename)) {
108
                throw new Exception('corpus filename does not exist'.$filename);
109
            }
110
        }
111
112
        // check if the element already in the corpus
113
        if ($this->isStringInCSV($filename, $element)) {
114
            return false;
115
        }
116
117
        $write = file_put_contents(
118
            $filename,
119
            utf8_encode($element)."\n",
120
            FILE_APPEND | LOCK_EX
121
        );
122
123
        return (bool) $write;
124
    }
125
126
    private function addNewElementToMemoryCorpus(string $corpusName, string $element): bool
127
    {
128
        if (isset($this->storage[$corpusName]) && is_array($this->storage[$corpusName])) {
129
            if (!in_array($element, $this->storage[$corpusName])) {
130
                $this->storage[$corpusName][] = $element;
131
            }
132
133
            return true;
134
        }
135
136
        return false;
137
    }
138
}
139