Passed
Push — master ( 8ca07b...393fa3 )
by Dev
11:40
created

LinksVisualizer::loadLinks()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 9
c 1
b 0
f 1
nc 3
nop 0
dl 0
loc 14
rs 9.6111
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 (
44
                $r['To'] > 0 // pas de liens externe
45
                && isset($this->results['nodes'][$r['From']]) && isset ($this->results['nodes'][$r['To']])
46
            )
47
            $this->results['links'][] = ['target' => $r['From'], 'source' => $r['To']];
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 ($url->mime_type == 1) //seulement html
59
            $this->results['nodes'][$url->id] = ['id' => $url->id, 'pagerank' => $url->pagerank, 'uri' => $url->uri];
60
        }
61
    }
62
}
63