Completed
Pull Request — master (#30)
by
unknown
04:27
created

ImageSubscriber::prePersist()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.032
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 15
    public function __construct(S3Manager $s3)
19
    {
20 15
        $this->s3 = $s3;
21 15
    }
22
23 15
    public function getSubscribedEvents()
24
    {
25
        return [
26 15
            'prePersist',
27
            'preUpdate',
28
            'postRemove',
29
        ];
30
    }
31
32
    /**
33
     * @param PreUpdateEventArgs $args
34
     */
35 7
    public function preUpdate(PreUpdateEventArgs $args)
36
    {
37 7
        $object = $args->getObject();
38
39 7
        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 7
    }
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