Browse Source

BGC-192 @fixed patch

raphael 3 years ago
parent
commit
38508a71dd

+ 70
- 0
src/Phoenix/DoctrineMigrations/Version20191203165250.php View File

@@ -0,0 +1,70 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+namespace Logipro\Phoenix\DoctrineMigrations;
6
+
7
+use Doctrine\DBAL\Schema\Schema;
8
+use Doctrine\Migrations\AbstractMigration;
9
+use Symfony\Component\Filesystem\Filesystem;
10
+use Logipro\Phoenix\Service\FileUploader;
11
+use Logipro\Phoenix\Service\FileUploaderInterface;
12
+use Logipro\Phoenix\Entity\Sequence;
13
+use Symfony\Component\DependencyInjection\ContainerAwareInterface;
14
+use Symfony\Component\DependencyInjection\ContainerInterface;
15
+
16
+final class Version20191203165250 extends AbstractMigration
17
+implements ContainerAwareInterface
18
+{
19
+    private $fileUploader;
20
+
21
+    public function setContainer(ContainerInterface $container = null)
22
+    {
23
+        $this->fileUploader = $container->get(FileUploader::class);
24
+    }
25
+    public function up(Schema $schema): void
26
+    {
27
+        $fileUploader = $this->fileUploader;
28
+        $filesystem = new Filesystem();
29
+        $sequence = new Sequence();
30
+        $propertyName = 'image';
31
+
32
+        // extrait les images des sequences et regarde les sequences qui utilisent plusieurs fois la même image
33
+        $query = "SELECT COUNT(*) as `number`,`image` FROM `phx_sequence` WHERE `image` != '' GROUP BY `image` HAVING `number` > 1";
34
+        foreach ($this->connection->fetchAll($query) as $result) {
35
+            $imageName = $result['image'];
36
+
37
+            // recherche les identifiants des sequences de l'image
38
+            $query = 'SELECT `sequence_id` FROM `phx_sequence` WHERE `image`=\'' . $imageName . '\'';
39
+            $results = $this->connection->fetchAll($query);
40
+
41
+            // supprime le premier
42
+            array_shift($results);
43
+
44
+            // parcours les identifiants pour la copie
45
+            $queries = array();
46
+            foreach ($results as $result) {
47
+                $newFileName = $fileUploader->generateNewName($imageName);
48
+                try {
49
+                    $filesystem->copy(
50
+                        $fileUploader->buildUploadPublicPath($imageName, $sequence, $propertyName),
51
+                        $fileUploader->buildUploadPublicPath($newFileName, $sequence, $propertyName)
52
+                    );
53
+
54
+                    // après la copie, on met a jour l'objet
55
+                    $queries[] = 'UPDATE `phx_sequence` SET `image`=\'' . $newFileName . '\' WHERE `sequence_id`=' . $result['sequence_id'];
56
+                } catch (\Exception $exp) {
57
+                    dump($exp->getMessage());
58
+                }
59
+            }
60
+
61
+            foreach ($queries as $query)
62
+            {
63
+                $this->connection->executeQuery($query);
64
+            }
65
+        }
66
+    }
67
+
68
+    public function down(Schema $schema): void
69
+    { }
70
+}

+ 52
- 27
src/Phoenix/Service/FileUploader.php View File

@@ -97,14 +97,27 @@ implements FileUploaderInterface
97 97
 	 * @param string $path Chemin relatif du fichier
98 98
 	 * @return string
99 99
 	 */
