Completed
Push — master ( 07f861...31eb2d )
by Iurii
02:00
created

Extractor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 92
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B process() 0 28 6
B exists() 0 24 6
1
<?php
2
3
/**
4
 * @package Extractor
5
 * @author Iurii Makukh <[email protected]>
6
 * @copyright Copyright (c) 2015, Iurii Makukh
7
 * @license https://www.gnu.org/licenses/gpl.html GNU/GPLv3
8
 */
9
10
namespace gplcart\modules\extractor\handlers;
11
12
use gplcart\core\models\Language as LanguageModel;
13
use gplcart\modules\extractor\models\Extractor as ExtractorModel;
14
15
/**
16
 * String extractor handler
17
 */
18
class Extractor
19
{
20
21
    /**
22
     * Extractor model instance
23
     * @var \gplcart\modules\extractor\models\Extractor $extractor
24
     */
25
    protected $extractor;
26
27
    /**
28
     * Language model class instance
29
     * @var \gplcart\core\models\Language $language
30
     */
31
    protected $language;
32
33
    /**
34
     * @param LanguageModel $language
35
     * @param ExtractorModel $extractor
36
     */
37
    public function __construct(LanguageModel $language,
38
            ExtractorModel $extractor)
39
    {
40
        $this->language = $language;
41
        $this->extractor = $extractor;
42
    }
43
44
    /**
45
     * Processes one extraction job iteration
46
     * @param array $job
47
     * @return array
48
     */
49
    public function process(array &$job)
50
    {
51
        if (!isset($job['context']['offset'])) {
52
            $job['context']['offset'] = 0;
53
        }
54
55
        $scanned = $this->extractor->scan(array('directory' => $job['data']['directory']));
56
        $files = array_slice($scanned, $job['context']['offset'], $job['data']['limit']);
57
58
        if (empty($files)) {
59
            $job['status'] = false;
60
            $job['done'] = $job['total'];
61
            return $job;
62
        }
63
64
        foreach ($files as $file) {
65
            foreach ($this->extractor->extractFromFile($file) as $string) {
66
                if (!$this->exists($string, $job)) {
67
                    $job['inserted'] ++;
68
                    gplcart_file_csv($job['data']['file'], array($string, ''));
69
                }
70
            }
71
        }
72
73
        $job['context']['offset'] += count($files);
74
        $job['done'] = $job['context']['offset'];
75
        return $job;
76
    }
77
78
    /**
79
     * Check if the string already exists in the file
80
     * @param string $string
81
     * @param array $job
82
     * @return boolean
83
     */
84
    protected function exists($string, array $job)
85
    {
86
        // Check core translation for dublicates
87
        if (!empty($job['data']['check_file'])) {
88
            $translations = $this->language->loadTranslation($job['data']['check_file']);
89
            if (isset($translations[$string])) {
90
                return true;
91
            }
92
        }
93
94
        // Check the string already written
95
        $handle = fopen($job['data']['file'], 'r');
96
97
        $found = false;
98
        while (($data = fgetcsv($handle, 1000)) !== false) {
99
            if (isset($data[0]) && $data[0] === $string) {
100
                $found = true;
101
                break;
102
            }
103
        }
104
105
        fclose($handle);
106
        return $found;
107
    }
108
109
}
110