• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-helpers / 12706396907

10 Jan 2025 09:01AM UTC coverage: 79.092% (+0.7%) from 78.385%
12706396907

Pull #170

github

web-flow
Merge 4b2fcaeed into 45d228ba8
Pull Request #170: #145: Fix bug with empty arguments

25 of 25 new or added lines in 1 file covered. (100.0%)

3 existing lines in 1 file now uncovered.

1063 of 1344 relevant lines covered (79.09%)

12.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.0
/src/HelpersServiceProvider.php
1
<?php
2

3
namespace RonasIT\Support;
4

5
use Illuminate\Routing\Route;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Facades\ParallelTesting;
11
use Illuminate\Support\Facades\Route as RouteFacade;
12
use Illuminate\Support\Facades\Validator;
13
use Illuminate\Support\ServiceProvider;
14
use Illuminate\Testing\Concerns\TestDatabases;
15
use Maatwebsite\Excel\ExcelServiceProvider;
16
use RonasIT\Support\Contracts\VersionEnumContract as Version;
17
use RonasIT\Support\Exceptions\InvalidValidationRuleUsageException;
18
use RonasIT\Support\Http\Middleware\SecurityMiddleware;
19

20
class HelpersServiceProvider extends ServiceProvider
21
{
22
    use TestDatabases;
23

24
    public function boot(): void
25
    {
26
        $router = $this->app['router'];
261✔
27

28
        $router->prependMiddlewareToGroup('web', SecurityMiddleware::class);
261✔
29
        $router->prependMiddlewareToGroup('api', SecurityMiddleware::class);
261✔
30

31
        $this->extendValidator();
261✔
32

33
        app(ExcelServiceProvider::class, ['app' => app()])->boot();
261✔
34

35
        $this->extendRouter();
261✔
36

37
        if ($this->app->runningUnitTests()) {
261✔
38
            $this->whenNotUsingInMemoryDatabase(function ($database) {
261✔
UNCOV
39
                [$testDatabase, $created] = $this->ensureTestDatabaseExists($database);
×
40

UNCOV
41
                $this->switchToDatabase($testDatabase);
×
42

43
                if ($created) {
×
UNCOV
44
                    ParallelTesting::callSetUpTestDatabaseCallbacks($testDatabase);
×
45
                }
46
            });
261✔
47
        }
48
    }
49

50
    public function register(): void
51
    {
52
        app(ExcelServiceProvider::class, ['app' => app()])->register();
261✔
53
    }
54

55
    protected function extendValidator(): void
56
    {
57
        Validator::extend('unique_except_of_authorized_user', function ($attribute, $value, $parameters = []) {
261✔
58
            $table = Arr::get($parameters, 0, 'users');
4✔
59
            $keyField = Arr::get($parameters, 1, 'id');
4✔
60
            $userId = Auth::id();
4✔
61

62
            $result = DB::table($table)
4✔
63
                ->where($keyField, '<>', $userId)
4✔
64
                ->whereIn($attribute, Arr::flatten((array) $value))
4✔
65
                ->exists();
4✔
66

67
            return !$result;
4✔
68
        });
261✔
69

70
        Validator::extend('list_exists', function ($attribute, $value, $parameters, $validator) {
261✔
71
            if (count($parameters) < 1) {
6✔
72
                throw new InvalidValidationRuleUsageException("list_exists: At least 1 parameter must be added when checking the {$attribute} field in the request.");
1✔
73
            }
74

75
            $hasFieldNameParam = !empty(Arr::get($parameters, 2));
5✔
76

77
            if (is_multidimensional($value) && !$hasFieldNameParam) {
5✔
78
                throw new InvalidValidationRuleUsageException("list_exists: The third parameter should be filled when checking the {$attribute} field if we are using a collection in request.");
1✔
79
            }
80

81
            if ($hasFieldNameParam) {
4✔
82
                $value = Arr::pluck($value, Arr::get($parameters, 2));
1✔
83
            }
84

85
            $value = array_unique($value);
4✔
86

87
            $table = Arr::get($parameters, 0);
4✔
88
            $keyField = Arr::get($parameters, 1, 'id');
4✔
89

90
            $existingValueCount = DB::table($table)
4✔
91
                ->whereIn($keyField, $value)
4✔
92
                ->distinct()
4✔
93
                ->count($keyField);
4✔
94

95
            $result = $existingValueCount === count($value);
4✔
96

97
            if (!$result) {
4✔
98
                $validator->errors()->add($attribute, "Some of the passed {$attribute} are not exists.");
1✔
99
            }
100

101
            return $result;
4✔
102
        });
261✔
103
    }
104

105
    protected function extendRouter(): void
106
    {
107
        /**
108
         * Specify that the route version must be in the range of given values inclusive.
109
         *
110
         * @param Version|null $start
111
         * @param Version|null $end
112
         * @param string|null $param (default is 'version')
113
         * @param Route|null $instance
114
         * @return Router|Route
115
         */
116
        $versionRange = function (
261✔
117
            ?Version $start,
261✔
118
            ?Version $end,
261✔
119
            ?string $param,
261✔
120
            Route $instance = null
261✔
121
        ) {
261✔
122
            if (!$param) {
38✔
123
                $param = 'version';
38✔
124
            }
125

126
            $versionEnum = app(Version::class);
38✔
127
            $disabledVersions = config('app.disabled_api_versions') ?: [];
38✔
128

129
            $versions = array_diff($versionEnum::values(), $disabledVersions);
38✔
130

131
            $versions = array_filter($versions, function ($version) use ($start, $end) {
38✔
132
                $result = true;
38✔
133

134
                if (!empty($start)) {
38✔
135
                    $result &= version_compare($version, $start->value, '>=');
38✔
136
                }
137

138
                if (!empty($end)) {
38✔
139
                    $result &= version_compare($version, $end->value, '<=');
38✔
140
                }
141

142
                return $result;
38✔
143
            });
38✔
144

145
            return (!empty($instance))
38✔
146
                ? $instance->whereIn($param, $versions)
38✔
147
                : RouteFacade::whereIn($param, $versions);
38✔
148
        };
261✔
149

150
        Route::macro(
261✔
151
            'versionRange',
261✔
152
            fn (Version $from, Version $to, $param = null) => $versionRange($from, $to, $param, $this)
261✔
153
        );
261✔
154
        Route::macro('versionFrom', fn (Version $from, $param = null) => $versionRange($from, null, $param, $this));
261✔
155
        Route::macro('versionTo', fn (Version $to, $param = null) => $versionRange(null, $to, $param, $this));
261✔
156

157
        RouteFacade::macro(
261✔
158
            'versionRange',
261✔
159
            fn (Version $from, Version $to, string $param = null) => $versionRange($from, $to, $param)
261✔
160
        );
261✔
161
        RouteFacade::macro('versionFrom', fn (Version $from, $param = null) => $versionRange($from, null, $param));
261✔
162
        RouteFacade::macro('versionTo', fn (Version $to, $param = null) => $versionRange(null, $to, $param));
261✔
163
        RouteFacade::macro('version', fn (Version $version) => RouteFacade::prefix('v' . $version->value));
261✔
164
    }
165
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc