Scrapper   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 90
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A factory() 0 9 1
A export() 0 6 1
A run() 0 9 1
A getInstance() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MazenTouati\NoEmoji;
6
7
use MazenTouati\Simple2wayConfig\S2WConfig;
8
use MazenTouati\NoEmoji\Entities\File;
9
use MazenTouati\NoEmoji\Entities\Group;
10
use MazenTouati\NoEmoji\Entities\GroupsFactory;
11
12
/**
13
 * Scrapes the Unicode references file to extract and organize Unicodes
14
 *
15
 * @author Mazen Touati <[email protected]>
16
 */
17
class Scrapper
18
{
19
    /**
20
     * The class instance
21
     *
22
     * @var Scrapper
23
     */
24
    private static $_instance = null;
25
26
    /**
27
     * Configuration object
28
     *
29
     * @var S2WConfig
30
     */
31
    public $config;
32
33
    /**
34
     * Input file
35
     *
36
     * @var File
37
     */
38
    private $_file;
39
40
    /**
41
     * Emoji unicodes
42
     *
43
     * @var array
44
     */
45
    private $_unicodes = [];
46
47
48
    /**
49
     * Retrieves the singleton instance
50
     *
51
     * @return Scrapper
52
     */
53
    public static function getInstance(): Scrapper
54
    {
55
        if (self::$_instance == null) {
56
            self::$_instance = new Scrapper();
57
        }
58
59
        return self::$_instance;
60
    }
61
62
    /**
63
     * Initializes the Scrapper
64
     *
65
     * @param  S2WConfig $c Configuration object
66
     *
67
     * @return Scrapper
68
     */
69
    public static function factory(S2WConfig $c): Scrapper
70
    {
71
        $instance = self::getInstance();
72
        $instance->config = $c;
73
74
        $instance->_file = new File($c);
75
        $instance->_file->prepareFileForScrapping();
76
77
        return $instance;
78
    }
79
80
    /**
81
     * Starts the scrapping
82
     *
83
     * @return Scrapper
84
     */
85
    public function run(): Scrapper
86
    {
87
        $this->_unicodes = (new GroupsFactory())
88
            ->extractGroups($this->_file->content)
89
            ->extractUnicodesFromGroups()
90
            ->flattenUnicodes()
91
            ->getUnicodes();
92
93
        return $this;
94
    }
95
96
    /**
97
     * Exports the unicodes data into JSON file
98
     *
99
     * @return boolean|array Returns false on error or the result array
100
     */
101
    public function export()
102
    {
103
        $base = $this->config->get('storage.base');
104
        $output = $this->config->get('storage.output.json');
105
        $fileTitle = $this->config->get('storage.output.jsonFileTitle', 'Undefined Title');
106
        return File::toJSON($base . $output, $fileTitle, $this->_unicodes);
107
    }
108
}
109