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

RonasIT / laravel-entity-generator / 12026977149

26 Nov 2024 08:49AM UTC coverage: 39.655% (+0.1%) from 39.537%
12026977149

Pull #108

github

web-flow
Merge ddd257d48 into 8497bb4ef
Pull Request #108: feat: skip generation for invalid stubs

33 of 75 new or added lines in 15 files covered. (44.0%)

23 existing lines in 2 files now uncovered.

391 of 986 relevant lines covered (39.66%)

1.31 hits per line

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

0.0
/src/Generators/RequestsGenerator.php
1
<?php
2

3
namespace RonasIT\Support\Generators;
4

5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use RonasIT\Support\Events\SuccessCreateMessage;
8

9
class RequestsGenerator extends EntityGenerator
10
{
11
    const SEARCH_METHOD = 'Search';
12
    const UPDATE_METHOD = 'Update';
13
    const CREATE_METHOD = 'Create';
14
    const DELETE_METHOD = 'Delete';
15
    const GET_METHOD = 'Get';
16

17
    public function setRelations($relations)
18
    {
19
        parent::setRelations($relations);
×
20

21
        $this->relations['belongsTo'] = array_map(function ($field) {
×
22
            return Str::snake($field) . '_id';
×
23
        }, $this->relations['belongsTo']);
×
24

25
        return $this;
×
26
    }
27

28
    public function generate(): void
29
    {
NEW
30
        if (!$this->checkStubExists('request')) {
×
NEW
31
            return;
×
32
        }
33

34
        if (in_array('R', $this->crudOptions)) {
×
35
            $this->createRequest(
×
36
                self::GET_METHOD,
×
37
                true,
×
38
                $this->getGetValidationParameters()
×
39
            );
×
40
            $this->createRequest(
×
41
                self::SEARCH_METHOD,
×
42
                false,
×
43
                $this->getSearchValidationParameters()
×
44
            );
×
45
        }
46

47
        if (in_array('D', $this->crudOptions)) {
×
48
            $this->createRequest(self::DELETE_METHOD);
×
49
        }
50

51
        if (in_array('C', $this->crudOptions)) {
×
52
            $this->createRequest(
×
53
                self::CREATE_METHOD,
×
54
                false,
×
55
                $this->getCreateValidationParameters()
×
56
            );
×
57
        }
58

59
        if (in_array('U', $this->crudOptions)) {
×
60
            $this->createRequest(
×
61
                self::UPDATE_METHOD,
×
62
                true,
×
63
                $this->getUpdateValidationParameters()
×
64
            );
×
65
        }
66
    }
67

68
    protected function createRequest($method, $needToValidate = true, $parameters = []): void
69
    {
70
        $requestsFolder = $this->getPluralName($this->model);
×
71
        $modelName = $this->getEntityName($method);
×
72

73
        $content = $this->getStub('request', [
×
74
            'method' => $method,
×
75
            'entity' => $modelName,
×
76
            'parameters' => $parameters,
×
77
            'needToValidate' => $needToValidate,
×
78
            'requestsFolder' => $requestsFolder,
×
79
            'namespace' => $this->getOrCreateNamespace('requests'),
×
80
            'servicesNamespace' => $this->getOrCreateNamespace('services')
×
81
        ]);
×
82

83
        $this->saveClass('requests', "{$method}{$modelName}Request",
×
84
            $content, $requestsFolder
×
85
        );
×
86

87
        event(new SuccessCreateMessage("Created a new Request: {$method}{$modelName}Request"));
×
88
    }
89

90
    protected function getGetValidationParameters(): array
91
    {
92
        $parameters['array'] = ['with'];
×
93

94
        $parameters['string-required'] = ['with.*'];
×
95

96
        return $this->getValidationParameters($parameters, true);
×
97
    }
98

99
    protected function getCreateValidationParameters(): array
100
    {
101
        $parameters = Arr::except($this->fields, 'boolean-required');
×
102

103
        if (!empty($this->fields['boolean-required'])) {
×
104
            $parameters['boolean-present'] = $this->fields['boolean-required'];
×
105
        }
106

107
        return $this->getValidationParameters($parameters, true);
×
108
    }
109

110
    protected function getUpdateValidationParameters(): array
111
    {
112
        $parameters = Arr::except($this->fields, 'boolean-required');
×
113

114
        if (!empty($this->fields['boolean-required'])) {
×
115
            $parameters['boolean'] = array_merge($parameters['boolean'], $this->fields['boolean-required']);
×
116
        }
117

118
        return $this->getValidationParameters($parameters, false);
×
119
    }
120

121
    protected function getSearchValidationParameters(): array
122
    {
123
        $parameters = Arr::except($this->fields, [
×
124
            'timestamp', 'timestamp-required', 'string-required', 'integer-required', 'boolean-required'
×
125
        ]);
×
126

127
        $parameters['boolean'] = array_merge($this->fields['boolean-required'], [
×
UNCOV
128
            'desc',
×
129
            'all',
×
130
        ]);
×
131

UNCOV
132
        $parameters['integer'] = array_merge($this->fields['integer'], [
×
133
            'page',
×
UNCOV
134
            'per_page',
×
135
        ]);
×
136

137
        $parameters['array'] = ['with'];
×
138

139
        $parameters['string'] = ['order_by'];
×
140

141
        $parameters['string-nullable'] = ['query'];
×
142

UNCOV
143
        $parameters['string-required'] = ['with.*'];
×
144

UNCOV
145
        return $this->getValidationParameters($parameters, false);
×
146
    }
147

148
    public function getValidationParameters($parameters, $requiredAvailable): array
149
    {
150
        $result = [];
×
151

152
        foreach ($parameters as $type => $parameterNames) {
×
UNCOV
153
            $isRequired = Str::contains($type, 'required');
×
154
            $isNullable = Str::contains($type, 'nullable');
×
155
            $isPresent = Str::contains($type, 'present');
×
UNCOV
156
            $type = head(explode('-', $type));
×
157

UNCOV
158
            foreach ($parameterNames as $name) {
×
UNCOV
159
                $required = $isRequired && $requiredAvailable;
×
160

161
                $result[] = $this->getRules($name, $type, $required, $isNullable, $isPresent);
×
162
            }
163
        }
164

UNCOV
165
        return $result;
×
166
    }
167

168
    protected function getRules($name, $type, $required, $nullable, $present): array
169
    {
170
        $replaces = [
×
UNCOV
171
            'timestamp' => 'date',
×
172
            'float' => 'numeric',
×
173
            'json' => 'array'
×
174
        ];
×
175

176
        $rules = [
×
177
            Arr::get($replaces, $type, $type)
×
UNCOV
178
        ];
×
179

UNCOV
180
        if (in_array($name, $this->relations['belongsTo'])) {
×
181
            $tableName = str_replace('_id', '', $name);
×
182

UNCOV
183
            $rules[] = "exists:{$this->getTableName($tableName)},id";
×
184

185
            $required = true;
×
186
        }
187

188
        if ($required) {
×
189
            $rules[] = 'required';
×
190
        }
191

192
        if ($nullable) {
×
193
            $rules[] = 'nullable';
×
194
        }
195

196
        if ($present) {
×
197
            $rules[] = 'present';
×
198
        }
199

UNCOV
200
        return [
×
UNCOV
201
            'name' => $name,
×
UNCOV
202
            'rules' => $rules
×
UNCOV
203
        ];
×
204
    }
205

206
    private function getEntityName($method): string
207
    {
208
        if ($method === self::SEARCH_METHOD) {
×
UNCOV
209
            return Str::plural($this->model);
×
210
        }
211

UNCOV
212
        return $this->model;
×
213
    }
214
}
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