Completed
Push — feature/EVO-7278-security-and-... ( ae4d63...67cd5b )
by
unknown
26:50 queued 21:17
created

EmptyExtRefListener::onPreSerialize()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 7
cp 0
cc 3
eloc 4
nc 2
nop 1
crap 12
1
<?php
2
/**
3
 * see that empty extrefs get rendered as type Empty, ie as null
4
 */
5
6
namespace Graviton\DocumentBundle\Serializer\Listener;
7
8
use Graviton\DocumentBundle\Document\ExtRefHoldingDocumentInterface;
9
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
10
11
/**
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class EmptyExtRefListener
18
{
19
20
    /**
21
     * if is empty extref object, divert serializing to type Empty, which creates a null instead of empty object
22
     *
23
     * @param PreSerializeEvent $event event
24
     *
25
     * @return void
26
     * @throws \Exception
27
     */
28
    public function onPreSerialize(PreSerializeEvent $event)
29
    {
30
        $object = $event->getObject();
31
        if ($object instanceof ExtRefHoldingDocumentInterface && $object->isEmptyExtRefObject() === true) {
32
            $event->setType('Empty', [$object]);
33
        }
34
    }
35
}
36