Created
December 20, 2017 20:31
-
-
Save zabaala/538d79fd5231f073e81274ba43af9108 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 App; | |
class CacheProductRepository extends ProductInterface | |
{ | |
public function getAll() | |
{ | |
// Get all products from redis... | |
} | |
} |
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 App; | |
class DatabaseProductRepository implements ProductInterface | |
{ | |
public function getAll() | |
{ | |
// Get all products from database. | |
} | |
} |
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 App/Http/Controllers; | |
class ProductController extends Controller | |
{ | |
// Aqui voce pode recuperar os registros direto do banco de dados, considerando que | |
// o bind que a aplicação está fazendo de ProductInterface irá retornar uma instância de | |
// DatabaseProductRepository. | |
// | |
// Mas, se você optar por recuperar os registros do cache, basta que seu bind retorne uma instância | |
// de CacheProductRepository. | |
// | |
// Com isso, o papel do contrato é garantir que a assinatura do método é a mesma. Ou seja, as classes | |
// que assinarem o contrato ProductInterface, possuirão o método getAll() com a mesma assinatura. | |
// | |
public function index(ProductInterface $products) | |
{ | |
return $products->getAll(); | |
} | |
} |
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 App; | |
interface ProductInterface | |
{ | |
/** | |
* @return Collection | |
*/ | |
public function getAll(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment