Generator   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 63
dl 0
loc 126
ccs 0
cts 87
cp 0
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getContentTypes() 0 4 1
B generateRecursive() 0 38 6
A __construct() 0 5 1
B checkConfiguration() 0 23 7
A generateAll() 0 27 2
1
<?php namespace Todaymade\Daux\Format\Confluence;
2
3
use Symfony\Component\Console\Input\InputInterface;
4
use Symfony\Component\Console\Output\OutputInterface;
5
use Todaymade\Daux\Config;
6
use Todaymade\Daux\Console\RunAction;
7
use Todaymade\Daux\Daux;
8
use Todaymade\Daux\Tree\Content;
9
use Todaymade\Daux\Tree\Directory;
10
11
class Generator implements \Todaymade\Daux\Format\Base\Generator
12
{
13
    use RunAction;
14
15
    /** @var string */
16
    protected $prefix;
17
18
    /** @var Daux */
19
    protected $daux;
20
21
    /**
22
     * @param Daux $daux
23
     */
24
    public function __construct(Daux $daux)
25
    {
26
        $this->daux = $daux;
27
28
        $this->checkConfiguration();
29
    }
30
31
    public function checkConfiguration()
32
    {
33
        $config = $this->daux->getParams();
34
        $confluence = $config->getConfluenceConfiguration();
35
36
        if ($confluence == null) {
37
            throw new \RuntimeException('You must specify your Confluence configuration');
38
        }
39
40
        $mandatory = ['space_id', 'base_url', 'user', 'pass', 'prefix'];
41
        $errors = [];
42
        foreach ($mandatory as $key) {
43
            if (!array_key_exists($key, $confluence)) {
44
                $errors[] = $key;
45
            }
46
        }
47
48
        if (count($errors)) {
49
            throw new \RuntimeException("The following options are mandatory for confluence : '" . implode("', '", $errors) . "'");
50
        }
51
52
        if (!array_key_exists('ancestor_id', $confluence) && !array_key_exists('root_id', $confluence)) {
53
            throw new \RuntimeException("You must specify an 'ancestor_id' or a 'root_id' for confluence.");
54
        }
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function getContentTypes()
61
    {
62
        return [
63
            new ContentTypes\Markdown\ContentType($this->daux->getParams()),
64
        ];
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function generateAll(InputInterface $input, OutputInterface $output, $width)
71
    {
72
        $params = $this->daux->getParams();
73
74
        $confluence = $params['confluence'];
75
        $this->prefix = trim($confluence['prefix']) . ' ';
76
        if ($this->prefix == ' ') {
77
            $this->prefix = '';
78
        }
79
80
        $tree = $this->runAction(
81
            'Generating Tree ...',
82
            $width,
83
            function() use ($params) {
84
                $tree = $this->generateRecursive($this->daux->tree, $params);
85
                $tree['title'] = $this->prefix . $params['title'];
86
87
                return $tree;
88
            }
89
        );
90
91
        $output->writeln('Start Publishing...');
92
93
        $publisher = new Publisher($confluence);
94
        $publisher->output = $output;
95
        $publisher->width = $width;
96
        $publisher->publish($tree);
97
    }
98
99
    private function generateRecursive(Directory $tree, Config $params, $base_url = '')
100
    {
101
        $final = ['title' => $this->prefix . $tree->getTitle()];
102
        $params['base_url'] = $params['base_page'] = $base_url;
103
104
        $params['image'] = str_replace('<base_url>', $base_url, $params['image']);
105
        if ($base_url !== '') {
106
            $params['entry_page'] = $tree->getFirstPage();
107
        }
108
        foreach ($tree->getEntries() as $key => $node) {
109
            if ($node instanceof Directory) {
110
                $final['children'][$this->prefix . $node->getTitle()] = $this->generateRecursive(
111
                    $node,
112
                    $params,
113
                    '../' . $base_url
114
                );
115
            } elseif ($node instanceof Content) {
116
                $params['request'] = $node->getUrl();
117
118
                $contentType = $this->daux->getContentTypeHandler()->getType($node);
119
120
                $data = [
121
                    'title' => $this->prefix . $node->getTitle(),
0 ignored issues
show
Bug introduced by Stéphane Goetz
Are you sure $node->getTitle() of type array|mixed|string can be used in concatenation? ( Ignorable by Annotation )

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

121
                    'title' => $this->prefix . /** @scrutinizer ignore-type */ $node->getTitle(),
Loading history...
122
                    'file' => $node,
123
                    'page' => ContentPage::fromFile($node, $params, $contentType),
124
                ];
125
126
                if ($key == 'index.html') {
127
                    $final['title'] = $this->prefix . $tree->getTitle();
128
                    $final['file'] = $node;
129
                    $final['page'] = $data['page'];
130
                } else {
131
                    $final['children'][$data['title']] = $data;
132
                }
133
            }
134
        }
135
136
        return $final;
137
    }
138
}
139