ScriptService::updateConfigProfile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Modules\Script\Services;
4
5
use Modules\Script\Dtos\CreateScriptDto;
6
use Modules\Script\Dtos\GrantUserExclusivityDto;
7
use Modules\Script\Dtos\UpdateUserExclusivityDto;
8
use Modules\Script\Entities\Script;
9
use Modules\Script\Entities\ScriptConfigProfile;
10
use Modules\Script\Entities\ScriptExclusivity;
11
use Modules\Script\Entities\ScriptRelease;
12
use Modules\Script\Entities\ScriptReview;
13
use Modules\Script\Entities\ScriptReviewReply;
14
use Modules\Script\Events\ScriptWasCreatedEvent;
15
use Modules\Script\Events\ScriptWasUpdatedEvent;
16
use Modules\Script\Events\ScriptWasDeletedEvent;
17
use Modules\Script\Contracts\ScriptServiceContract;
18
use Illuminate\Database\Eloquent\Collection;
19
use Modules\Script\Exceptions\ScriptAlreadyReviewedException;
20
use Modules\Script\Exceptions\ScriptReviewReplyAlreadyExistsException;
21
use Modules\Script\Exceptions\UserAlreadyHasExclusivityException;
22
use Modules\Script\Exceptions\UserDoesNotHaveExclusivityException;
23
use Modules\Script\Guards\ScriptAlreadyReviewedGuard;
24
use Modules\Script\Support\Version;
25
26
class ScriptService implements ScriptServiceContract
27
{
28
    /**
29
     * @param $id
30
     * @return Script|null
31
     */
32
    public function resolve($id): ?Script
33
    {
34
        if ($id instanceof Script) {
35
            return $id;
36
        }
37
38
        return Script::find($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Modules\Script\Entities\Script::find($id) could return the type Illuminate\Database\Eloq...go\Abstracts\MongoModel which includes types incompatible with the type-hinted return Modules\Script\Entities\Script|null. Consider adding an additional type-check to rule them out.
Loading history...
39
    }
40
41
    /**
42
     * @param $userId
43
     * @return Script[]
44
     */
45
    public function getByAuthorId($userId): Collection
46
    {
47
        return Script::where(Script::AUTHOR_ID, $userId)->get();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Modules\Script\En...HOR_ID, $userId)->get() returns the type Illuminate\Database\Eloquent\Collection which is incompatible with the documented return type Modules\Script\Entities\Script[].
Loading history...
48
    }
49
50
    /**
51
     * @param $id
52
     * @return Script|null
53
     */
54
    public function update($id, array $data): Script
55
    {
56
        $script = $this->resolve($id);
57
        $script->update($data);
58
        event(new ScriptWasUpdatedEvent($script));
59
        return $script;
60
    }
61
62
    /**
63
     * @param $id
64
     * @return Script
65
     */
66
    public function create(CreateScriptDto $data): Script
67
    {
68
        $data->user_id = get_authenticated_user_id();
69
        $script = Script::create($data->toArray());
70
        event(new ScriptWasCreatedEvent($script));
71
        return $script;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $script returns the type Illuminate\Database\Eloq...go\Abstracts\MongoModel which includes types incompatible with the type-hinted return Modules\Script\Entities\Script.
Loading history...
72
    }
73
74
    /**
75
     * @param $id
76
     * @return bool
77
     */
78
    public function delete($id): bool
79
    {
80
        $script = $this->resolve($id);
81
        $deleted = $script->delete();
82
        if ($deleted)
83
            event(new ScriptWasDeletedEvent($script));
84
        return $deleted;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $deleted could return the type null which is incompatible with the type-hinted return boolean. Consider adding an additional type-check to rule them out.
Loading history...
85
    }
86
87
    public function releaseVersion($id, array $data): Script
88
    {
89
        $script = $this->resolve($id);
90
        $releases = $script->releases()->get();
91
        if ($releases->isEmpty())
92
            $lastVersion = "0.0.0";
93
        else
94
            $lastVersion = $releases->last()->version;
95
96
        $version = new Version($lastVersion);
97
98
        switch ($data['type']) {
99
            case 'MAJOR':
100
                $version->incrementMajor();
101
                break;
102
            case 'MINOR':
103
                $version->incrementMinor();
104
                break;
105
            case 'PATCH':
106
                $version->incrementPatch();
107
                break;
108
            default:
109
                throw new \Exception("Invalid release type specified");
110
        }
111
112
        //TODO ADD CONFIG TEMPLATE
113
        $script->releases()->create([
114
            ScriptRelease::VERSION => $version->__toString(),
115
            ScriptRelease::TYPE => $data['type'],
116
            ScriptRelease::CHANGELOG => $data['changelog']
117
        ]);
118
119
        return $script;
120
    }
121
122
    public function publishReview($id, array $data): ScriptReview
123
    {
124
        $script = $this->resolve($id);
125
126
        guard(new ScriptAlreadyReviewedGuard($script));
127
128
        //TODO CHECK IF THE USER IS ACTUALLY SUBSCRIBED TO THE SCRIPT
129
        //TODO PREVENT THE AUTHOR FROM REVIEWING HIS OWN SCRIPT
130
131
        $review = $script->reviews()->create([
132
            ScriptReview::REVIEWER_ID => get_authenticated_user_id(),
133
            ScriptReview::VERSION => $script->releases->last()->version,
0 ignored issues
show
Bug introduced by
The property releases does not seem to exist on Modules\Script\Entities\Script. 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...
134
            ScriptReview::MESSAGE => $data['message']
135
        ]);
136
137
        return $review;
138
    }
139
140
    public function publishReviewReply($id, array $data): ScriptReviewReply
141
    {
142
        $script = $this->resolve($id);
143
144
        $review = $script->reviews()->withTrashed()->find($data['review_id']);
145
146
        if (!isset($review)) {
147
            throw new ScriptAlreadyReviewedException();
148
        }
149
150
        if ($review->reply !== null) {
151
            throw new ScriptReviewReplyAlreadyExistsException();
152
        }
153
154
        $reply = $review->reply()->create([
155
            ScriptReviewReply::REPLIER_ID => get_authenticated_user_id(),
156
            ScriptReviewReply::MESSAGE => $data['message']
157
        ]);
158
159
        return $reply;
160
    }
161
162
    public function grantUserExclusivity($id, GrantUserExclusivityDto $data): ScriptExclusivity
163
    {
164
        $script = $this->resolve($id);
165
        if ($script->exclusivity()->where(ScriptExclusivity::USER_ID, $data->user_id)->first() !== null) {
166
            throw new UserAlreadyHasExclusivityException();
167
        }
168
169
        return $script->exclusivity()->create($data->toArray());
170
    }
171
172
    public function removeUserExclusivity($id, $userId): bool
173
    {
174
        $script = $this->resolve($id);
175
176
        if (($exclusivity = $script->exclusivity()->where(ScriptExclusivity::USER_ID, $userId)->first()) === null) {
177
            throw new UserDoesNotHaveExclusivityException();
178
        }
179
180
        return $exclusivity->delete();
181
    }
182
183
    public function updateUserExclusivity($id, UpdateUserExclusivityDto $data): ScriptExclusivity
184
    {
185
        $script = $this->resolve($id);
186
187
        if (($exclusivity = $script->exclusivity()->where(ScriptExclusivity::USER_ID, $data->user_id ?? null)->first()) === null) {
188
            throw new UserDoesNotHaveExclusivityException();
189
        }
190
191
        $exclusivity->update($data->toArray());
192
193
        return $exclusivity;
194
    }
195
196
    public function subscribe($id, array $data): ScriptExclusivity
197
    {
198
        // TODO: Implement subscribe() method.
199
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Modules\Script\Entities\ScriptExclusivity. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
200
201
    public function unsubscribe($id, array $data): ScriptExclusivity
202
    {
203
        // TODO: Implement unsubscribe() method.
204
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Modules\Script\Entities\ScriptExclusivity. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
205
206
    public function createConfigProfile($id, array $data): ScriptConfigProfile
207
    {
208
        // TODO: Implement createConfigProfile() method.
209
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Modules\Script\Entities\ScriptConfigProfile. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
210
211
    public function updateConfigProfile($id, array $data): ScriptConfigProfile
212
    {
213
        // TODO: Implement updateConfigProfile() method.
214
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Modules\Script\Entities\ScriptConfigProfile. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
215
216
    public function deleteConfigProfile($id): bool
217
    {
218
        // TODO: Implement deleteConfigProfile() method.
219
    }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
220
221
222
}
223