Completed
Pull Request — master (#30)
by nonanerz
04:41
created

ImageSubscriber   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 56.52%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 62
ccs 13
cts 23
cp 0.5652
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSubscribedEvents() 0 8 1
A preUpdate() 0 13 4
A prePersist() 0 8 2
A postRemove() 0 8 2
1
<?php
2
3
namespace AppBundle\Listener;
4
5
use AppBundle\Aws\S3Manager;
6
use AppBundle\Entity\S3\Image;
7
use Doctrine\Common\EventSubscriber;
8
use Doctrine\ORM\Event\LifecycleEventArgs;
9
use Doctrine\ORM\Event\PreUpdateEventArgs;
10
11
class ImageSubscriber implements EventSubscriber
12
{
13
    /**
14
     * @var S3Manager
15
     */
16
    private $s3;
17
18 14
    public function __construct(S3Manager $s3)
19
    {
20 14
        $this->s3 = $s3;
21 14
    }
22
23 14
    public function getSubscribedEvents()
24
    {
25
        return [
26 14
            'prePersist',
27
            'preUpdate',
28
            'postRemove',
29
        ];
30
    }
31
32
    /**
33
     * @param PreUpdateEventArgs $args
34
     */
35 5
    public function preUpdate(PreUpdateEventArgs $args)
36
    {
37 5
        $object = $args->getObject();
38
39 5
        if ($object instanceof Image) {
40
            if ($object->getFile()) {
0 ignored issues
show
Bug introduced by
The method getFile() does not seem to exist on object<AppBundle\Entity\S3\Image>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
41
                if ($object->getOldS3key()) {
42
                    $this->s3->removeImage($object->getOldS3key());
43
                }
44
                $this->s3->upload($object);
45
            }
46
        }
47 5
    }
48
49
    /**
50
     * @param LifecycleEventArgs $args
51
     */
52 2
    public function prePersist(LifecycleEventArgs $args)
53
    {
54 2
        $object = $args->getObject();
55
56 2
        if ($object instanceof Image) {
57
            $this->s3->upload($object);
58
        }
59 2
    }
60
61
    /**
62
     * @param LifecycleEventArgs $args
63
     */
64
    public function postRemove(LifecycleEventArgs $args)
65
    {
66
        $object = $args->getObject();
67
68
        if ($object instanceof Image) {
69
            $this->s3->removeImage($object->getS3key());
70
        }
71
    }
72
}
73