IndexController::initialize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sitemap\Controller;
4
5
use Application\Mvc\Controller;
6
use Cms\Model\Language;
7
use Application\Mvc\Router\DefaultRouter;
8
9
10
class IndexController extends Controller
11
{
12
    private $cacheViewKey;
13
    private $models;
14
    private $links = [];
15
16
    public function initialize()
17
    {
18
        $this->cacheViewKey =  HOST_HASH . md5('Sitemap\Model\Sitemap');
19
20
        $this->models = [
21
            [
22
                'class' => 'Publication',
23
                'model' => 'Publication',
24
                'where' => "", // preview_inner='0'  ,  etc.
25
                'getLink' => function($model, $lang){
26
                    return $this->langUrlCustom([
27
                        'for' => 'publication',
28
                        'type' => $model->getTypeSlug(),
29
                        'slug' => $model->getSlug()], $lang);
30
                }
31
            ],[
32
                'class' => 'Page',
33
                'model' => 'Page',
34
                'getLink'      => function($model, $lang){
35
                    return $this->langUrlCustom([
36
                        'for' => 'page',
37
                        'slug' => $model->getSlug()], $lang);
38
                },
39
            ]
40
        ];
41
    }
42
43
44
    public function indexAction()
45
    {
46
        $this->view->setRenderLevel( \Phalcon\Mvc\View::LEVEL_NO_RENDER );
47
        $cache = $this->getDi()->get('cache');
0 ignored issues
show
Bug introduced by
The method get cannot be called on $this->getDi() (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
48
        $sitemap_xml = $cache->get($this->cacheViewKey);
49
50
        if(!$sitemap_xml){
51
            $langs = Language::find(['columns' => 'iso,primary']);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $langs is correct as \Cms\Model\Language::fin...mns' => 'iso,primary')) (which targets Phalcon\Mvc\Model::find()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
52
53
            //link(s) for main-page(s)
54
            foreach ($langs as $lang){
0 ignored issues
show
Bug introduced by
The expression $langs of type null is not traversable.
Loading history...
55
                $suffix = !$lang['primary'] ? $lang['iso'] . '/' : '';
56
                $this->links[] = [
57
                    'url' => 'http://' . $_SERVER['HTTP_HOST'] .  '/' . $suffix,
58
                    'updated_at' => date('c',time()),
59
                ];
60
            }
61
62
            foreach ($this->models as $m) {
63
                $class_name = '\\' . $m['class'] . '\Model\\' . $m['model'];
64
                $where      = !empty($m['where']) ? $m['where'] : '';
65
66
                $rows = $class_name::find($where);
67
68
                foreach ($langs as $lang) {
0 ignored issues
show
Bug introduced by
The expression $langs of type null is not traversable.
Loading history...
69
                    foreach ($rows as $row) {
70
                        $row::setCustomLang($lang->iso);
71
                        if($row->getSlug() !== 'index' && $row->getTitle()){
72
                            $this->links[] = [
73
                                'url'        => 'http://' . $_SERVER['HTTP_HOST'] .  $m['getLink']($row, $lang->iso),
74
                                'updated_at' => date('c', strtotime($row->getUpdatedAt())),
75
                            ];
76
                        }
77
                    }
78
                }
79
80
                $sitemap_xml = $this->getRawXml();
81
                $cache->save($this->cacheViewKey, $sitemap_xml);
82
            }
83
        }
84
85
        $this->response->setHeader("Content-Type", "text/xml");
86
        $this->response->setContent($sitemap_xml);
87
        return $this->response->send();
88
    }
89
90 View Code Duplication
    public function langUrlCustom($params, $lang)
91
    {
92
        $routeName = $params['for'];
93
        $routeName = DefaultRouter::ML_PREFIX . $routeName . '_' . $lang;
94
        $params['for'] = $routeName;
95
        return $this->url->get($params);
96
    }
97
98
    private function getRawXml()
99
    {
100
        $this->view->setRenderLevel( \Phalcon\Mvc\View::LEVEL_ACTION_VIEW );
101
        $this->view->links = $this->links;
0 ignored issues
show
Bug introduced by
Accessing links on the interface Phalcon\Mvc\ViewInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
102
        $this->view->start();
103
        $this->view->setLayoutsDir('../views/');
104
        $this->view->render('index', 'index');
105
        $this->view->finish();
106
        return $this->view->getContent();
107
    }
108
109
}