ImageUploadSubscriber::getSubscribedEvents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace DoS\ResourceBundle\EventListener;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\ORM\Event\LifecycleEventArgs;
7
use DoS\ResourceBundle\Model\ImageInterface;
8
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
class ImageUploadSubscriber implements ContainerAwareInterface, EventSubscriber
12
{
13
    /**
14
     * @var ContainerInterface;
15
     */
16
    private $container;
17
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function setContainer(ContainerInterface $container = null)
22
    {
23
        $this->container = $container;
24
    }
25
26
    /**
27
     * @param $subject
28
     */
29
    private function upload($subject)
30
    {
31
        if ($subject instanceof ImageInterface && $subject->hasFile()) {
32
            $this->container->get('dos.image_uploader')->upload($subject);
33
        }
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function prePersist(LifecycleEventArgs $event)
40
    {
41
        $this->upload($event->getObject());
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function preUpdate(LifecycleEventArgs $event)
48
    {
49
        $this->upload($event->getObject());
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getSubscribedEvents()
56
    {
57
        return array(
58
            'prePersist',
59
            'preUpdate',
60
        );
61
    }
62
}
63