FileHelper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace KI\PonthubBundle\Helper;
4
5
use Doctrine\ORM\EntityManager;
6
use KI\PonthubBundle\Entity\Episode;
7
use KI\PonthubBundle\Entity\Game;
8
use KI\PonthubBundle\Entity\Genre;
9
use KI\PonthubBundle\Entity\Movie;
10
use KI\PonthubBundle\Entity\Other;
11
use KI\PonthubBundle\Entity\Serie;
12
use KI\PonthubBundle\Entity\Software;
13
14
/**
15
 * Class FileHelper
16
 * @package KI\PonthubBundle\Helper
17
 */
18
class FileHelper
19
{
20
    /**
21
     * @var EntityManager
22
     */
23
    protected $manager;
24
25
    /**
26
     * FileHelper constructor.
27
     * @param EntityManager $manager
28
     */
29
    public function __construct(EntityManager $manager)
0 ignored issues
show
Bug introduced by
You have injected the EntityManager via parameter $manager. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

Loading history...
30
    {
31
        $this->manager = $manager;
32
    }
33
34
    /**
35
     * @param string $path
36
     * @param string $name
37
     * @param integer $size
38
     */
39 View Code Duplication
    public function tryToStoreMovie($path, $name, $size)
40
    {
41
        if (!preg_match('#^/root/web/films/#', $path) || $size == 0) {
42
            return;
43
        }
44
45
        $item = new Movie();
46
        $item = $this->basicInfos($item, $size, $path, $name);
47
        $this->manager->persist($item);
48
    }
49
50
    /**
51
     * @param string $path
52
     * @param string $name
53
     * @param integer $size
54
     */
55 View Code Duplication
    public function tryToStoreGame($path, $name, $size)
56
    {
57
        if (!preg_match('#^/root/web/jeux/#', $path) || $size == 0) {
58
            return;
59
        }
60
61
        $item = new Game();
62
        $item = $this->basicInfos($item, $size, $path, $name);
63
        $this->manager->persist($item);
64
    }
65
66
    /**
67
     * @param string $path
68
     * @param string $name
69
     * @param integer $size
70
     */
71 View Code Duplication
    public function tryToStoreSoftware($path, $name, $size)
72
    {
73
        if (!preg_match('#^/root/web/logiciels/#', $path) || $size == 0) {
74
            return;
75
        }
76
77
        $item = new Software();
78
        $item = $this->basicInfos($item, $size, $path, $name);
79
        $this->manager->persist($item);
80
    }
81
82
    /**
83
     * @param string $path
84
     * @param string $name
85
     * @param integer $size
86
     */
87 View Code Duplication
    public function tryToStoreOther($path, $name, $size)
88
    {
89
        if (!preg_match('#^/root/web/autres/#', $path) || $size == 0) {
90
            return;
91
        }
92
93
        $item = new Other();
94
        $item = $this->basicInfos($item, $size, $path, $name);
95
        $this->manager->persist($item);
96
    }
97
98
    /**
99
     * @param Serie[] $series
100
     * @param string[] $pathsDone
101
     * @param string $ext
102
     * @param string $path
103
     * @param string $name
104
     * @param integer $size
105
     */
106
    public function tryToStoreSerie(&$series, &$pathsDone, $ext, $path, $name, $size)
0 ignored issues
show
Unused Code introduced by
The parameter $ext is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
107
    {
108
        if (!preg_match('#^/root/web/series/.+#', $path)) {
109
            return;
110
        }
111
112
        preg_match('#^(/root/web/series/(.+?)/)#', $path, $matches);
113
114
        list(, $seriePath, $serieName) = $matches;
115
116
        // Si la série existe, on la récupère, sinon on la rajoute
117
        if (!isset($series[$seriePath])) {
118
            $serieItem = new Serie();
119
            $serieItem = $this->basicInfos($serieItem, null, $seriePath, $serieName);
120
            $this->manager->persist($serieItem);
121
            $series[$seriePath] = $serieItem;
122
        } else {
123
            $serieItem = $series[$seriePath];
124
        }
125
126
        if (!in_array($seriePath, $pathsDone)) {
127
            $pathsDone[] = $seriePath;
128
        }
129
130
        //On range l'épisode en commencant par déterminer le numéro de saison et d'épisode
131
        if (!preg_match('#^S([0-9]{2}) E([0-9]{2})#', $name, $matches)) {
132
            return;
133
        }
134
135
        list(, $numSaison, $numEpisode) = $matches;
136
        $item = new Episode();
137
        $item = $this->basicInfos($item, $size, $path, $name);
138
        $item->setStatus('OK');
139
        $item->setSeason($numSaison);
140
        $item->setNumber($numEpisode);
141
        $item->setSerie($serieItem);
142
143
        // On actualise la date de modification de la série
144
        $serieItem->setAdded(time());
145
        $this->manager->persist($item);
146
        $pathsDone[] = $path;
147
    }
148
149
    /**
150
     * @param \KI\PonthubBundle\Entity\PonthubFile $item
151
     * @param integer $size
152
     * @param string $path
153
     * @param string $name
154
     * @return \KI\PonthubBundle\Entity\PonthubFile
155
     */
156
    private function basicInfos($item, $size, $path, $name)
157
    {
158
        $item->setSize($size);
159
        $item->setAdded(time());
160
        $item->setPath($path);
161
        $item->setStatus('OK');
162
        $item->setName($name);
163
        return $item;
164
    }
165
}
166