Completed
Pull Request — master (#2)
by
unknown
20:58
created

RichText::addRandomI()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: carlos
5
 * Date: 15/12/18
6
 * Time: 12:23
7
 */
8
9
namespace Kaliop\eZLoremIpsumBundle\Faker\Provider;
10
11
class RichText extends Base
12
{
13
14
    const P_TAG = "para";
15
    const A_TAG = "link";
16
    const E_TAG = "ezembed";
17
    const TABLE_TAG = "informaltable";
18
    const TR_TAG = "tr";
19
    const TD_TAG = "td";
20
    const UL_TAG = "itemizedlist";
21
    const OL_TAG = "orderedlist";
22
    const LI_TAG = "listitem";
23
    const H_TAG = "title";
24
    const EMPHASIS_TAG = "emphasis";
25
    const CUSTOM_TAG = "custom";
26
27
    /**
28
     * @param integer $maxDepth
29
     * @param integer $maxWidth
30
     *
31
     * @return string
32
     */
33
    public function randomRichText($maxDepth = 4, $maxWidth = 4)
34
    {
35
        $document = new \DOMDocument();
36
37
        $body = $document->createElementNS("http://docbook.org/ns/docbook", "section");
38
        $body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', 'http://www.w3.org/1999/xlink');
39
        $body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ezxhtml', 'http://ez.no/xmlns/ezpublish/docbook/xhtml');
40
        $body->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ezcustom', 'http://ez.no/xmlns/ezpublish/docbook/custom');
41
        $body->setAttribute('version', '5.0-variant ezpublish-1.0');
42
43
        $this->addRandomSubTree($body, $maxDepth, $maxWidth);
44
45
        $document->appendChild($body);
46
47
        $out = $document->saveXML();
48
49
        return $out;
50
    }
51
52
53
    private function addRandomSubTree(\DOMElement $root, $maxDepth, $maxWidth)
54
    {
55
        $maxDepth--;
56
        if ($maxDepth <= 0) {
57
            $this->addRandomLeaf($root);
58
            return $root;
59
        }
60
61
        $siblings = mt_rand(1, $maxWidth);
62
        for ($i = 0; $i < $siblings; $i++) {
63
            if ($maxDepth == 1) {
64
                $this->addRandomLeaf($root);
65
            } else {
66
                $this->addRandomSubTree($root, mt_rand(1, $maxDepth), $maxWidth);
67
            }
68
        };
69
        return $root;
70
    }
71
72 View Code Duplication
    private function addRandomLeaf(\DOMElement $node)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $rand = mt_rand(1, 10);
75
        switch($rand) {
76
            case 1:
77
                $this->addRandomE($node);
78
                break;
79
            case 2:
80
                $this->addRandomA($node);
81
                break;
82
            case 3:
83
                $this->addRandomOL($node);
84
                break;
85
            case 4:
86
                $this->addRandomUL($node);
87
                break;
88
            case 5:
89
                $this->addRandomH($node);
90
                break;
91
            case 6:
92
                $this->addRandomB($node);
93
                break;
94
            case 7:
95
                $this->addRandomI($node);
96
                break;
97
            case 8:
98
                $this->addRandomTable($node);
99
                break;
100
            case 9:
101
                $this->addRandomU($node);
102
                break;
103
            default:
104
                $this->addRandomP($node);
105
                break;
106
        }
107
    }
108
109
    private function addRandomE(\DOMElement $element)
110
    {
111
        $node = $element->ownerDocument->createElement(static::E_TAG);
112
        $node->setAttribute("view", "embed");
113
        /// @todo improve: generation of a radmon object id
114
        $node->setAttribute("xlink:href", 'ezcontent://' . mt_rand(1, 100000));
115
        $element->appendChild($node);
116
    }
117
118
    /**
119
     * @todo add random align
120
     * @param \DOMElement $element
121
     * @param int $maxLength
122
     */
123 View Code Duplication
    private function addRandomP(\DOMElement $element, $maxLength = 50)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        $p = $element->ownerDocument->createElement(static::P_TAG);
126
        $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength)));
127
        // left-aligned paragraphs have double frequency
128
        switch (mt_rand(1, 4)) {
129
            case 1:
130
                $p->setAttribute("ezxhtml:textalign", "right");
131
                break;
132
            case 2:
133
                $p->setAttribute("ezxhtml:textalign", "center");
134
                break;
135
        }
136
        $p->appendChild($text);
137
        $element->appendChild($p);
138
    }
139
140 View Code Duplication
    private function addRandomA(\DOMElement $element, $maxLength = 10)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength)));
143
        $node = $element->ownerDocument->createElement(static::A_TAG);
144
        $node->setAttribute("xlink:href", 'ezurl://28');
145
        $node->setAttribute('xlink:show', 'none');
146
        $node->appendChild($text);
147
        $this->wrapInParagraph($node, $element);
148
    }
