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

daycry / jobs / 20522746527

19 Nov 2025 03:37PM UTC coverage: 62.561% (+0.06%) from 62.5%
20522746527

push

github

daycry
- Improvements

22 of 25 new or added lines in 12 files covered. (88.0%)

2 existing lines in 1 file now uncovered.

1148 of 1835 relevant lines covered (62.56%)

4.69 hits per line

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

38.89
/src/Libraries/JsonPayloadSerializer.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Queues.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Jobs\Libraries;
15

16
use Daycry\Jobs\Interfaces\PayloadSerializerInterface;
17
use JsonException;
18

19
/**
20
 * JSON serializer with optional schema versioning.
21
 *
22
 * Features:
23
 *  - Automatic schema version injection (_schemaVersion field)
24
 *  - Graceful handling of legacy payloads without version
25
 *  - Validation of required fields
26
 *  - Migration path for schema upgrades
27
 *
28
 * Usage:
29
 *   $serializer = new JsonPayloadSerializer(schemaVersion: 2);
30
 *   $json = $serializer->serialize($payload);
31
 *   $obj = $serializer->deserialize($json);
32
 */
33
class JsonPayloadSerializer implements PayloadSerializerInterface
34
{
35
    private const DEFAULT_SCHEMA_VERSION = 1;
36

37
    public function __construct(
38
        private ?int $schemaVersion = self::DEFAULT_SCHEMA_VERSION,
39
    ) {
40
    }
9✔
41

42
    public function serialize(object $payload): string
43
    {
44
        // Clonar para evitar mutar el original
45
        $data = json_decode(json_encode($payload));
8✔
46

47
        // Inyectar versión de esquema si está configurada
48
        if ($this->schemaVersion !== null && ! isset($data->_schemaVersion)) {
8✔
49
            $data->_schemaVersion = $this->schemaVersion;
8✔
50
        }
51

52
        return json_encode($data, JSON_THROW_ON_ERROR);
8✔
53
    }
54

55
    public function deserialize(string $data): ?object
56
    {
57
        try {
58
            $decoded = json_decode($data, false, 512, JSON_THROW_ON_ERROR);
5✔
59

60
            return is_object($decoded) ? $decoded : null;
5✔
NEW
61
        } catch (JsonException) {
×
62
            return null;
×
63
        }
64
    }
65

66
    public function getSchemaVersion(object $payload): ?int
67
    {
68
        return isset($payload->_schemaVersion) ? (int) $payload->_schemaVersion : null;
×
69
    }
70

71
    /**
72
     * Valida que el payload contenga campos mínimos requeridos.
73
     *
74
     * @param object $payload        Payload a validar
75
     * @param array  $requiredFields Lista de campos obligatorios
76
     *
77
     * @return bool True si válido
78
     */
79
    public function validate(object $payload, array $requiredFields = ['job']): bool
80
    {
81
        foreach ($requiredFields as $field) {
×
82
            if (! isset($payload->{$field})) {
×
83
                return false;
×
84
            }
85
        }
86

87
        return true;
×
88
    }
89

90
    /**
91
     * Migra un payload de versión antigua a la actual.
92
     * Extiende este método en subclases para migraciones complejas.
93
     *
94
     * @param object $payload Payload a migrar
95
     *
96
     * @return object Payload migrado
97
     */
98
    public function migrate(object $payload): object
99
    {
100
        $version = $this->getSchemaVersion($payload);
×
101

102
        // Sin versión = legacy (pre-v1), actualizar a v1
103
        if ($version === null && $this->schemaVersion !== null) {
×
104
            $payload->_schemaVersion = $this->schemaVersion;
×
105
        }
106

107
        // Aquí podrían agregarse migraciones específicas:
108
        // if ($version === 1 && $this->schemaVersion === 2) { ... }
109

110
        return $payload;
×
111
    }
112
}
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

© 2026 Coveralls, Inc