Last active
January 27, 2017 11:31
-
-
Save yourwebmaker/688dc9fb4e932b8300ff to your computer and use it in GitHub Desktop.
Exemplo mostrando desacoplamento de camadas e de framework
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 Domain\Entidades; | |
/** | |
* Representa uma música no sistema. | |
* Não importa de onde ela venha: Banco, Cache, WS, FileSystem | |
*/ | |
class Musica | |
{ | |
protected $id; | |
protected $nome; | |
protected $artista; | |
protected $hits; | |
function __construct($id, $artista, $nome, $hits) | |
{ | |
$this->id = $id; | |
$this->artista = $artista; | |
$this->nome = $nome; | |
$this->hits = $hits; | |
} | |
} | |
/** | |
* Representa um artista no sistema. | |
* Não importa de onde ela venha: Banco, Cache, WS, FileSystem | |
*/ | |
class Artista | |
{ | |
protected $id; | |
protected $nome; | |
protected $hits; | |
function __construct($id, $artista, $hits) | |
{ | |
$this->id = $id; | |
$this->artista = $artista; | |
$this->hits = $hits; | |
} | |
} |
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 | |
use Phalcon\Mvc\Micro\Collection as MicroCollection; | |
$app = new Phalcon\Mvc\Micro(); | |
$app->setDi(require 'Injecao-de-dependencia.php'); | |
$musicas = new MicroCollection(); | |
$musicas->get('/musicas/{idArtista}', 'listarAction'); | |
$app->mount($musicas); | |
$app->handle(); |
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 Infra\Repositories; | |
/** | |
* Implementacao de armazenamento e busca de musicas | |
*/ | |
class MusicaDbRepository implements MusicaRepositoryInterface | |
{ | |
// instancia do banco de dados | |
protected $db; | |
// cara que converte array para objeto | |
protected $factory; | |
function __construct(DatabaseConnection $db, Factory $factory) | |
{ | |
$this->db = $db; | |
$this->factory = $factory; | |
} | |
/** | |
* Retorna lista de músicas mais acessadas | |
*/ | |
function buscarMaisAcessadas($quantidade = 1000) | |
{ | |
$sql = "SELECT * FROM musicas ORDER BY hits DESC LIMIT {$quantidade}"; | |
$result = $this->db->fetch($sql); | |
return $this->factory->createFromResult($result); | |
} | |
/** | |
* Retorna lista de músicas mais acessadas por artista | |
*/ | |
function buscarMaisAcessadasPorArtista(Artista $artista) | |
{ | |
$sql = "SELECT * FROM musicas WHERE id_artista = {$artista->getId()} ORDER BY hits "; | |
$result = $this->db->fetch($sql); | |
return $this->factory->createFromResult($result); | |
} | |
/** | |
* Sava uma música no sistema | |
*/ | |
function save(Musica $musica) | |
{ | |
// insere no banco | |
// whatever | |
$sql = "INSERT INTO..."; | |
return $this->db->query($sql)? $musica : false; | |
} | |
} | |
/** | |
* Implementacao de armazenamento e busca de musicas (Doctrine) | |
*/ | |
class MusicaDoctrineRepository extends Doctrine\ORM\EntityRepository implements MusicaRepositoryInterface | |
{ | |
/** | |
* Retorna lista de músicas mais acessadas | |
*/ | |
function buscarMaisAcessadas($quantidade = 1000) | |
{ | |
$q = $this->em->createQuery("select m from Domain\Musucas m "); | |
$q->setMaxResults($quantidade); | |
return $q->getResult(); | |
} | |
/** | |
* Retorna lista de músicas mais acessadas por artista | |
*/ | |
function buscarMaisAcessadasPorArtista(Artista $artista) | |
{ | |
$dql = "select m from Domain\Musucas m where m.artista = {$artista}"; | |
$q = $this->em->createQuery($dql); | |
return $q->getResult(); | |
} | |
/** | |
* Sava uma música no sistema | |
*/ | |
function save(Musica $musica) | |
{ | |
$this->em->persist($musica); | |
$this->em->flush(); | |
return $musica; | |
} | |
} |
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 | |
//configurando as dependencias do meu projeto | |
$di = new Phalcon\DI(); | |
$db = new \DbConnection('mysql:daniel:123456@localhost:foo'); | |
$factory = new TransformadorDeArrayParaObjeto; | |
$repository = new MusicaDbRepository($db, $factory); // Ou Repositorio do Doctrine, ou qualquer outra coisa | |
$di->set('musica.service', new MusicaService($repository)); | |
$di->set('artista.repository', new ArtistaRepository); | |
return $di; |
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 Domain\Repositories; | |
/** | |
* Representa o armazenamento de músicas | |
*/ | |
interface MusicaRepositoryInterface | |
{ | |
/** | |
* Retorna lista de músicas mais acessadas | |
*/ | |
function buscarMaisAcessadas($quantidade = 1000); | |
/** | |
* Retorna lista de músicas mais acessadas por artista | |
*/ | |
function buscarMaisAcessadasPorArtista(Artista $artista); | |
/** | |
* Sava uma música no sistema | |
*/ | |
function save(Musica $musica); | |
} |
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 Application\Controllers; | |
/** | |
* Somente mapeia a requisição do usuário, escolhe qual dominio usar e o executa | |
*/ | |
class MusicaController extends \Phalcon\Mvc\Controller | |
{ | |
protected $service; | |
// ODEIO ESSE CARA!!!! | |
function onConstruct() | |
{ | |
$service = $this->di->get('musica.service'); | |
$artistaRepository = $this->di->get('artista.repository'); | |
} | |
function listarAction($idArista) | |
{ | |
$artista = $this->artistaRepository->find($idArista); | |
$this->view->musicas = $this->service->buscarMaisAcessadasPorArtista($artista); | |
} | |
} |
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 Domain\Services; | |
/** | |
* Representa todas as operações feitas no dominio de música | |
* que não podem ser feitas por uma uma entidade em sua responsabilidade natural | |
*/ | |
class MusicaService | |
{ | |
protected $repository; | |
/** | |
* Injeto O repositório.... não preciso saber de onde vem os objetos | |
*/ | |
function __construct(MusicaRepositoryInterface $repository) | |
{ | |
$this->repository = $repository; | |
} | |
function buscarMaisAcessadas($quantidade = 1000) | |
{ | |
return $this->repository->buscarMaisAcessadas($quantidade); | |
} | |
function buscarMaisAcessadasPorArtista(Artista $artista) | |
{ | |
return $this->repository->buscarMaisAcessadasPorArtista($artista); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment