Skip to content

Commit 1c8b15d

Browse files
committed
feat(legacy-create-video): [golden-master] constructor inyection
1 parent 5b50614 commit 1c8b15d

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@
44

55
namespace AppBundle\Application;
66

7+
use Doctrine\DBAL\Connection;
8+
79
final class VideoCreator
810
{
9-
public function createVideo($title, $url, $courseId, object $connection): array
11+
/** @var Connection */
12+
private $connection;
13+
14+
public function __construct($connection)
15+
{
16+
$this->connection = $connection;
17+
}
18+
19+
public function createVideo($title, $url, $courseId): array
1020
{
1121
$title = $this->sanitizeTitle($title);
1222

@@ -17,11 +27,11 @@ public function createVideo($title, $url, $courseId, object $connection): array
1727
)";
1828

1929
// Prepare doctrine statement
20-
$stmt = $connection->prepare($sql);
30+
$stmt = $this->connection->prepare($sql);
2131
$stmt->execute();
2232

2333
// IMPORTANT: Obtaining the video id. Take care, it's done without another query :)
24-
$videoId = $connection->lastInsertId();
34+
$videoId = $this->connection->lastInsertId();
2535
return array($title, $videoId);
2636
}
2737

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@
1111
*/
1212
class VideoController extends BaseController
1313
{
14-
/** @var VideoCreator */
15-
private $videoCreator;
16-
17-
public function __construct()
18-
{
19-
$this->videoCreator = new VideoCreator();
20-
}
21-
2214
/**
2315
* Method used to create a new video
2416
* @todo Validate the request
@@ -30,8 +22,9 @@ public function postVideoAction(Request $request)
3022
$url = $request->get('url');
3123
$courseId = $request->get('course_id');
3224
$connection = $this->getDoctrine()->getConnection();
25+
$videoCreator = new VideoCreator($connection);
3326

34-
list($title, $videoId) = $this->videoCreator->createVideo($title, $url, $courseId, $connection);
27+
list($title, $videoId) = $videoCreator->createVideo($title, $url, $courseId);
3528

3629
// And we return the video created
3730
return [

0 commit comments

Comments
 (0)