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

RonasIT / laravel-entity-generator / 12291261101

12 Dec 2024 06:45AM UTC coverage: 48.366% (-0.06%) from 48.422%
12291261101

Pull #124

github

web-flow
Merge 61782be76 into 06a0bd176
Pull Request #124: #97: Use model test states in tests generator

444 of 918 relevant lines covered (48.37%)

1.65 hits per line

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

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

3
namespace RonasIT\Support\Generators;
4

5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Str;
7
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
8
use RonasIT\Support\Exceptions\ClassNotExistsException;
9
use RonasIT\Support\Events\SuccessCreateMessage;
10

11
class ModelGenerator extends EntityGenerator
12
{
13
    CONST PLURAL_NUMBER_REQUIRED = [
14
        'belongsToMany',
15
        'hasMany'
16
    ];
17

18
    public function generate(): void
19
    {
20
        if ($this->classExists('models', $this->model)) {
×
21
            $this->throwFailureException(
×
22
                ClassAlreadyExistsException::class,
×
23
                "Cannot create {$this->model} Model cause {$this->model} Model already exists.",
×
24
                "Remove {$this->model} Model."
×
25
            );
×
26
        }
27

28
        if ($this->isStubExists('model') && ($this->isStubExists('relation') || empty($this->relations))) {
×
29
            $this->prepareRelatedModels();
×
30
            $modelContent = $this->getNewModelContent();
×
31

32
            $this->saveClass('models', $this->model, $modelContent);
×
33

34
            event(new SuccessCreateMessage("Created a new Model: {$this->model}"));
×
35
        }
36
    }
37

38
    protected function getNewModelContent(): string
39
    {
40
        return $this->getStub('model', [
×
41
            'entity' => $this->model,
×
42
            'fields' => Arr::collapse($this->fields),
×
43
            'relations' => $this->prepareRelations(),
×
44
            'casts' => $this->getCasts($this->fields),
×
45
            'namespace' => $this->getOrCreateNamespace('models')
×
46
        ]);
×
47
    }
48

49
    public function prepareRelatedModels(): void
50
    {
51
        $types = [
×
52
            'hasMany' => 'belongsTo',
×
53
            'hasOne' => 'belongsTo',
×
54
            'belongsTo' => 'hasOne',
×
55
            'belongsToMany' => 'belongsToMany',
×
56
        ];
×
57

58
        foreach ($this->relations as $type => $relationsByType) {
×
59
            foreach ($relationsByType as $relation) {
×
60
                if (!$this->classExists('models', $relation)) {
×
61
                    $this->throwFailureException(
×
62
                        ClassNotExistsException::class,
×
63
                        "Cannot create {$relation} Model cause {$relation} Model does not exists.",
×
64
                        "Create a {$relation} Model by himself or run command 'php artisan make:entity {$relation} --only-model'."
×
65
                    );
×
66
                }
67

68
                $content = $this->getModelContent($relation);
×
69

70
                $newRelation = $this->getStub('relation', [
×
71
                    'name' => $this->getRelationName($this->model, $types[$type]),
×
72
                    'type' => $types[$type],
×
73
                    'entity' => $this->model
×
74
                ]);
×
75

76
                $fixedContent = preg_replace('/\}$/', "\n    {$newRelation}\n}", $content);
×
77

78
                $this->saveClass('models', $relation, $fixedContent);
×
79
            }
80
        }
81
    }
82

83
    public function getModelContent($model): string
84
    {
85
        $modelPath = base_path($this->paths['models'] . "/{$model}.php");
×
86

87
        return file_get_contents($modelPath);
×
88
    }
89

90
    public function prepareRelations(): array
91
    {
92
        $result = [];
×
93

94
        foreach ($this->relations as $type => $relations) {
×
95
            foreach ($relations as $relation) {
×
96
                if (!empty($relation)) {
×
97
                    $result[] = [
×
98
                        'name' => $this->getRelationName($relation, $type),
×
99
                        'type' => $type,
×
100
                        'entity' => $relation
×
101
                    ];
×
102
                }
103
            }
104
        }
105

106
        return $result;
×
107
    }
108

109
    protected function getCasts($fields): array
110
    {
111
        $casts = [
×
112
            'boolean-required' => 'boolean',
×
113
            'boolean' => 'boolean',
×
114
            'json' => 'array'
×
115
        ];
×
116

117
        $result = [];
×
118

119
        foreach ($fields as $fieldType => $names) {
×
120
            if (empty($casts[$fieldType])) {
×
121
                continue;
×
122
            }
123

124
            foreach ($names as $name) {
×
125
                $result[$name] = $casts[$fieldType];
×
126
            }
127
        }
128

129
        return $result;
×
130
    }
131

132
    private function getRelationName($relation, $type): string
133
    {
134
        $relationName = Str::snake($relation);
×
135

136
        if (in_array($type, self::PLURAL_NUMBER_REQUIRED)) {
×
137
            $relationName = Str::plural($relationName);
×
138
        }
139

140
        return $relationName;
×
141
    }
142
}
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