Skip to content

Commit da9c61d

Browse files
committed
feat(legacy-create-video): [golden-master] extract VideoCreator class
1 parent 15d90db commit da9c61d

File tree

2 files changed

+51
-29
lines changed

2 files changed

+51
-29
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Application;
6+
7+
final class VideoCreator
8+
{
9+
public function createVideo($title, $url, $courseId, object $connection): array
10+
{
11+
$title = $this->sanitizeTitle($title);
12+
13+
$sql = "INSERT INTO video (title, url, course_id)
14+
VALUES (\"{$title}\",
15+
\"{$url}\",
16+
{$courseId}
17+
)";
18+
19+
// Prepare doctrine statement
20+
$stmt = $connection->prepare($sql);
21+
$stmt->execute();
22+
23+
// IMPORTANT: Obtaining the video id. Take care, it's done without another query :)
24+
$videoId = $connection->lastInsertId();
25+
return array($title, $videoId);
26+
}
27+
28+
private function sanitizeTitle(string $title): string
29+
{
30+
if (strpos($title, "hexagonal")) {
31+
$title = str_replace("hexagonal", "Hexagonal", $title);
32+
}
33+
if (strpos($title, "solid")) {
34+
$title = str_replace("solid", "SOLID", $title);
35+
}
36+
if (strpos($title, "tdd")) {
37+
$title = str_replace("tdd", "TDD", $title);
38+
}
39+
return $title;
40+
}
41+
}

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

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace AppBundle\Controller;
44

5+
use AppBundle\Application\VideoCreator;
56
use Symfony\Component\HttpFoundation\Request;
67

78
/**
@@ -10,6 +11,14 @@
1011
*/
1112
class VideoController extends BaseController
1213
{
14+
/** @var VideoCreator */
15+
private $videoCreator;
16+
17+
public function __construct()
18+
{
19+
$this->videoCreator = new VideoCreator();
20+
}
21+
1322
/**
1423
* Method used to create a new video
1524
* @todo Validate the request
@@ -33,36 +42,8 @@ public function postVideoAction(Request $request)
3342
];
3443
}
3544

36-
private function sanitizeTitle(string $title): string
37-
{
38-
if (strpos($title, "hexagonal")) {
39-
$title = str_replace("hexagonal", "Hexagonal", $title);
40-
}
41-
if (strpos($title, "solid")) {
42-
$title = str_replace("solid", "SOLID", $title);
43-
}
44-
if (strpos($title, "tdd")) {
45-
$title = str_replace("tdd", "TDD", $title);
46-
}
47-
return $title;
48-
}
49-
5045
private function createVideo($title, $url, $courseId, object $connection): array
5146
{
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);
47+
return $this->videoCreator->createVideo($title, $url, $courseId, $connection);
6748
}
6849
}

0 commit comments

Comments
 (0)