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

tochka-developers / queue-promises / 11937060552

20 Nov 2024 03:58PM UTC coverage: 46.235% (-0.4%) from 46.616%
11937060552

push

github

darkdarin
fix: при ошибке сериализации исключения - конвертируем исключение в то, которое точно сериализуется

2 of 14 new or added lines in 2 files covered. (14.29%)

614 of 1328 relevant lines covered (46.23%)

2.47 hits per line

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

78.95
/src/Models/Casts/SerializableClassCast.php
1
<?php
2

3
namespace Tochka\Promises\Models\Casts;
4

5
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
6
use Tochka\Promises\Exceptions\IncorrectResolvingClass;
7
use Tochka\Promises\Exceptions\NonSerializableException;
8

9
/**
10
 * @template-implements CastsAttributes<object, string>
11
 */
12
class SerializableClassCast implements CastsAttributes
13
{
14
    /**
15
     * @throws \JsonException
16
     */
17
    public function get($model, string $key, $value, array $attributes)
18
    {
19
        if ($value === null) {
9✔
20
            return null;
3✔
21
        }
22

23
        $castedObject = unserialize(
6✔
24
            json_decode($value, true, 512, JSON_THROW_ON_ERROR),
6✔
25
            ['allowed_classes' => true],
6✔
26
        );
6✔
27

28
        if ($castedObject instanceof \__PHP_Incomplete_Class) {
6✔
29
            throw new IncorrectResolvingClass(
3✔
30
                'Unknown class after deserialization. Most likely the serialized class no longer exists.',
3✔
31
            );
3✔
32
        }
33

34
        return $castedObject;
3✔
35
    }
36

37
    /**
38
     * @throws \JsonException
39
     * @throws \Throwable
40
     */
41
    public function set($model, string $key, $value, array $attributes): array
42
    {
43
        try {
44
            $serializedValue = serialize($value);
6✔
NEW
45
        } catch (\Throwable $e) {
×
NEW
46
            if ($value instanceof \Throwable) {
×
NEW
47
                $serializedValue = serialize(new NonSerializableException($value));
×
48
            } else {
NEW
49
                throw $e;
×
50
            }
51
        }
52
        return [
6✔
53
            $key => $value === null ? null : json_encode($serializedValue, JSON_THROW_ON_ERROR),
6✔
54
        ];
6✔
55
    }
56
}
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