LinksVisualizer::loadNodes()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 7
c 2
b 1
f 1
nc 3
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace PiedWeb\SeoPocketCrawler;
4
5
use League\Csv\Reader;
6
7
/**
8
 * Not used anymore...
9
 */
10
class LinksVisualizer
11
{
12
    /**
13
     * @var CrawlerConfig
14
     */
15
    protected $config;
16
17
    protected $results = ['nodes' => [], 'links' => []];
18
19
    public function __construct(string $id, ?string $dataDirectory = null)
20
    {
21
        $this->config = CrawlerConfig::loadFrom($id, $dataDirectory);
22
23
        //$this->loadNodes();
24
        //$this->loadLinks();
25
26
        file_put_contents(
27
            $this->config->getDataFolder().'/pagerank.html',
28
            file_get_contents(dirname(__FILE__).'/Resources/PageRankVisualizer.html')
29
        );
30
        /*
31
        file_put_contents(
32
            $this->config->getDataFolder().Recorder::LINKS_DIR.'/data.json',
33
            json_encode($this->results, JSON_PRETTY_PRINT)
34
        );**/
35
    }
36
37
    protected function loadLinks()
38
    {
39
        $csv = Reader::createFromPath($this->config->getDataFolder().Recorder::LINKS_DIR.'/Index.csv', 'r');
40
        $csv->setHeaderOffset(0);
41
        $records = $csv->getRecords();
42
        foreach ($records as $r) {
43
            if ($r['To'] > 0 // pas de liens externe
44
                && isset($this->results['nodes'][$r['From']]) && isset($this->results['nodes'][$r['To']])
45
            ) {
46
                $this->results['links'][] = ['target' => $r['From'], 'source' => $r['To']];
47
            }
48
        }
49
50
        $this->results['nodes'] = array_values($this->results['nodes']);
51
    }
52
53
    protected function loadNodes()
54
    {
55
        $urls = $this->config->getDataFromPreviousCrawl()['urls'];
56
57
        foreach ($urls as $url) {
58
            if (1 == $url->mime_type) { //seulement html
59
                $this->results['nodes'][$url->id] = [
60
                    'id' => $url->id,
61
                    'pagerank' => $url->pagerank,
62
                    'uri' => $url->uri,
63
                ];
64
            }
65
        }
66
    }
67
}
68