Daux   A
last analyzed

Complexity

Total Complexity 39

Size/Duplication

Total Lines 285
Duplicated Lines 0 %

Test Coverage

Coverage 48.54%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 99
c 3
b 0
f 1
dl 0
loc 285
ccs 50
cts 103
cp 0.4854
rs 9.28
wmc 39

16 Methods

Rating   Name   Duplication   Size   Complexity  
A setProcessor() 0 8 1
A getParams() 0 3 1
A getConfig() 0 19 6
A getProcessor() 0 7 2
A getGenerators() 0 11 1
A generateTree() 0 21 3
A __construct() 0 5 1
A findAlternatives() 0 13 3
A getContentTypeHandler() 0 13 2
A getOutput() 0 7 2
A writeln() 0 3 1
A getVerbosity() 0 3 1
B getGenerator() 0 37 7
A getContentExtensions() 0 7 2
A write() 0 3 1
A getProcessorClass() 0 21 5
1
<?php namespace Todaymade\Daux;
2
3
use Symfony\Component\Console\Output\NullOutput;
4
use Symfony\Component\Console\Output\OutputInterface;
5
use Todaymade\Daux\ContentTypes\ContentTypeHandler;
6
use Todaymade\Daux\Tree\Builder;
7
use Todaymade\Daux\Tree\Root;
8
9
class Daux
10
{
11
    const STATIC_MODE = 'DAUX_STATIC';
12
    const LIVE_MODE = 'DAUX_LIVE';
13
14
    public static $output;
15
16
    /** @var Tree\Root */
17
    public $tree;
18
19
    /** @var Config */
20
    public $config;
21
22
    /** @var \Todaymade\Daux\Format\Base\Generator */
23
    protected $generator;
24
25
    /** @var ContentTypeHandler */
26
    protected $typeHandler;
27
28
    /** @var string[] */
29
    protected $validExtensions;
30
31
    /** @var Processor */
32
    protected $processor;
33
34
    /** @var bool */
35
    private $merged_tree = false;
36
37
    public function __construct(Config $config, OutputInterface $output)
38
    {
39 1
        Daux::$output = $output;
40
41 1
        $this->config = $config;
42
    }
43
44 1
    /**
45 1
     * Generate the tree that will be used.
46
     */
47
    public function generateTree()
48
    {
49
        $this->config->setValidContentExtensions($this->getContentExtensions());
50 1
51
        $this->tree = new Root($this->getConfig());
52 1
        Builder::build($this->tree, $this->config->getIgnore());
53
54 1
        // Apply the language name as Section title
55 1
        if ($this->config->isMultilanguage()) {
56
            foreach ($this->config->getLanguages() as $key => $node) {
57
                $this->tree->getEntries()[$key]->setTitle($node);
58 1
            }
59
        }
60
61
        // Enhance the tree with processors
62
        $this->getProcessor()->manipulateTree($this->tree);
63
64
        // Sort the tree one last time before it is finalized
65 1
        Builder::sortTree($this->tree);
66
67
        Builder::finalizeTree($this->tree);
68 1
    }
69
70 1
    /**
71 1
     * @return Config
72
     */
73
    public function getConfig()
74
    {
75
        if ($this->tree && !$this->merged_tree) {
76 1
            $this->config->setTree($this->tree);
77
            $this->config->setIndex($this->tree->getIndexPage() ?: $this->tree->getFirstPage());
78 1
            $entry_page = null;
79
            if ($this->config->isMultilanguage()) {
80
                $entry_page = [];
81
                foreach ($this->config->getLanguages() as $key => $name) {
82
                    $entry_page[$key] = $this->tree->getEntries()[$key]->getFirstPage();
0 ignored issues
show
Bug introduced by Stéphane Goetz
The method getFirstPage() does not exist on Todaymade\Daux\Tree\Entry. It seems like you code against a sub-type of Todaymade\Daux\Tree\Entry such as Todaymade\Daux\Tree\Directory. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
                    /** @scrutinizer ignore-call */ 
83
                    $entry_page[$key] = $this->tree->getEntries()[$key]->getFirstPage();
Loading history...
83
                }
84
            } else {
85
                $entry_page = $this->tree->getFirstPage();
86
            }
87
            $this->config->setEntryPage($entry_page);
88
            $this->merged_tree = true;
89
        }
90
91
        return $this->config;
92
    }
93
94 1
    /**
95
     * @return Config
96
     *
97
     * @deprecated Use getConfig instead
98
     */
99
    public function getParams()
100
    {
101
        return $this->getConfig();
102
    }
103
104
    /**
105
     * @return Processor
106
     */
107
    public function getProcessor()
108
    {
109 1
        if (!$this->processor) {
110
            $this->processor = new Processor($this, Daux::getOutput(), 0);
111 1
        }
112 1
113
        return $this->processor;
114
    }
115 1
116
    public function setProcessor(Processor $processor)
117
    {
118
        $this->processor = $processor;
119
120
        // This is not the cleanest but it's
121
        // the best I've found to use the
122
        // processor in very remote places
123
        $this->config->setProcessorInstance($processor);
124
    }
125
126
    public function getGenerators()
127
    {
128
        $default = [
129
            'confluence' => '\Todaymade\Daux\Format\Confluence\Generator',
130
            'html-file' => '\Todaymade\Daux\Format\HTMLFile\Generator',
131 1
            'html' => '\Todaymade\Daux\Format\HTML\Generator',
132
        ];
133
134 1
        $extended = $this->getProcessor()->addGenerators();
135
136
        return array_replace($default, $extended);
137
    }
138
139 1
    /**
140
     * Processor class
141 1
     * 
142
     * You can provide absolute class name or short class name if processor locates in \Todaymade\Daux\Extension namespace. 
143
     * Location: vendor/daux/daux.io/daux
144
     * 
145
     * @see \Todaymade\Daux\Extension\Processor
146
     * @example -p \\Todaymade\\Daux\\Extension\\Processor
147
     * @throws \RuntimeException
148
     * @return NULL|string
149
     */
150
    public function getProcessorClass()
151
    {
152
        $processor = $this->getConfig()->getProcessor();
153
154
        if (empty($processor)) {
155
            return null;
156
        }
157
        
158
        if (!strstr($processor, "\\")) {
159
            $processor = '\\Todaymade\\Daux\\Extension\\' . $processor;
160
        }
161
        
162
        if (!class_exists($processor)) {
163
            throw new \RuntimeException("Class '$processor' not found. We cannot use it as a Processor");
164
        }
165
        
166
        if (!array_key_exists('Todaymade\\Daux\\Processor', class_parents($processor))) {
167
            throw new \RuntimeException("Class '$processor' invalid, should extend '\\Todaymade\\Daux\\Processor'");
168
        }
169
170
        return $processor;
171
    }
172
173
    protected function findAlternatives($input, $words)
174
    {
175
        $alternatives = [];
176
177
        foreach ($words as $word) {
178
            $lev = levenshtein($input, $word);
179
180
            if ($lev <= \strlen($word) / 3) {
181 1
                $alternatives[] = $word;
182
            }
183 1
        }
184
185
        return $alternatives;
186
    }
187 1
188
    /**
189 1
     * @return \Todaymade\Daux\Format\Base\Generator
190
     */
191 1
    public function getGenerator()
192
    {
193
        if ($this->generator) {
194
            return $this->generator;
195
        }
196
197
        $generators = $this->getGenerators();
198
199
        $format = $this->getConfig()->getFormat();
200
201
        if (!array_key_exists($format, $generators)) {
202
            $message = "The format '$format' doesn't exist, did you forget to set your processor ?";
203
204
            $alternatives = $this->findAlternatives($format, array_keys($generators));
205
206
            if (0 == \count($alternatives)) {
207 1
                $message .= "\n\nAvailable formats are \n    " . implode("\n    ", array_keys($generators));
208 1
            } elseif (1 == \count($alternatives)) {
209
                $message .= "\n\nDid you mean this?\n    " . implode("\n    ", $alternatives);
210
            } else {
211
                $message .= "\n\nDid you mean one of these?\n    " . implode("\n    ", $alternatives);
212 1
            }
213 1
214
            throw new \RuntimeException($message);
215
        }
216
217 1
        $class = $generators[$format];
218
        if (!class_exists($class)) {
219
            throw new \RuntimeException("Class '$class' not found. We cannot use it as a Generator");
220 1
        }
221
222 1
        $interface = 'Todaymade\Daux\Format\Base\Generator';
223
        if (!in_array('Todaymade\Daux\Format\Base\Generator', class_implements($class))) {
224
            throw new \RuntimeException("The class '$class' does not implement the '$interface' interface");
225
        }
226 1
227
        return $this->generator = new $class($this);
228 1
    }
229
230 1
    public function getContentTypeHandler()
231
    {
232 1
        if ($this->typeHandler) {
233
            return $this->typeHandler;
234
        }
235
236
        $base_types = $this->getGenerator()->getContentTypes();
237
238
        $extended = $this->getProcessor()->addContentType();
239
240 1
        $types = array_merge($base_types, $extended);
241
242 1
        return $this->typeHandler = new ContentTypeHandler($types);
243
    }
244
245
    /**
246 1
     * Get all content file extensions.
247
     *
248
     * @return string[]
249 3
     */
250 3
    public function getContentExtensions()
251 1
    {
252
        if (!empty($this->validExtensions)) {
253
            return $this->validExtensions;
254 3
        }
255
256
        return $this->validExtensions = $this->getContentTypeHandler()->getContentExtensions();
257
    }
258
259
    public static function getOutput()
260
    {
261
        if (!Daux::$output) {
262
            Daux::$output = new NullOutput();
263
        }
264
265
        return Daux::$output;
266
    }
267
268
    /**
269
     * Writes a message to the output.
270
     *
271
     * @param array|string $messages The message as an array of lines or a single string
272
     * @param bool         $newline  Whether to add a newline
273
     * @param int          $options  A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
274 2
     */
275 2
    public static function write($messages, $newline = false, $options = 0)
276 2
    {
277
        Daux::getOutput()->write($messages, $newline, $options);
278
    }
279
280
    /**
281
     * Writes a message to the output and adds a newline at the end.
282
     *
283
     * @param array|string $messages The message as an array of lines of a single string
284
     * @param int          $options  A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL
285
     */
286
    public static function writeln($messages, $options = 0)
287
    {
288
        Daux::getOutput()->write($messages, true, $options);
289
    }
290
291
    public static function getVerbosity()
292
    {
293
        return Daux::getOutput()->getVerbosity();
294
    }
295
}
296