Passed
Push — Assets/Image ( bd550f...7a290d )
by Arnaud
09:48 queued 04:01
created

PagesSave::process()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 37
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 23
nc 5
nop 0
dl 0
loc 37
rs 9.2408
c 0
b 0
f 0
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\Step;
10
11
use Cecil\Collection\Page\Page;
12
use Cecil\Exception\Exception;
13
use Cecil\Util;
14
15
/**
16
 * Pages saving.
17
 */
18
class PagesSave extends AbstractStep
19
{
20
    /**
21
     * {@inheritdoc}
22
     *
23
     * @throws Exception
24
     */
25
    public function init($options)
26
    {
27
        if ($options['dry-run']) {
28
            $this->process = false;
29
            call_user_func_array($this->builder->getMessageCb(), ['SAVE', 'Dry run']);
30
31
            return;
32
        }
33
34
        Util::getFS()->mkdir($this->config->getOutputPath());
35
36
        $this->process = true;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @throws Exception
43
     */
44
    public function process()
45
    {
46
        call_user_func_array($this->builder->getMessageCb(), ['SAVE', 'Saving pages']);
47
48
        /* @var $page Page */
49
        $filteredPages = $this->builder->getPages()->filter(function (Page $page) {
50
            return !empty($page->getVariable('rendered'));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $page->getVariable('rendered') targeting Cecil\Collection\Page\Page::getVariable() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
51
        });
52
        $max = count($filteredPages);
53
54
        $count = 0;
55
        foreach ($filteredPages as $page) {
56
            $count++;
57
            $message = [];
58
59
            foreach ($page->getVariable('rendered') as $format => $rendered) {
60
                if (false === $pathname = $page->getOutputFile($format, $this->config)) {
61
                    throw new Exception(sprintf(
62
                        "Can't get pathname of page '%s' (format: '%s')",
63
                        $page->getId(),
64
                        $format
65
                    ));
66
                }
67
                $pathname = $this->cleanPath($this->config->getOutputPath().'/'.$pathname);
68
69
                try {
70
                    Util::getFS()->dumpFile($pathname, $rendered['output']);
71
                } catch (\Exception $e) {
72
                    throw new Exception($e->getMessage());
73
                }
74
75
                $message[] = substr($pathname, strlen($this->config->getDestinationDir()) + 1);
76
            }
77
78
            call_user_func_array(
79
                $this->builder->getMessageCb(),
80
                ['SAVE_PROGRESS', implode(', ', $message), $count, $max]
81
            );
82
        }
83
    }
84
85
    /**
86
     * Remove unnecessary slashes.
87
     *
88
     * @param string $pathname
89
     *
90
     * @return string
91
     */
92
    protected function cleanPath($pathname)
93
    {
94
        return preg_replace('#/+#', '/', $pathname);
95
    }
96
}
97