Completed
Push — master ( 7d04c8...2a4aa6 )
by Jan
10:55
created

PageController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 143
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 5
dl 143
loc 143
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 15 15 1
A associateRelationships() 49 49 3
A associateRelationshipsWithID() 41 41 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Page module controller
4
 * 
5
 * Controller for module Page
6
 * 
7
 * @category Controller
8
 * @subpackage Admin
9
 * @package Olapus
10
 * @author Jan Drda <[email protected]>
11
 * @copyright Jan Drda
12
 * @license https://opensource.org/licenses/MIT MIT
13
 */
14
15
namespace App\Http\Controllers\Admin;
16
17
use Illuminate\Http\Request;
18
use Illuminate\Support\Facades\Validator;
19
use Illuminate\Support\Facades\Auth;
20
21 View Code Duplication
class PageController extends AdminModuleController {
0 ignored issues
show
Duplication introduced by
This class 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...
22
23
    /**
24
     * Constructor
25
     * 
26
     * @var Request $request
27
     */
28
    public function __construct(Request $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
29
        parent::__construct();
30
31
        /**
32
         * Add lookup tables
33
         */
34
        $this->middleware('add.lookup.tables:User', ['only' => ['create', 'edit']]);
35
        $this->middleware('add.lookup.tables:PageCategory', ['only' => ['create', 'edit']]);
36
        $this->middleware('add.published.at', ['only' => ['store', 'update']]);
37
38
        /**
39
         * Other tables
40
         */
41
        $this->middleware('media.image.select:Image', ['only' => ['create', 'edit']]);
42
    }
43
44
    /**
45
     * Validation rules
46
     * 
47
     * @var array
48
     */
49
    protected $arValidationArray = [
50
        'name' => 'required|max:255|unique:page,name',
51
        'meta_title' => 'max:255',
52
        'meta_description' => 'max:255',
53
        'meta_keywords' => 'max:255',
54
        'text' => 'max:1000000',
55
        'url' => 'required|max:255|unique:page,url',
56
        'author_name' => 'max:255',
57
        'published_at' => 'date',
58
    ];
59
60
    /**
61
     * Associate relationships to other table
62
     * 
63
     * @param object $object
64
     * @param Request $request
65
     */
66
    public function associateRelationships($object, Request $request) {
67
68
        /**
69
         * Validate user ID, if failed set to actual authorized user
70
         */
71
        $validator = Validator::make($request->all(), [
72
                    'user_id' => 'required|integer|min:1|exists:users,id',
73
        ]);
74
        
75
        /**
76
         * Validator fails - add actual authorized user
77
         */
78
        if ($validator->fails()) {
79
80
            $object->users()->associate(Auth::user()->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable 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...
81
            
82
        } 
83
        
84
        /**
85
         * Validator OK - save it
86
         */
87
        else {
88
89
            $object->users()->associate($request->input('user_id'));
90
        }
91
92
        /**
93
         * Validate image ID, if failed set to default
94
         */
95
        $validator = Validator::make($request->all(), [
96
                    'image_id' => 'integer|min:1|exists:image,id',
97
        ]);
98
99
        /**
100
         * Validator fails - nothing to do
101
         */
102
        if ($validator->fails()) {
103
104
           
105
        } 
106
        
107
        /**
108
         * Validator OK - save it
109
         */
110
        else {
111
112
            $object->images()->associate($request->input('image_id'));
113
        }
114
    }
115
    
116
    /**
117
     * Associate relationships to other table, where ID if object must be present
118
     * 
119
     * @param object $object
120
     * @param Request $request
121
     */
122
    public function associateRelationshipsWithID($object, Request $request) {
123
        
124
        /**
125
         * Validate page category ID, if failed set to default
126
         */
127
        if($request->has('pagecategory_id')){
128
            $validIDs = [];
129
            
130
            /**
131
             * For each category
132
             */
133
            foreach ($request->input('pagecategory_id') as $pagecategory_id) {
134
135
                $arrayForValidator = ['pagecategory_id' =>  $pagecategory_id];
136
137
                $validator = Validator::make($arrayForValidator, [
138
                            'pagecategory_id' => 'required|integer|min:1|exists:pagecategory,id',
139
                ]);
140
141
                /**
142
                 * Validator fails - nothing to do
143
                 */
144
                if ($validator->fails()) {
145
146
                } 
147
                
148
                /**
149
                 * Validator OK - save it
150
                 */
151
                else {
152
153
                    $validIDs[] = $pagecategory_id;
154
                }
155
            }
156
            
157
            /**
158
             * Sync to pivot
159
             */
160
            $object->pagecategories()->sync($validIDs);
161
        }
162
    }
163
}
164