Passed
Push — master ( aaffa4...1570ce )
by mazen
03:31 queued 01:59
created

Prettifier::getInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace MazenTouati\NoEmoji;
5
6
use MazenTouati\Simple2wayConfig\S2WConfig;
7
use MazenTouati\NoEmoji\Entities\File;
8
9
/**
10
 * Uses the scrapped unicodes to generate a RegEx pattern
11
 *
12
 * @author Mazen Touati <[email protected]>
13
 */
14
class Prettifier
15
{
16
    /**
17
     * The class instance
18
     *
19
     * @var Prettifier
20
     */
21
    private static $_instance = null;
22
23
    /**
24
     * File instance
25
     * @var File
26
     */
27
    private $_file;
28
29
    /**
30
     * The pretty content
31
     * @var string
32
     */
33
    private $_content;
34
35
    /**
36
     * Retrieves the singleton instance
37
     *
38
     * @return Prettifier
39
     */
40
    public static function getInstance()
41
    {
42
        if (self::$_instance == null) {
43
            self::$_instance = new Prettifier();
44
        }
45
46
        return self::$_instance;
47
    }
48
49
    /**
50
     * Initializes the Scrapper
51
     *
52
     * @param S2WConfig $c Configuration object
53
     *
54
     * @return Handler
55
     */
56
    public static function factory(S2WConfig $c)
57
    {
58
        $instance = self::getInstance();
59
        $instance->config = $c;
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
61
        $instance->_file = new File($c, 'storage.output.ranges');
62
63
        return $instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $instance returns the type MazenTouati\NoEmoji\Prettifier which is incompatible with the documented return type MazenTouati\NoEmoji\Handler.
Loading history...
64
    }
65
66
    /**
67
     * Starts prettifying
68
     *
69
     * @param  string $type Prettifier class name
70
     *
71
     * @return Prettifier
72
     */
73
    public function run($type = 'ArrayPrettifier')
74
    {
75
        $type = 'MazenTouati\NoEmoji\Prettifiers\\'.$type;
76
        $this->_content = $type::run($this->_file);
77
        return $this;
78
    }
79
80
    /**
81
     * Exports the pretty output
82
     *
83
     * @return boolean|array Returns false on error or the result array
84
     */
85
    public function export()
86
    {
87
        $base = $this->config->get('storage.base');
88
        $fileTitle = $this->config->get('storage.output.prettyRangesFileTitle', 'Undefined Title');
89
        $output = $this->config->get('storage.output.prettyRanges');
90
91
        $export = File::toText($base . $output, $fileTitle, $this->_content);
92
93
        if (!is_array($export)) {
94
            return false;
95
        }
96
97
        return $export;
98
    }
99
}
100