Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

ExternalBody::generate()   A

Complexity

Conditions 3
Paths 6

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 3
nc 6
nop 2
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Generator;
10
11
use Cecil\Collection\Collection as PageCollection;
12
use Cecil\Collection\Page\Page;
13
use Cecil\Converter\Converter;
14
15
/**
16
 * Class Homepage.
17
 */
18
class ExternalBody extends AbstractGenerator implements GeneratorInterface
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    public function generate(PageCollection $pageCollection, \Closure $messageCallback)
24
    {
25
        $generatedPages = new PageCollection();
26
27
        $filteredPages = $pageCollection->filter(function (Page $page) {
28
            return null !== $page->getVariable('external');
29
        });
30
31
        /* @var $page Page */
32
        foreach ($filteredPages as $page) {
33
            try {
34
                $pageContent = file_get_contents($page->getVariable('external'), false);
35
                $html = (new Converter())
36
                    ->convertBody($pageContent);
37
                $page->setHtml($html);
38
39
                $generatedPages->add($page);
40
            } catch (\Exception $e) {
41
                $error = sprintf('Cannot get contents from %s', $page->getVariable('external'));
42
                $message = sprintf("Unable to generate '%s': %s", $page->getId(), $error);
43
                call_user_func_array($messageCallback, ['GENERATE_ERROR', $message]);
44
            }
45
        }
46
47
        return $generatedPages;
48
    }
49
}
50