Passed
Push — master ( 02a4a5...734a7e )
by Mihail
05:36
created

Profile   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 196
Duplicated Lines 22.45 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 9
Bugs 3 Features 0
Metric Value
c 9
b 3
f 0
dl 44
loc 196
rs 9.1666
wmc 25
lcom 1
cbo 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A actionDelete() 0 4 1
C actionUpdate() 0 32 8
A actionFieldlist() 0 8 1
B actionIndex() 26 26 1
B actionFieldupdate() 0 23 5
B actionFielddelete() 0 24 6
A actionSettings() 18 18 3

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
namespace Apps\Controller\Admin;
4
5
use Apps\ActiveRecord\ProfileField;
6
use Apps\Model\Admin\Profile\FormFieldUpdate;
7
use Apps\Model\Admin\Profile\FormSettings;
8
use Apps\Model\Front\Profile\FormSettings as FrontFormSettings;
9
use Extend\Core\Arch\AdminController;
10
use Apps\ActiveRecord\Profile as ProfileRecords;
11
use Ffcms\Core\App;
12
use Ffcms\Core\Exception\ForbiddenException;
13
use Ffcms\Core\Exception\NotFoundException;
14
use Ffcms\Core\Helper\HTML\SimplePagination;
15
use Ffcms\Core\Helper\Type\Obj;
16
17
class Profile extends AdminController
18
{
19
    const VERSION = 0.1;
20
    const ITEM_PER_PAGE = 10;
21
22
    public $type = 'app';
23
24
    /**
25
     * List all profiles in website with pagination
26
     * @return string
27
     * @throws \Ffcms\Core\Exception\NativeException
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     */
30 View Code Duplication
    public function actionIndex()
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...
31
    {
32
        // init Active Record
33
        $query = new ProfileRecords();
34
35
        // set current page and offset
36
        $page = (int)$this->request->query->get('page');
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Apps\Controller\Admin\Profile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
37
        $offset = $page * self::ITEM_PER_PAGE;
38
39
        // build pagination
40
        $pagination = new SimplePagination([
41
            'url' => ['profile/index'],
42
            'page' => $page,
43
            'step' => self::ITEM_PER_PAGE,
44
            'total' => $query->count()
45
        ]);
46
47
        // build listing objects
48
        $records = $query->orderBy('id', 'desc')->skip($offset)->take(self::ITEM_PER_PAGE)->get();
49
50
        // display viewer
51
        return $this->view->render('index', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Profile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
52
            'records' => $records,
53
            'pagination' => $pagination
54
        ]);
55
    }
56
57
    /**
58
     * Redirect to user controller
59
     * @param $id
60
     */
61
    public function actionDelete($id)
62
    {
63
        $this->response->redirect('user/delete/' . $id);
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

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...
64
    }
65
66
    /**
67
     * Profile edit action
68
     * @param int $id
69
     * @return string
70
     * @throws \Ffcms\Core\Exception\SyntaxException
71
     * @throws \Ffcms\Core\Exception\NativeException
72
     * @throws NotFoundException
73
     */
74
    public function actionUpdate($id)
75
    {
76
        if (!Obj::isLikeInt($id) || $id < 1) {
77
            throw new NotFoundException();
78
        }
79
80
        // get user profile via id
81
        $profile = ProfileRecords::find($id);
82
        if (false === $profile || null === $profile) {
83
            throw new NotFoundException();
84
        }
85
86
        // check if user id for this profile_id is exist
87
        if (!App::$User->isExist($profile->user_id)) {
88
            throw new NotFoundException();
89
        }
90
91
        // get user object from profile
92
        $user = $profile->User();
93
        $model = new FrontFormSettings($user);
94
95
        if ($model->send() && $model->validate()) {
96
            $model->save();
97
            App::$Session->getFlashBag()->add('success', __('Profile is updated'));
98
        }
99
100
        return $this->view->render('update', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Profile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
101
            'model' => $model->filter(),
102
            'user' => $user,
103
            'profile' => $profile
104
        ]);
