Completed
Push — master ( 2031fa...cf579d )
by Rigel Kent
01:29
created

WebsiteSource::getDOMDocumentArticle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Cion\TextToSpeech\Sources;
4
5
use Cion\TextToSpeech\Contracts\Source as SourceContract;
6
use DOMDocument;
7
use DOMNodeList;
8
use RecursiveIteratorIterator;
9
10
class WebsiteSource implements SourceContract
11
{
12
    /**
13
     * Handles in getting the text from source.
14
     *
15
     * @param  string $data
16
     * @return string
17
     */
18
    public function handle(string $data): string
19
    {
20
        $articles = $this->getDOMDocumentArticle($data);
21
22
        if ($articles === null)
23
        {
24
            return '';
25
        }
26
27
        return $this->getTextFromArticle($articles);
28
    }
29
30
    /**
31
     * Get the DOM Node List of article tag.
32
     *
33
     * @return DOMNodeList|null
34
     */
35
    protected function getDOMDocumentArticle(string $url)
36
    {
37
        $dom = new DOMDocument();
38
        @$dom->loadHTML(file_get_contents($url));
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
39
        $element = $dom->getElementsByTagName('article')->item(0);
40
41
        if ($element !== null)
42
        {
43
            return $element->childNodes;
44
        }
45
    }
46
47
    /**
48
     * Get text from the articles DOM Node List.
49
     *
50
     * @param DOMNodeList $articles
51
     * @return string
52
     */
53
    protected function getTextFromArticle(DOMNodeList $articles): string
54
    {
55
        $text = '';
56
57
        for ($i = 0; $i < $articles->length; $i++)
58
        {
59
            // Check element if there is a childNodes
60
            if ($articles->item($i)->childNodes === null)
61
            {
62
                continue;
63
            }
64
65
            $dit = new RecursiveIteratorIterator(
66
                new RecursiveDOMIterator($articles->item($i)),
67
                RecursiveIteratorIterator::SELF_FIRST
68
            );
69
            foreach ($dit as $node)
70
            {
71
                if ($node->nodeName === 'p')
72
                {
73
                    $text .= $node->textContent;
74
                }
75
            }
76
        }
77
        
78
        return $text;
79
    }
80
}