1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Faithgen\Testimonies\Http\Requests; |
4
|
|
|
|
5
|
|
|
use Faithgen\Testimonies\Services\TestimoniesService; |
6
|
|
|
use Faithgen\Testimonies\Testimonies; |
7
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
8
|
|
|
|
9
|
|
|
class AddImagesRequest extends FormRequest |
10
|
|
|
{ |
11
|
|
|
private TestimoniesService $testimoniesService; |
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Determine if the user is authorized to make this request. |
15
|
|
|
* |
16
|
|
|
* @param \Faithgen\Testimonies\Services\TestimoniesService $testimoniesService |
17
|
|
|
* |
18
|
|
|
* @return bool |
19
|
|
|
*/ |
20
|
|
|
public function authorize(TestimoniesService $testimoniesService) |
21
|
|
|
{ |
22
|
|
|
$this->testimoniesService = $testimoniesService; |
23
|
|
|
|
24
|
|
|
return $this->testimoniesService->getTestimony() |
25
|
|
|
&& $this->user()->can('update', $this->testimoniesService->getTestimony()); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the validation rules that apply to the request. |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
|
|
public function rules() |
34
|
|
|
{ |
35
|
|
|
return [ |
36
|
|
|
'images' => 'required|array|max:'.$this->getRemainingImages(), |
37
|
|
|
'images.*' => 'base64image', |
38
|
|
|
]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Calculates the number of images the user should upload so that they do not |
43
|
|
|
* exceed the limit set for their ministries. |
44
|
|
|
* |
45
|
|
|
* @return int number of images |
46
|
|
|
*/ |
47
|
|
|
private function getRemainingImages(): int |
48
|
|
|
{ |
49
|
|
|
$currentImagesCount = $this->testimoniesService->getTestimony()->images()->count(); |
50
|
|
|
$subscriptionLevel = auth()->user()->account->level; |
51
|
|
|
if ($subscriptionLevel === 'Free') { |
52
|
|
|
abort(403, 'You are not allowed to add images on this testimony'); |
53
|
|
|
} |
54
|
|
|
if ($subscriptionLevel === 'Premium') { |
55
|
|
|
return Testimonies::$premiumImageCount - $currentImagesCount; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return Testimonies::$premiumPlusImageCount - $currentImagesCount; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Converts image string array to usable string in the validation. |
63
|
|
|
* |
64
|
|
|
* @return void |
65
|
|
|
*/ |
66
|
|
|
public function prepareForValidation() |
67
|
|
|
{ |
68
|
|
|
if (is_string($this->images)) { |
69
|
|
|
$this->merge([ |
70
|
|
|
'images' => json_decode($this->images, true), |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|