Last active
August 29, 2015 13:58
-
-
Save l3l0/9949589 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Rd\HelpBundle\Model\Tire\Repository; | |
use Rd\HelpBundle\Model\Tire\Tire; | |
class DbalTireRepository implements TireRepository | |
{ | |
private $connection; | |
public function __construct($connection) | |
{ | |
$this->connection = $connection; | |
} | |
public function getTires() | |
{ | |
$query = "SELECT id, model, mark, size FROM tire ORDER BY mark"; | |
$stmt = $this->connection->prepare($query); | |
$stmt->execute(); | |
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC); | |
$tires = []; | |
foreach ($rows as $row) { | |
$tires[] = $this->buildTire($row); | |
} | |
return $tires; | |
} | |
private function buildTire($row) | |
{ | |
$tire = new Tire($row['model'], $row['mark'], $row['size']); | |
$this->setId($tire, $row['id']); | |
return $tire; | |
} | |
/** | |
* We does not want to change object id during its live time using public API | |
*/ | |
private function setId(Tire $tire, $id) | |
{ | |
$reflection = new \ReflectionObject($tire); | |
$property = $reflection->getProperty('id'); | |
$property->setAccessible(true); | |
$property->setValue($object, $id) | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Rd\HelpBundle\Controller; | |
use Rd\HelpBundle\Model\Tire\Repository\TireRepository; | |
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; | |
/** | |
* Controller as service | |
*/ | |
class SomeController | |
{ | |
private $templating; | |
private $tireRepository; | |
public function __construct(EngineInterface $templating, TireRepository $repository) | |
{ | |
$this->templating = $templating; | |
$this->tireRepository = $repository; | |
} | |
public function tireAction() | |
{ | |
return $this->templating->renderResponse( | |
'RdHelpBundle:Query:test.html.twig', | |
['tires' => $this->tireRepository->getTires()] | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Rd\HelpBundle\Model\Tire; | |
class Tire | |
{ | |
private $id; | |
private $model; | |
private $mark; | |
private $size; | |
public function __constructor($model, $mark, $size) | |
{ | |
$this->model = $model; | |
$this->mark = $mark; | |
$this->size = $size; | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function technicalSpecification() | |
{ | |
return [ | |
'model' => $this->model, | |
'mark' => $this->mark, | |
'size' => $this->size | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Rd\HelpBundle\Model\Tire\Repository; | |
interface TireRepository | |
{ | |
public function getTires(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment