Completed
Pull Request — development (#783)
by Nick
04:28
created

ArticlesController::index()   D

Complexity

Conditions 21
Paths 108

Size

Total Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
nc 108
nop 0
dl 0
loc 86
rs 4.1
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace OcLegacy\Controller;
4
5
use Doctrine\DBAL\Connection;
6
use Oc\AbstractController;
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\Routing\Annotation\Route;
9
10
class ArticlesController extends AbstractController
11
{
12
    /**
13
     * @var Connection
14
     */
15
    private $connection;
16
17
    public function __construct(Connection $connection)
18
    {
19
        $this->connection = $connection;
20
    }
21
22
    /**
23
     * @Route("/articles.php", name="legacy_articles")
24
     */
25
    public function index()
26
    {
27
        global $opt, $tpl;
28
29
        ob_start();
30
31
        //get the article name to display
32
        $article = '';
33
        $language = $opt['template']['locale'];
34
        if (isset($_REQUEST['page']) &&
35
            (mb_strpos($_REQUEST['page'], '.') === false) &&
36
            (mb_strpos($_REQUEST['page'], '/') === false) &&
37
            (mb_strpos($_REQUEST['page'], '\\') === false)
38
        ) {
39
            $article = $_REQUEST['page'];
40
        }
41
42
        if ($article === '') {
43
            //no article specified
44
            $tpl->redirect('index.php');
45
        } elseif (isset($_REQUEST['wiki'])) {
46
            $tpl->redirect(helppageurl($article));
47
        } elseif (!file_exists($opt['stylepath'] . '/articles/' . $language . '/' . $article . '.tpl')) {
48
            // does article exist in default-language?
49
            $file = $opt['stylepath'] . '/articles/' . $opt['template']['default']['article_locale'] . '/' . $article . '.tpl';
50
            if (file_exists($file)) {
51
                $language = $opt['template']['default']['article_locale'];
52
            } elseif (file_exists($opt['stylepath'] . '/articles/EN/' . $article . '.tpl')) {
53
                $language = 'EN';
54
            } else {
55
                // use any
56
                $language = false;
57
                if ($hDir = opendir($opt['stylepath'] . '/articles/')) {
58
                    while ((($sFile = readdir($hDir)) !== false) && ($language === false)) {
59
                        if ($sFile != '.'
60
                            && $sFile != '..'
61
                            && is_dir($opt['stylepath'] . '/articles/' . $sFile)
62
                            && file_exists($opt['stylepath'] . '/articles/' . $sFile . '/' . $article . '.tpl')
63
                        ) {
64
                            $language = $sFile;
65
                        }
66
                    }
67
                    closedir($hDir);
68
                }
69
70
                //article doesn't exists
71
                if ($language === false) {
72
                    $tpl->redirect('index.php');
73
                }
74
            }
75
        }
76
77
        $tpl->name = 'articles';
78
79
        $tpl->caching = true;
80
        $tpl->cache_id = 'articles|' . $language . '|' . $article;
81
        $tpl->cache_lifetime = 43200;
82
83
        $tpl->menuitem = $this->connection->fetchColumn(
84
            'SELECT `id` FROM `sys_menu` WHERE `href`= :href LIMIT 1',
85
            [':href' => 'articles.php?page=' . urlencode($article)]
86
        );
87
        if ($tpl->menuitem == 0) {
88
            $tpl->redirect('index.php');
89
        }
90
91
        if (!$tpl->is_cached()) {
92
            $tpl->assign('article', $article);
93
            $tpl->assign('language', $language);
94
95
            /* prepare smarty vars for special pages ...
96
             */
97
            if ($article === 'cacheinfo') {
98
                require_once __DIR__ . '/../../../lib2/logic/attribute.class.php';
99
                $attributes = \attribute::getSelectableAttributesListArray(true);
100
                $tpl->assign('attributes', $attributes);
101
            }
102
        }
103
104
        $tpl->display();
105
106
        $content = ob_get_contents();
107
        ob_clean();
108
109
        return Response::create($content);
110
    }
111
}
112