Passed
Push — nested-sections ( 2d40b2...7dbf34 )
by Arnaud
12:03 queued 05:37
created

Page::getOutputFilePath()   B

Complexity

Conditions 9
Paths 32

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 9

Importance

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