Skip to content

Commit 8316ffe

Browse files
committed
feat(legacy-create-video): [golden-master] extract MySql video repository
1 parent 6632627 commit 8316ffe

File tree

3 files changed

+44
-24
lines changed

3 files changed

+44
-24
lines changed

exercises/legacy_create_video/solutions/codely_php-symfony_golden-master/src/AppBundle/Application/VideoCreator.php

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44

55
namespace AppBundle\Application;
66

7-
use Doctrine\DBAL\Connection;
7+
use AppBundle\Repository\MySqlVideoRepository;
88

99
final class VideoCreator
1010
{
11-
/** @var Connection */
12-
private $connection;
11+
/** @var MySqlVideoRepository */
12+
private $repository;
1313

14-
public function __construct($connection)
14+
public function __construct(MySqlVideoRepository $repository)
1515
{
16-
$this->connection = $connection;
16+
$this->repository = $repository;
1717
}
1818

1919
public function createVideo($title, $url, $courseId): array
2020
{
2121
$title = $this->sanitizeTitle($title);
2222

23-
$videoId = $this->save($title, $url, $courseId);
23+
$videoId = $this->repository->save($title, $url, $courseId);
2424

2525
return array($title, $videoId);
2626
}
@@ -38,21 +38,4 @@ private function sanitizeTitle(string $title): string
3838
}
3939
return $title;
4040
}
41-
42-
private function save(string $title, $url, $courseId): string
43-
{
44-
$sql = "INSERT INTO video (title, url, course_id)
45-
VALUES (\"{$title}\",
46-
\"{$url}\",
47-
{$courseId}
48-
)";
49-
50-
// Prepare doctrine statement
51-
$stmt = $this->connection->prepare($sql);
52-
$stmt->execute();
53-
54-
// IMPORTANT: Obtaining the video id. Take care, it's done without another query :)
55-
$videoId = $this->connection->lastInsertId();
56-
return $videoId;
57-
}
5841
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace AppBundle\Controller;
44

55
use AppBundle\Application\VideoCreator;
6+
use AppBundle\Repository\MySqlVideoRepository;
67
use Symfony\Component\HttpFoundation\Request;
78

89
/**
@@ -22,7 +23,7 @@ public function postVideoAction(Request $request)
2223
$url = $request->get('url');
2324
$courseId = $request->get('course_id');
2425
$connection = $this->getDoctrine()->getConnection();
25-
$videoCreator = new VideoCreator($connection);
26+
$videoCreator = new VideoCreator(new MySqlVideoRepository($connection));
2627

2728
list($title, $videoId) = $videoCreator->createVideo($title, $url, $courseId);
2829

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Repository;
6+
7+
use Doctrine\DBAL\Connection;
8+
9+
final class MySqlVideoRepository
10+
{
11+
/**
12+
* @var Connection
13+
*/
14+
private $connection;
15+
16+
public function __construct(Connection $connection)
17+
{
18+
$this->connection = $connection;
19+
}
20+
21+
public function save(string $title, $url, $courseId): string
22+
{
23+
$sql = "INSERT INTO video (title, url, course_id)
24+
VALUES (\"{$title}\",
25+
\"{$url}\",
26+
{$courseId}
27+
)";
28+
29+
// Prepare doctrine statement
30+
$stmt = $this->connection->prepare($sql);
31+
$stmt->execute();
32+
33+
// IMPORTANT: Obtaining the video id. Take care, it's done without another query :)
34+
return $this->connection->lastInsertId();
35+
}
36+
}

0 commit comments

Comments
 (0)