149
150 View Code Duplication
    private function addRandomH(\DOMElement $element, $maxLength = 10)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $h = static::H_TAG;
153
        $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength)));
154
        $node = $element->ownerDocument->createElement($h);
155
        $node->setAttribute('ezxhtml:level', mt_rand(1, 6));
156
        $node->appendChild($text);
157
        $element->appendChild($node);
158
    }
159
160 View Code Duplication
    private function addRandomB(\DOMElement $element, $maxLength = 10)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength)));
163
        $node = $element->ownerDocument->createElement(static::EMPHASIS_TAG);
164
        $node->setAttribute('role', 'strong');
165
        $node->appendChild($text);
166
        $this->wrapInParagraph($node, $element);
167
    }
168
169 View Code Duplication
    private function addRandomU(\DOMElement $element, $maxLength = 10)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
170
    {
171
        $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength)));
172
        $node = $element->ownerDocument->createElement(static::EMPHASIS_TAG);
173
        $node->setAttribute('role', 'underlined');
174
        $node->appendChild($text);
175
        $this->wrapInParagraph($node, $element);
176
    }
177
178 View Code Duplication
    private function addRandomI(\DOMElement $element, $maxLength = 10)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
179
    {
180
        $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength)));
181
        $node = $element->ownerDocument->createElement(static::EMPHASIS_TAG);
182
        $node->setAttribute('role', 'emphasis');
183
        $node->appendChild($text);
184
        $this->wrapInParagraph($node, $element);
185
    }
186
187 View Code Duplication
    private function addRandomTable(\DOMElement $element, $maxRows = 10, $maxCols = 6, $maxLength = 10)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
    {
189
        $rows = mt_rand(1, $maxRows);
190
        $cols = mt_rand(1, $maxCols);
191
192
        $table = $element->ownerDocument->createElement(static::TABLE_TAG);
193
194
        for ($i = 0; $i < $rows; $i++) {
195
            $tr = $element->ownerDocument->createElement(static::TR_TAG);
196
            $table->appendChild($tr);
197
            for ($j = 0; $j < $cols; $j++) {
198
                $th = $element->ownerDocument->createElement(static::TD_TAG);
199
                $th->textContent = $this->getSentence(mt_rand(1, $maxLength));
200
                $tr->appendChild($th);
201
            }
202
        }
203
        $this->wrapInParagraph($table, $element);
204
    }
205
206 View Code Duplication
    private function addRandomUL(\DOMElement $element, $maxItems = 11, $maxLength = 4)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
    {
208
        $num = mt_rand(1, $maxItems);
209
        $ul = $element->ownerDocument->createElement(static::UL_TAG);
210
        for ($i = 0; $i < $num; $i++) {
211
            $li = $element->ownerDocument->createElement(static::LI_TAG);
212
            $lip = $element->ownerDocument->createElement(static::P_TAG);
213
            $lip->textContent = $this->getSentence(mt_rand(1, $maxLength));
214
            $li->appendChild($lip);
215
            $ul->appendChild($li);
216
        }
217
        $element->appendChild($ul);
218
    }
219
220 View Code Duplication
    private function addRandomOL(\DOMElement $element, $maxItems = 11, $maxLength = 4)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
    {
222
        $num = mt_rand(1, $maxItems);
223
        $ul = $element->ownerDocument->createElement(static::OL_TAG);
224
        for ($i = 0; $i < $num; $i++) {
225
            $li = $element->ownerDocument->createElement(static::LI_TAG);
226
            $lip = $element->ownerDocument->createElement(static::P_TAG);
227
            $lip->textContent = $this->getSentence(mt_rand(1, $maxLength));
228
            $li->appendChild($lip);
229
            $ul->appendChild($li);
230
        }
231
        $element->appendChild($ul);
232
    }
233
234 View Code Duplication
    protected function wrapInParagraph(\DOMElement $element, \DOMElement $parent, $maxLength = 10)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
235
    {
236
        $p = $element->ownerDocument->createElement(static::P_TAG);
237
        $chance = mt_rand(1, 4);
238
        if ($chance == 1 || $chance == 4) {
239
            $text = $element->ownerDocument->createTextNode($this->getSentence(mt_rand(1, $maxLength)) . ' ');
240
            $p->appendChild($text);
241
        }
242
        $p->appendChild($element);
243
        if ($chance == 2 || $chance == 4) {
244
            $text = $element->ownerDocument->createTextNode(' ' . $this->getSentence(mt_rand(1, $maxLength)));
245
            $p->appendChild($text);
246
        }
247
        $parent->appendChild($p);
248
    }
249
250
    protected function getSentence($nbWords = 6, $variableNbWords = true)
251
    {
252
        return $this->generator->sentence($nbWords, $variableNbWords);
253
    }
254
255
    protected function getUrl()
256
    {
257
        return $this->generator->url;
258
    }
259
}
260