Skip to content

Commit 15d90db

Browse files
committed
feat(legacy-create-video): [golden-master] extract createVideo method
1 parent 92bbcc3 commit 15d90db

File tree

1 file changed

+20
-14
lines changed
  • exercises/legacy_create_video/solutions/codely_php-symfony_golden-master/src/AppBundle/Controller

1 file changed

+20
-14
lines changed

exercises/legacy_create_video/solutions/codely_php-symfony_golden-master/src/AppBundle/Controller/VideoController.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,9 @@ public function postVideoAction(Request $request)
2020
$title = $request->get('title');
2121
$url = $request->get('url');
2222
$courseId = $request->get('course_id');
23-
24-
$title = $this->sanitizeTitle($title);
25-
26-
$sql = "INSERT INTO video (title, url, course_id)
27-
VALUES (\"{$title}\",
28-
\"{$url}\",
29-
{$courseId}
30-
)";
31-
32-
// Prepare doctrine statement
3323
$connection = $this->getDoctrine()->getConnection();
34-
$stmt = $connection->prepare($sql);
35-
$stmt->execute();
3624

37-
// IMPORTANT: Obtaining the video id. Take care, it's done without another query :)
38-
$videoId = $connection->lastInsertId();
25+
list($title, $videoId) = $this->createVideo($title, $url, $courseId, $connection);
3926

4027
// And we return the video created
4128
return [
@@ -59,4 +46,23 @@ private function sanitizeTitle(string $title): string
5946
}
6047
return $title;
6148
}
49+
50+
private function createVideo($title, $url, $courseId, object $connection): array
51+
{
52+
$title = $this->sanitizeTitle($title);
53+
54+
$sql = "INSERT INTO video (title, url, course_id)
55+
VALUES (\"{$title}\",
56+
\"{$url}\",
57+
{$courseId}
58+
)";
59+
60+
// Prepare doctrine statement
61+
$stmt = $connection->prepare($sql);
62+
$stmt->execute();
63+
64+
// IMPORTANT: Obtaining the video id. Take care, it's done without another query :)
65+
$videoId = $connection->lastInsertId();
66+
return array($title, $videoId);
67+
}
6268
}

0 commit comments

Comments
 (0)