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

ExternalBody   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 4
dl 0
loc 32
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A generate() 0 26 3
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