Passed
Push — 8.x-dev ( 640d34...2250ec )
by Arnaud
04:51 queued 16s
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 0
Metric Value
cc 9
eloc 17
nc 32
nop 2
dl 0
loc 30
ccs 16
cts 16
cp 1
crap 9
rs 8.0555
c 0
b 0
f 0
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 1
    public function __construct(\Cecil\Config $config)
27
    {
28 1
        $this->config = $config;
29
    }
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 1
    public function getOutputFilePath(PageItem $page, string $format): string
45
    {
46 1
        $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
        // is ugly URL?
53 1
        if ($uglyurl) {
54 1
            $filename = '';
55
        }
56
        // add extension if exists
57 1
        if ($extension) {
58 1
            $extension = sprintf('.%s', $extension);
59
        }
60
        // homepage special case (need "index")
61 1
        if (empty($path) && empty($filename)) {
62 1
            $path = 'index';
63
        }
64
        // do not prefix path with language code for the default language (and default language home page)
65 1
        if ($language === null || ($language == $this->config->getLanguageDefault() && !$this->config->get('language.prefix'))) {
66 1
            $language = '';
67
        }
68
        // do not prefix "not multilingual" virtual pages
69 1
        if ($page->getVariable('multilingual') === false) {
70
            $language = '';
71
        }
72
73
        return \Cecil\Util::joinPath($language, $path, $subpath, $filename) . $extension;
74
    }
75
76
    /**
77
     * Returns the public URL.
78 1
     *
79
     * @param PageItem $page
80 1
     * @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
    {
84
        $output = $this->getOutputFilePath($page, $format);
85 1
86
        // 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