100
-	public function buildUploadPublicPath(string $path)
100
+	public function buildUploadPublicPath(string $path = null, AbstractEntity $entity = null, string $propertyName = null)
101 101
 	{
102
-		$pathPrefix = $this->getUploadPublicRootPath();
103
-
104 102
 		// Elimine le slash au début
105
-		$path = ltrim($path, '/');
103
+		$path = ltrim($path, DIRECTORY_SEPARATOR);
106 104
 
107
-		return sprintf('%s/%s', $pathPrefix, $path);
105
+		if ($entity && $propertyName) {
106
+			// ici, sert uniquement à tester si la méthode de l'entité existe
107
+			$this->getEntityValue($entity, $propertyName);
108
+
109
+			// plugin vich uploader
110
+			$mapping = $this->factory->fromObject($entity);
111
+			foreach ($mapping as $map) {
112
+
113
+				if ($map->getFileNamePropertyName() == $propertyName) {
114
+					$pathPrefix = $map->getUploadDestination();
115
+					return $pathPrefix . DIRECTORY_SEPARATOR . $path;
116
+				}
117
+			}
118
+		}
119
+		$pathPrefix = $this->getUploadPublicRootPath();
120
+		return $pathPrefix . DIRECTORY_SEPARATOR . $path;
108 121
 	}
109 122
 
110 123
 	/**
@@ -123,6 +136,14 @@ implements FileUploaderInterface
123 136
 		return sprintf('%s/%s', $pathPrefix, $path);
124 137
 	}
125 138
 
139
+	public function getUploadPublicPath(AbstractEntity $entity, string $propertyName)
140
+	{
141
+		$oldFileName = $this->getEntityValue($entity, $propertyName);
142
+
143
+		// fonctionnement normal
144
+		return $this->buildUploadPublicPath($oldFileName, $entity, $propertyName);
145
+	}
146
+
126 147
 	/**
127 148
 	 * Retourne l'URL complète vers un fichier à partir de son chemin
128 149
 	 * relatif dans le répertoire public des uploads.
@@ -139,32 +160,36 @@ implements FileUploaderInterface
139 160
 		return sprintf('%s/%s', $urlPrefix, $path);
140 161
 	}
141 162
 
142
-	public function cloneFile(AbstractEntity $entity, string $propertyName): string
163
+	private function getEntityValue(AbstractEntity $entity, string $propertyName)
164
+	{
165
+		// test le lien entre le propriété et l'objet
166
+		$getter = 'get' . ucfirst($propertyName);
167
+		if (!method_exists($entity, $getter)) {
168
+			throw new \Exception();
169
+		}
170
+		return $entity->{$getter}();
171
+	}
172
+
173
+	public function generateNewName(string $oldFileName): string
143 174
 	{
144
-		$mapping = $this->factory->fromObject($entity);
175
+		return uniqid() . '.' . pathinfo($oldFileName, PATHINFO_EXTENSION);
176
+	}
145 177
 
146
-		$newFileName = '';
178
+	public function cloneFile(AbstractEntity $entity, string $propertyName): string
179
+	{
147 180
 		$filesystem = new Filesystem();
148
-		foreach ($mapping as $map) {
149
-
150
-			if ($map->getFileNamePropertyName() == $propertyName) {
151
-				$getter = 'get' . ucfirst($propertyName);
152
-				$oldFileName = $entity->$getter();
153
-				$oldFilePath = $map->getUploadDestination() . DIRECTORY_SEPARATOR . $oldFileName;
154
-
155
-				if (is_file($oldFilePath)) {
156
-					$newFileName = uniqid() . '.' . pathinfo($oldFileName, PATHINFO_EXTENSION);
157
-					$newFilePath = $map->getUploadDestination() . DIRECTORY_SEPARATOR . $newFileName;
158
-
159
-					try {
160
-						$filesystem->copy($oldFilePath, $newFilePath);
161
-					} catch (\Exception $exp) {
162
-						dd($exp->getMessage());
163
-					}
164
-				}
165
-				break;
166
-			}
181
+		$oldFileName = $this->getEntityValue($entity, $propertyName);
182
+		$newFileName = $this->generateNewName($oldFileName);
183
+
184
+		try {
185
+			$filesystem->copy(
186
+				$this->buildUploadPublicPath($oldFileName, $entity, $propertyName),
187
+				$this->buildUploadPublicPath($newFileName, $entity, $propertyName)
188
+			);
189
+		} catch (\Exception $exp) {
190
+			dd($exp->getMessage());
167 191
 		}
192
+
168 193
 		return $newFileName;
169 194
 	}
170 195
 }

Loading…
Cancel
Save