Passed
Pull Request — master (#1822)
by Arnaud
08:29 queued 03:25
created

Page::getOutputFilePath()   B

Complexity

Conditions 9
Paths 32

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 9

Importance

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