Layout::verifyLayoutOptions()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 5
nop 1
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
1
<?php
2
3
namespace JumpGate\ViewResolution\Resolvers;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\View\Factory;
7
use Illuminate\Http\Request;
8
9
class Layout
10
{
11
    /**
12
     * @var \Illuminate\View\View
13
     */
14
    public $layout;
15
16
    /**
17
     * @var \Illuminate\View\View
18
     */
19
    protected $view;
20
21
    /**
22
     * @var \Illuminate\Http\Request
23
     */
24
    protected $request;
25
26
    /**
27
     * @var \Illuminate\Config\Repository
28
     */
29
    protected $config;
30
31
    /**
32
     * @var array
33
     */
34
    protected $layoutOptions;
35
36
    /**
37
     * @param \Illuminate\View\Factory      $view
38
     * @param \Illuminate\Http\Request      $request
39
     * @param \Illuminate\Config\Repository $config
40
     */
41
    public function __construct(Factory $view, Request $request, Repository $config)
0 ignored issues
show
Bug introduced by
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
42
    {
43
        $this->view    = $view;
0 ignored issues
show
Documentation Bug introduced by
It seems like $view of type object<Illuminate\View\Factory> is incompatible with the declared type object<Illuminate\View\View> of property $view.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
44
        $this->request = $request;
45
        $this->config  = $config;
46
    }
47
48
    /**
49
     * Set up the initial layout to be used.
50
     *
51
     * @param $layoutOptions
52
     *
53
     * @return $this
54
     */
55 View Code Duplication
    public function setUp($layoutOptions)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
    {
57
        $this->layoutOptions = $this->verifyLayoutOptions($layoutOptions);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->verifyLayoutOptions($layoutOptions) of type * is incompatible with the declared type array of property $layoutOptions.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
58
        $this->layout        = $this->determineLayout(null);
59
        $this->setPageTitle();
60
        $this->layout->content = null;
0 ignored issues
show
Bug introduced by
Accessing content on the interface Illuminate\Contracts\View\View 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...
61
62
        return $this;
63
    }
64
65
    /**
66
     * Change the selected layout to something else and ready it for content.
67
     *
68
     * @param $view
69
     *
70
     * @return $this
71
     */
72 View Code Duplication
    public function change($view)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $this->layout = $this->determineLayout($view);
75
        $this->setPageTitle();
76
        $this->layout->content = null;
0 ignored issues
show
Bug introduced by
Accessing content on the interface Illuminate\Contracts\View\View 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...
77
78
        return $this;
79
    }
80
81
    /**
82
     * Auto determine a logic page title.
83
     */
84
    public function setPageTitle()
85
    {
86
        $area     = $this->request->segment(1);
87
        $location = ($this->request->segment(2) != null ? ': ' . ucwords($this->request->segment(2)) : '');
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->request->segment(2) of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
88
89
        if ($area != null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $area of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
90
            $pageTitle = ucwords($area) . $location;
91
        } else {
92
            $pageTitle = $this->config->get('core::siteName') . ($this->request->segment(1) != null ? ': ' . ucwords($this->request->segment(1)) : '');
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $this->request->segment(1) of type string|null against null; this is ambiguous if the string can be empty. Consider using a strict comparison !== instead.
Loading history...
93
        }
94
95
        $this->view->share('pageTitle', $pageTitle);
96
    }
97
98
    /**
99
     * Determine which layout to use based on the request.
100
     *
101
     * @param $layout
102
     *
103
     * @return \Illuminate\Contracts\View\View
104
     */
105
    protected function determineLayout($layout)
106
    {
107
        if (! is_null($layout)) {
108
            return $this->view->make($layout);
109
        }
110
111
        if (is_string($this->layout)) {
112
            return $this->view->make($this->layout);
113
        }
114
115
        if (is_null($this->layout) && $this->request->ajax()) {
116
            return $this->view->make($this->layoutOptions['ajax']);
117
        }
118
119
        if (is_null($this->layout)) {
120
            return $this->view->make($this->layoutOptions['default']);
121
        }
122
123
        return $this->layout;
124
    }
125
126
    /**
127
     * Make sure that we have a valid set of layout options.
128
     *
129
     * @param $layoutOptions
130
     *
131
     * @return mixed
132
     */
133
    private function verifyLayoutOptions($layoutOptions)
134
    {
135
        // If using the config details, go with that instead of expecting passed options.
136
        if (config('jumpgate.view-resolution.load_layout')) {
137
            return config('jumpgate.view-resolution.layout_options');
138
        }
139
140
        // If using the passed options, make sure they are valid options.
141
        if (! is_array($layoutOptions)) {
142
            throw new \InvalidArgumentException('The layoutOptions must be an array.');
143
        }
144
        if (! isset($layoutOptions['default'])) {
145
            throw new \InvalidArgumentException('The layoutOptions must have a default layout view.');
146
        }
147
        if (! isset($layoutOptions['ajax'])) {
148
            throw new \InvalidArgumentException('The layoutOptions must have a ajax layout view.');
149
        }
150
151
        return $layoutOptions;
152
    }
153
}
154