105
    }
106
107
    /**
108
     * List additional fields
109
     * @return string
110
     * @throws \Ffcms\Core\Exception\NativeException
111
     * @throws \Ffcms\Core\Exception\SyntaxException
112
     */
113
    public function actionFieldlist()
114
    {
115
        $records = ProfileField::all();
116
117
        return $this->view->render('field_list', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Profile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
118
            'records' => $records
119
        ]);
120
    }
121
122
    /**
123
     * Add and edit additional fields data
124
     * @param int $id
125
     * @return string
126
     * @throws \Ffcms\Core\Exception\SyntaxException
127
     * @throws \Ffcms\Core\Exception\NativeException
128
     */
129
    public function actionFieldupdate($id)
130
    {
131
        // get current record or new and init form DI
132
        $record = ProfileField::findOrNew($id);
133
        $model = new FormFieldUpdate($record);
134
135
        $isNew = false;
136
        if ($record->id === null) {
137
            $isNew = true;
138
        }
139
        // check if form is submited
140
        if ($model->send() && $model->validate()) {
141
            $model->save();
142
            if (true === $isNew) {
143
                $this->response->redirect('profile/fieldlist');
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

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...
144
            }
145
            App::$Session->getFlashBag()->add('success', __('Profile field was successful updated'));
146
        }
147
148
        return $this->view->render('field_update', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Profile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
149
            'model' => $model->filter()
150
        ]);
151
    }
152
153
    /**
154
     * Delete custom field action
155
     * @param int $id
156
     * @return string
157
     * @throws \Ffcms\Core\Exception\SyntaxException
158
     * @throws \Ffcms\Core\Exception\NativeException
159
     * @throws ForbiddenException
160
     */
161
    public function actionFielddelete($id)
162
    {
163
        if (!Obj::isLikeInt($id) || $id < 1) {
164
            throw new ForbiddenException();
165
        }
166
167
        // check if record with $id is exist
168
        $record = ProfileField::find($id);
169
        if ($record === null || $record === false) {
170
            throw new ForbiddenException();
171
        }
172
173
        $model = new FormFieldUpdate($record);
0 ignored issues
show
Compatibility introduced by
$record of type object<Ffcms\Core\Arch\ActiveModel> is not a sub-type of object<Apps\ActiveRecord\ProfileField>. It seems like you assume a child class of the class Ffcms\Core\Arch\ActiveModel to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
174
175
        // if delete is submited - lets remove this record
176
        if ($model->send()) {
177
            $model->delete();
178
            $this->response->redirect('profile/fieldlist');
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

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...
179
        }
180
181
        return $this->view->render('field_delete', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Profile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
182
            'model' => $model->filter()
183
        ]);
184
    }
185
186
    /**
187
     * Show profiles settings
188
     * @return string
189
     * @throws \Ffcms\Core\Exception\NativeException
190
     * @throws \Ffcms\Core\Exception\SyntaxException
191
     */
192 View Code Duplication
    public function actionSettings()
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...
193
    {
194
        $model = new FormSettings($this->getConfigs());
195
196
        if ($model->send()) {
197
            if ($model->validate()) {
198
                $this->setConfigs($model->getAllProperties());
199
                App::$Session->getFlashBag()->add('success', __('Settings is successful updated'));
200
                $this->response->redirect('profile/index');
0 ignored issues
show
Bug introduced by
The method redirect cannot be called on $this->response (of type string).

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...
201
            } else {
202
                App::$Session->getFlashBag()->add('error', __('Form validation is failed'));
203
            }
204
        }
205
206
        return $this->view->render('settings', [
0 ignored issues
show
Documentation introduced by
The property view does not exist on object<Apps\Controller\Admin\Profile>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
207
            'model' => $model->filter()
208
        ]);
209
    }
210
211
212
}