Passed
Pull Request — master (#405)
by
unknown
05:48
created

UserRepository   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 42
dl 0
loc 146
ccs 32
cts 48
cp 0.6667
rs 10
c 4
b 1
f 0
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A afterSave() 0 9 2
A sendWelcomeEmail() 0 11 4
A getCountForTrash() 0 3 1
A filter() 0 8 1
A prepareFieldsBeforeSave() 0 13 5
A getCountForDraft() 0 3 1
A __construct() 0 13 1
A getCountForPublished() 0 3 1
A afterUpdateBasic() 0 4 1
1
<?php
2
3
namespace A17\Twill\Repositories;
4
5
use App;
6
use A17\Twill\Models\User;
7
use A17\Twill\Repositories\Behaviors\HandleMedias;
8
use A17\Twill\Repositories\Behaviors\HandleOauth;
9
use Illuminate\Auth\Passwords\PasswordBrokerManager;
10
use Illuminate\Config\Repository as Config;
11
use Illuminate\Contracts\Auth\Factory as AuthFactory;
12
use Illuminate\Database\DatabaseManager as DB;
13
14
class UserRepository extends ModuleRepository
15
{
16
    use HandleMedias, HandleOauth;
0 ignored issues
show
introduced by
The trait A17\Twill\Repositories\Behaviors\HandleOauth requires some properties which are not provided by A17\Twill\Repositories\UserRepository: $name, $email, $id
Loading history...
introduced by
The trait A17\Twill\Repositories\Behaviors\HandleMedias requires some properties which are not provided by A17\Twill\Repositories\UserRepository: $medias, $crop_y, $pivot, $metadatas, $mediasParams, $crop_h, $ratio, $crop_x, $crop_w
Loading history...
17
18
    /**
19
     * @var Config
20
     */
21
    protected $config;
22
23
    /**
24
     * @var DB
25
     */
26
    protected $db;
27
28
    /**
29
     * @var PasswordBrokerManager
30
     */
31
    protected $passwordBrokerManager;
32
33
    /**
34
     * @var AuthFactory
35
     */
36
    protected $authFactory;
37
38
    /**
39
     * @param DB $db
40
     * @param Config $config
41
     * @param PasswordBrokerManager $passwordBrokerManager
42
     * @param AuthFactory $authFactory
43
     * @param User $model
44
     */
45 6
    public function __construct(
46
        DB $db,
47
        Config $config,
48
        PasswordBrokerManager $passwordBrokerManager,
49
        AuthFactory $authFactory,
50
        User $model
51
    ) {
52
53 6
        $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by
It seems like $model of type A17\Twill\Models\User is incompatible with the declared type A17\Twill\Models\Model of property $model.

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...
54 6
        $this->passwordBrokerManager = $passwordBrokerManager;
55 6
        $this->authFactory = $authFactory;
56 6
        $this->config = $config;
57 6
        $this->db = $db;
58 6
    }
59
60
    /**
61
     * @param \Illuminate\Database\Query\Builder $query
62
     * @param array $scopes
63
     * @return \Illuminate\Database\Query\Builder
64
     */
65 1
    public function filter($query, array $scopes = [])
66
    {
67
        $query->when(isset($scopes['role']), function ($query) use ($scopes) {
68
            $query->where('role', $scopes['role']);
69 1
        });
70 1
        $query->where('role', '<>', 'SUPERADMIN');
71 1
        $this->searchIn($query, $scopes, 'search', ['name', 'email', 'role']);
72 1
        return parent::filter($query, $scopes);
73
    }
74
75
    /**
76
     * @param \A17\Twill\Models\Model $user
77
     * @param array $fields
78
     */
79
    public function afterUpdateBasic($user, $fields)
80
    {
81
        $this->sendWelcomeEmail($user);
0 ignored issues
show
Bug introduced by
$user of type A17\Twill\Models\Model is incompatible with the type A17\Twill\Models\User expected by parameter $user of A17\Twill\Repositories\U...ory::sendWelcomeEmail(). ( Ignorable by Annotation )

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

81
        $this->sendWelcomeEmail(/** @scrutinizer ignore-type */ $user);
Loading history...
82
        parent::afterUpdateBasic($user, $fields);
83
    }
84
85
    /**
86
     * @return int
87
     */
88 1
    public function getCountForPublished()
89
    {
90 1
        return $this->model->where('role', '<>', 'SUPERADMIN')->published()->count();
91
    }
92
93
    /**
94
     * @return int
95
     */
96 1
    public function getCountForDraft()
97
    {
98 1
        return $this->model->where('role', '<>', 'SUPERADMIN')->draft()->count();
99
    }
100
101
    /**
102
     * @return int
103
     */
104 1
    public function getCountForTrash()
105
    {
106 1
        return $this->model->where('role', '<>', 'SUPERADMIN')->onlyTrashed()->count();
107
    }
108
109
    /**
110
     * @param \A17\Twill\Models\Model $user
111
     * @param array $fields
112
     * @return string[]
113
     */
114 4
    public function prepareFieldsBeforeSave($user, $fields)
115
    {
116 4
        $editor = $this->authFactory->guard('twill_users')->user();
117 4
        $with2faSettings = $this->config->get('twill.enabled.users-2fa', false) && $editor->id === $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?
Loading history...
118
119 4
        if ($with2faSettings
120 4
            && $user->google_2fa_enabled
0 ignored issues
show
Bug introduced by
The property google_2fa_enabled does not seem to exist on A17\Twill\Models\Model. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
121 4
            && !($fields['google_2fa_enabled'] ?? false)
122
        ) {
123
            $fields['google_2fa_secret'] = null;
124
        }
125
126 4
        return parent::prepareFieldsBeforeSave($user, $fields);
127
    }
128
129
    /**
130
     * @param \A17\Twill\Models\Model|User $user
131
     * @param array $fields
132
     * @return void
133
     */
134 4
    public function afterSave($user, $fields)
135
    {
136 4
        $this->sendWelcomeEmail($user);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type A17\Twill\Models\Model; however, parameter $user of A17\Twill\Repositories\U...ory::sendWelcomeEmail() does only seem to accept A17\Twill\Models\User, maybe add an additional type check? ( Ignorable by Annotation )

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

136
        $this->sendWelcomeEmail(/** @scrutinizer ignore-type */ $user);
Loading history...
137 4
        $language = $fields['language'];
138
        if ($language !== App::getLocale()) {
139
            $user->language = $language;
0 ignored issues
show
Bug introduced by
The property language does not seem to exist on A17\Twill\Models\Model. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
Bug Best Practice introduced by
The property language does not exist on A17\Twill\Models\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
140
            $user->save();
141
        }
142
        parent::afterSave($user, $fields);
0 ignored issues
show
Bug introduced by
It seems like $user can also be of type A17\Twill\Models\User; however, parameter $object of A17\Twill\Repositories\M...Repository::afterSave() does only seem to accept A17\Twill\Models\Model, maybe add an additional type check? ( Ignorable by Annotation )

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

142
        parent::afterSave(/** @scrutinizer ignore-type */ $user, $fields);
Loading history...
143
    }
144
145
    /**
146
     * @param User $user
147
     * @return void
148
     */
149 4
    private function sendWelcomeEmail($user)
150
    {
151 4
        if (empty($user->password)
0 ignored issues
show
Bug Best Practice introduced by
The property password does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
152 4
            && $user->published
0 ignored issues
show
Bug Best Practice introduced by
The property published does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
153
            && !$this->db
154
            ->table($this->config->get('twill.password_resets_table', 'twill_password_resets'))
155
            ->where('email', $user->email)
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist on A17\Twill\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
156 4
            ->exists()
157
        ) {
158
            $user->sendWelcomeNotification(
159
                $this->passwordBrokerManager->broker('twill_users')->getRepository()->create($user)
160
            );
161
        }
162 4
    }
163
164
}
165