Entity   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 58
ccs 21
cts 21
cp 1
rs 10
c 2
b 1
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A postRemove() 0 10 3
A postUpdate() 0 7 2
A removeOldFiles() 0 7 2
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\AppBundle\Event\Listener;
10
11
use Symfony\Component\Filesystem\Filesystem;
12
use Doctrine\ORM\Event\LifecycleEventArgs;
13
use AnimeDb\Bundle\AppBundle\Service\Downloader\Entity\EntityInterface;
14
15
class Entity
16
{
17
    /**
18
     * @var Filesystem
19
     */
20
    protected $fs;
21
22
    /**
23
     * @var string
24
     */
25
    protected $root = '';
26
27
    /**
28
     * @param Filesystem $fs
29
     * @param string $root
30
     */
31 8
    public function __construct(Filesystem $fs, $root)
32
    {
33 8
        $this->fs = $fs;
34 8
        $this->root = $root;
35 8
    }
36
37
    /**
38
     * @param LifecycleEventArgs $args
39
     */
40 5
    public function postRemove(LifecycleEventArgs $args)
41
    {
42 5
        $entity = $args->getEntity();
43 5
        if ($entity instanceof EntityInterface) {
44 4
            if ($entity->getFilename()) {
45 2
                $this->fs->remove($this->root.$entity->getDownloadPath().'/'.$entity->getFilename());
46
            }
47 4
            $this->removeOldFiles($entity);
48
        }
49 5
    }
50
51
    /**
52
     * @param LifecycleEventArgs $args
53
     */
54 3
    public function postUpdate(LifecycleEventArgs $args)
55
    {
56 3
        $entity = $args->getEntity();
57 3
        if ($entity instanceof EntityInterface) {
58 2
            $this->removeOldFiles($entity);
59
        }
60 3
    }
61
62
    /**
63
     * @param EntityInterface $entity
64
     */
65 6
    protected function removeOldFiles(EntityInterface $entity)
66
    {
67 6
        $root = $this->root.$entity->getDownloadPath().'/';
68 6
        foreach ($entity->getOldFilenames() as $filename) {
69 3
            $this->fs->remove($root.$filename);
70
        }
71 6
    }
72
}
73