Passed
Push — v5 ( c0775f...b45ef7 )
by Alexey
16:22
created

Head::faviconPath()   B

Complexity

Conditions 8
Paths 5

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 8
nc 5
nop 0
dl 0
loc 12
rs 7.7777
c 0
b 0
f 0
1
<?php
2
3
namespace Inji\View\Page;
4
5
use Inji\App;
6
use Inji\View\Page;
7
8
class Head {
9
    /**
10
     * @var Page
11
     */
12
    public $page;
13
14
    function __construct(Page $page) {
15
        $this->page = $page;
16
    }
17
18
    public function generate() {
19
        $html = '';
20
        foreach ($this->elements() as $el) {
21
            $html .= call_user_func_array(['Inji\Html', 'el'], $el) . "\n";
22
        }
23
        return $html;
24
    }
25
26
    public function elements() {
27
        $els = [
28
            ['title', $this->page->title],
29
            ['link', '', ['rel' => 'shortcut icon', 'href' => $this->faviconPath()], null]
30
        ];
31
        foreach ($this->getMetaTags() as $meta) {
32
            $els[] = ['meta', $meta, '', null];
33
        }
34
35
        if (empty($this->page->template->config['noInjects']) && !empty(\Inji::$config['assets']['js'])) {
36
            foreach (\Inji::$config['assets']['js'] as $js) {
37
                $this->customAsset('js', $js);
38
            }
39
        }
40
41
        $this->checkNeedLibs();
0 ignored issues
show
Bug introduced by
The method checkNeedLibs() does not exist on Inji\View\Page\Head. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
        $this->/** @scrutinizer ignore-call */ 
42
               checkNeedLibs();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
        $this->parseCss();
0 ignored issues
show
Bug introduced by
The method parseCss() does not exist on Inji\View\Page\Head. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

42
        $this->/** @scrutinizer ignore-call */ 
43
               parseCss();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
43
        echo "\n        <script src='" . Statics::file("/static/system/js/Inji.js") . "'></script>";
0 ignored issues
show
Bug introduced by
The type Inji\View\Page\Statics was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
        return $els;
45
    }
46
47
    public function customAsset($type, $asset, $lib = false) {
48
        if (!$lib) {
49
            $this->dynAssets[$type][] = $asset;
0 ignored issues
show
Bug Best Practice introduced by
The property dynAssets does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
50
        } else {
51
            if (empty($this->libAssets[$type][$lib]) || !in_array($asset, $this->libAssets[$type][$lib])) {
52
                $this->libAssets[$type][$lib][] = $asset;
0 ignored issues
show
Bug Best Practice introduced by
The property libAssets does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
53
            }
54
        }
55
    }
56
57
    public function faviconPath() {
58
        if (!empty(App::$primary->config['site']['favicon']) && file_exists(App::$primary->path . '/' . App::$primary->config['site']['favicon'])) {
59
            return App::$primary->config['site']['favicon'];
60
        }
61
        if (!empty($this->page->template->config['favicon']) && file_exists($this->page->template->path . "/{$this->page->template->config['favicon']}")) {
62
            return $this->page->template->name . '/' . $this->page->template->config['favicon'];
63
        }
64
        if (!empty($this->page->template->config['favicon']) && file_exists($this->page->app->path . "/static/images/{$this->page->template->config['favicon']}")) {
65
            return '/static/images/' . $this->page->template->config['favicon'];
66
        }
67
        if (file_exists($this->page->app->path . '/static/images/favicon.ico')) {
68
            return '/static/images/favicon.ico';
69
        }
70
    }
71
72
    public function getMetaTags() {
73
        $metas = [];
74
75
        if (!empty($this->page->app->config['site']['keywords'])) {
76
            $metas['metaName:keywords'] = ['name' => 'keywords', 'content' => $this->page->app->config['site']['keywords']];
77
        }
78
        if (!empty($this->page->app->config['site']['description'])) {
79
            $metas['metaName:description'] = ['name' => 'description', 'content' => $this->page->app->config['site']['description']];
80
        }
81
        if (!empty($this->page->app->config['site']['metatags'])) {
82
            foreach ($this->page->app->config['site']['metatags'] as $meta) {
83
                if (!empty($meta['name'])) {
84
                    $metas['metaName:' . $meta['name']] = $meta;
85
                } elseif (!empty($meta['property'])) {
86
                    $metas['metaProperty:' . $meta['property']] = $meta;
87
                }
88
            }
89
        }
90
        if ($this->dynMetas) {
0 ignored issues
show
Bug Best Practice introduced by
The property dynMetas does not exist on Inji\View\Page\Head. Did you maybe forget to declare it?
Loading history...
91
            $metas = array_merge($metas, $this->dynMetas);
92
        }
93
        return $metas;
94
    }
95
}