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

api-platform / core / 15255731762

26 May 2025 01:55PM UTC coverage: 0.0% (-26.5%) from 26.526%
15255731762

Pull #7176

github

web-flow
Merge 66f6cf4d2 into 79edced67
Pull Request #7176: Merge 4.1

0 of 387 new or added lines in 28 files covered. (0.0%)

11394 existing lines in 372 files now uncovered.

0 of 51334 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Laravel/Eloquent/State/PersistProcessor.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Laravel\Eloquent\State;
15

16
use ApiPlatform\Laravel\Eloquent\Metadata\ModelMetadata;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\Operation;
20
use ApiPlatform\State\ProcessorInterface;
21
use Illuminate\Database\Eloquent\Relations\BelongsTo;
22
use Illuminate\Database\Eloquent\Relations\HasMany;
23

24
/**
25
 * @implements ProcessorInterface<\Illuminate\Database\Eloquent\Model, \Illuminate\Database\Eloquent\Model>
26
 */
27
final class PersistProcessor implements ProcessorInterface
28
{
29
    /**
30
     * @var array<string, string>
31
     */
32
    private array $relations;
33

34
    public function __construct(
35
        private readonly ModelMetadata $modelMetadata,
36
    ) {
37
    }
×
38

39
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = [])
40
    {
NEW
41
        $toMany = [];
×
42

43
        foreach ($this->modelMetadata->getRelations($data) as $relation) {
×
44
            if (!isset($data->{$relation['name']})) {
×
45
                continue;
×
46
            }
47

48
            if (BelongsTo::class === $relation['type']) {
×
NEW
49
                $rel = $data->{$relation['name']};
×
50

NEW
51
                if (!$rel->exists) {
×
NEW
52
                    $rel->save();
×
53
                }
54

NEW
55
                $data->{$relation['method_name']}()->associate($data->{$relation['name']});
×
NEW
56
                unset($data->{$relation['name']});
×
NEW
57
                $this->relations[$relation['method_name']] = $relation['name'];
×
58
            }
59

NEW
60
            if (HasMany::class === $relation['type']) {
×
NEW
61
                $rel = $data->{$relation['name']};
×
62

NEW
63
                if (!\is_array($rel)) {
×
NEW
64
                    throw new RuntimeException('To-Many relationship is not a collection.');
×
65
                }
66

NEW
67
                $toMany[$relation['method_name']] = $rel;
×
UNCOV
68
                unset($data->{$relation['name']});
×
NEW
69
                $this->relations[$relation['method_name']] = $relation['name'];
×
70
            }
71
        }
72

73
        if (($previousData = $context['previous_data'] ?? null) && $operation instanceof HttpOperation && 'PUT' === $operation->getMethod() && ($operation->getExtraProperties()['standard_put'] ?? true)) {
×
74
            foreach ($this->modelMetadata->getAttributes($data) as $attribute) {
×
75
                if ($attribute['primary'] ?? false) {
×
76
                    $data->{$attribute['name']} = $previousData->{$attribute['name']};
×
77
                }
78
            }
79
            $data->exists = true;
×
80
        }
81

82
        $data->saveOrFail();
×
83
        $data->refresh();
×
84

NEW
85
        foreach ($data->getRelations() as $methodName => $obj) {
×
NEW
86
            if (isset($this->relations[$methodName])) {
×
NEW
87
                $data->{$this->relations[$methodName]} = $obj;
×
88
            }
89
        }
90

NEW
91
        foreach ($toMany as $methodName => $relations) {
×
NEW
92
            $data->{$methodName}()->saveMany($relations);
×
NEW
93
            $data->{$this->relations[$methodName]} = $relations;
×
NEW
94
            unset($toMany[$methodName]);
×
95
        }
96

UNCOV
97
        return $data;
×
98
    }
99
}
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