Passed
Push — i18n ( 63f136...52f30c )
by Arnaud
03:22
created

Page::getOutputFile()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 7

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 7
eloc 18
c 1
b 1
f 0
nc 16
nop 2
dl 0
loc 30
ccs 19
cts 19
cp 1
crap 7
rs 8.8333
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Renderer;
12
13
use Cecil\Collection\Page\Page as PageItem;
14
use Cecil\Config;
15
16
/**
17
 * Class Renderer\Page.
18
 */
19
class Page
20
{
21
    /** @var Config */
22
    protected $config;
23
24
    /**
25
     * @param Config $config
26
     */
27 1
    public function __construct(Config $config)
28
    {
29 1
        $this->config = $config;
30 1
    }
31
32
    /**
33
     * Returns the path to the output (rendered) file.
34
     *
35
     * Use cases:
36
     * - default: path + suffix + extension (ie: blog/post-1/index.html)
37
     * - subpath: path + subpath + suffix + extension (ie: blog/post-1/amp/index.html)
38
     * - ugly: path + extension (ie: 404.html, sitemap.xml, robots.txt)
39
     * - path only (ie: _redirects)
40
     * - l10n: language + path + suffix + extension (ie: fr/blog/page/index.html)
41
     *
42
     * @param PageItem $page
43
     * @param string   $format Output format (ie: html, amp, json, etc.)
44
     *
45
     * @return string
46
     */
47 1
    public function getOutputFile(PageItem $page, string $format): string
48
    {
49 1
        $path = $page->getPath();
50 1
        $subpath = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $subpath is dead and can be removed.
Loading history...
51 1
        $suffix = '/index';
0 ignored issues
show
Unused Code introduced by
The assignment to $suffix is dead and can be removed.
Loading history...
52 1
        $extension = 'html';
0 ignored issues
show
Unused Code introduced by
The assignment to $extension is dead and can be removed.
Loading history...
53 1
        $uglyurl = (bool) $page->getVariable('uglyurl');
54 1
        $language = $page->getVariable('language');
55
        // site config
56 1
        $subpath = (string) $this->config->getOutputFormatProperty($format, 'subpath');
57 1
        $suffix = (string) $this->config->getOutputFormatProperty($format, 'suffix');
58 1
        $extension = (string) $this->config->getOutputFormatProperty($format, 'extension');
59
        // if ugly URL: not suffix
60 1
        if ($uglyurl) {
61 1
            $suffix = '';
62
        }
63
        // add extension
64 1
        if ($extension) {
65 1
            $extension = \sprintf('.%s', $extension);
66
        }
67
        // homepage special case: path = 'index'
68 1
        if (empty($path) && empty($suffix)) {
69 1
            $path = 'index';
70
        }
71
        // do not prefix URL for default language
72 1
        if ($language == $this->config->getLanguageDefault() || $language === null) {
73 1
            $language = '';
74
        }
75
76 1
        return \Cecil\Util::joinPath($language, $path, $subpath, $suffix).$extension;
77
    }
78
79
    /**
80
     * Returns the public URL.
81
     *
82
     * @param PageItem $page
83
     * @param string   $format Output format (ie: html, amp, json, etc.)
84
     *
85
     * @return string
86
     */
87 1
    public function getUrl(PageItem $page, string $format = 'html'): string
88
    {
89 1
        $uglyurl = $page->getVariable('uglyurl') ? true : false;
90 1
        $output = $this->getOutputFile($page, $format);
91
92 1
        if (!$uglyurl) {
93 1
            $output = str_replace('index.html', '', $output);
94
        }
95
96 1
        return $output;
97
    }
98
}
99