Completed
Push — master ( 1e2cfa...28995d )
by Eric
04:38
created

EventClassMetadataFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Ivory Serializer package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Ivory\Serializer\Mapping\Factory;
13
14
use Ivory\Serializer\Event\ClassMetadataLoadEvent;
15
use Ivory\Serializer\Event\ClassMetadataNotFoundEvent;
16
use Ivory\Serializer\Event\SerializerEvents;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
19
/**
20
 * @author GeLo <[email protected]>
21
 */
22
class EventClassMetadataFactory implements ClassMetadataFactoryInterface
23
{
24
    /**
25
     * @var ClassMetadataFactoryInterface
26
     */
27
    private $factory;
28
29
    /**
30
     * @var EventDispatcherInterface
31
     */
32
    private $dispatcher;
33
34
    /**
35
     * @param ClassMetadataFactoryInterface $factory
36
     * @param EventDispatcherInterface      $dispatcher
37
     */
38 8
    public function __construct(ClassMetadataFactoryInterface $factory, EventDispatcherInterface $dispatcher)
39
    {
40 8
        $this->factory = $factory;
41 8
        $this->dispatcher = $dispatcher;
42 8
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 8
    public function getClassMetadata($class)
48
    {
49 8
        $classMetadata = $this->factory->getClassMetadata($class);
50
51 8
        if ($classMetadata === null) {
52 4
            $this->dispatcher->dispatch(
53 4
                SerializerEvents::CLASS_METADATA_NOT_FOUND,
54 4
                $event = new ClassMetadataNotFoundEvent($class)
55 2
            );
56 2
        } else {
57 4
            $this->dispatcher->dispatch(
58 4
                SerializerEvents::CLASS_METADATA_LOAD,
59 4
                $event = new ClassMetadataLoadEvent($classMetadata)
60 2
            );
61
        }
62
63 8
        return $event->getClassMetadata();
64
    }
65
}
66