Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Completed
Pull Request — master (#61)
by Oliver
01:39
created

checkForTemplatesAndUniquePagesNotDistinct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 15
rs 9.4285
cc 3
eloc 7
nc 3
nop 0
1
<?php
2
3
namespace Backpack\PageManager\app;
4
5
use Illuminate\Support\Collection;
6
7
trait TraitReflections
8
{
9
    /**
10
     * Check for equal named templates and unique pages.
11
     *
12
     * As the method name of a unique page will also be the template name in the database,
13
     * we must ensure that there are not any equal names defined.
14
     * If different Models (and so different tables in the database) are used, this condition is obsolete.
15
     *
16
     * @throws \Exception
17
     */
18
    public function checkForTemplatesAndUniquePagesNotDistinct()
19
    {
20
        if (config('backpack.pagemanager.page_model_class') !== config('backpack.pagemanager.unique_page_model_class')) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 121 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
21
            return;
22
        }
23
24
        // use the internal function directly to prevent an 503 abort if no pages are defined
25
        $uniquePages = $this->loadUniquePages()->pluck('name');
26
        // use the internal function directly to prevent an 503 abort if no templates are defined
27
        $templates = $this->loadTemplates()->pluck('name');
28
29
        if ($uniquePages->intersect($templates)->isNotEmpty()) {
30
            throw new \Exception('Templates and unique pages must not have the same function names when same model class is used.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
31
        }
32
    }
33
34
    /*
35
    |--------------------------------------------------------------------------
36
    | UNIQUE PAGES
37
    |--------------------------------------------------------------------------
38
    */
39
40
    /**
41
     * Load all defined unique pages.
42
     *
43
     * @return Collection
44
     */
45
    private function loadUniquePages()
46
    {
47
        $pages_trait = new \ReflectionClass('App\UniquePages');
48
        $pages = $pages_trait->getMethods(\ReflectionMethod::IS_PRIVATE);
49
50
        return collect($pages);
51
    }
52
53
    /**
54
     * Get all defined unique pages.
55
     *
56
     * @return Collection
57
     */
58
    public function getUniquePages()
59
    {
60
        $pages = $this->loadUniquePages();
61
62
        if (! count($pages)) {
63
            abort(503, trans('backpack::pagemanager.template_not_found'));
64
        }
65
66
        return $pages;
67
    }
68
69
    /**
70
     * Get all defined unique page names.
71
     *
72
     * @return Collection
73
     */
74
    public function getUniquePageNames()
75
    {
76
        return $this->getUniquePages()->pluck('name');
77
    }
78
79
    /**
80
     * Get the page names keyed with slugs.
81
     *
82
     * @return Collection
83
     */
84
    public function getUniqueSlugs()
85
    {
86
        return $this->getUniquePageNames()->mapWithKeys(function ($name) {
87
            return [str_slug($name) => $name];
88
        });
89
    }
90
91
92
    /*
93
    |--------------------------------------------------------------------------
94
    | TEMPLATES
95
    |--------------------------------------------------------------------------
96
    */
97
98
    /**
99
     * Load all defined templates.
100
     *
101
     * @return Collection
102
     */
103
    private function loadTemplates()
104
    {
105
        $templates_trait = new \ReflectionClass('App\PageTemplates');
106
        $templates = $templates_trait->getMethods(\ReflectionMethod::IS_PRIVATE);
107
108
        return collect($templates);
109
    }
110
111
    /**
112
     * Get all defined templates.
113
     *
114
     * @return Collection
115
     */
116
    public function getTemplates($template_name = false)
0 ignored issues
show
Unused Code introduced by
The parameter $template_name is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style Naming introduced by
The parameter $template_name is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
117
    {
118
        $templates = $this->loadTemplates();
119
120
        if (! count($templates)) {
121
            abort(503, trans('backpack::pagemanager.template_not_found'));
122
        }
123
124
        return collect($templates);
125
    }
126
127
    /**
128
     * Get all defined template names.
129
     *
130
     * @return Collection
131
     */
132
    public function getTemplateNames()
133
    {
134
        return $this->getTemplates()->pluck('name');
135
    }
136
}
137