Completed
Push — master ( ef0404...81c807 )
by Craig
23:17 queued 14:56
created

DocController::setBasePath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\CoreInstallerBundle\Controller;
13
14
use Symfony\Component\HttpFoundation\Response;
15
use Symfony\Component\HttpFoundation\Request;
16
use Michelf\MarkdownExtra;
17
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
18
use Zikula\Common\Translator\TranslatorInterface;
19
use Zikula\Core\Response\PlainResponse;
20
21
/**
22
 * Class DocController
23
 */
24
class DocController
25
{
26
    /**
27
     * @var ZikulaHttpKernelInterface
28
     */
29
    private $kernel;
30
31
    /**
32
     * @var \Twig_Environment
33
     */
34
    private $twig;
35
36
    /**
37
     * @var MarkdownExtra
38
     */
39
    private $parser;
40
41
    /**
42
     * @var
43
     */
44
    private $basePath;
45
46
    /**
47
     * @var TranslatorInterface
48
     */
49
    private $translator;
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param ZikulaHttpKernelInterface $kernel
55
     * @param \Twig_Environment $twig
56
     * @param MarkdownExtra $parser
57
     * @param TranslatorInterface $translator
58
     */
59
    public function __construct(
60
        ZikulaHttpKernelInterface $kernel,
61
        \Twig_Environment $twig,
62
        MarkdownExtra $parser,
63
        TranslatorInterface $translator
64
    ) {
65
        $this->kernel = $kernel;
66
        $this->twig = $twig;
67
        $this->parser = $parser;
68
        $this->translator = $translator;
69
    }
70
71
    /**
72
     * @param Request $request
73
     * @param string $name
74
     * @return Response
75
     */
76
    public function displayAction(Request $request, $name = 'INSTALL-2.0.md')
77
    {
78
        $this->setBasePath($request);
79
80
        if (file_exists($this->basePath . "/$name")) {
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
81
            $content = file_get_contents($this->basePath . "/$name");
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
82
        } else {
83
            $content = $this->translator->__f('The file you requested (%s) could not be found.', ['%s' => "$name"]);
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $name instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
84
        }
85
        $content = $this->parser->defaultTransform($content);
86
        $templateParams = [
87
            'lang' => $request->getLocale(),
88
            'charset' => $this->kernel->getCharset(),
89
            'content' => $content,
90
        ];
91
        $response = new PlainResponse();
92
        $response->setContent($this->twig->render('ZikulaCoreInstallerBundle::doc.html.twig', $templateParams));
93
94
        return $response;
95
    }
96
97
    /**
98
     * set the base path for doc files, computing whether this is a Github clone or CI build.
99
     * @param Request $request
100
     */
101
    private function setBasePath(Request $request)
102
    {
103
        $paths = [
104
            $this->kernel->getRootDir() . '/../docs/' . $request->getLocale(), // localized in ci build
105
            $this->kernel->getRootDir() . '/../docs/en', // default in ci build
106
            $this->kernel->getRootDir() . '/../..' // github clone
107
        ];
108
        foreach ($paths as $docPath) {
109
            if ($path = realpath($docPath)) {
110
                $this->basePath = $path;
111
            }
112
        }
113
    }
114
}
115