Skip to content

Commit

Permalink
Add album search command
Browse files Browse the repository at this point in the history
  • Loading branch information
Oguzhan Uysal committed Apr 2, 2017
1 parent 6764051 commit acb535b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
3 changes: 2 additions & 1 deletion musicinfo
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use Symfony\Component\Console\Application;
$app = new Application('PHPMusicInfo', '0.1.0');
$app->addCommands([
new \Pbxg33k\MusicInfo\Command\SearchArtistCommand(),
new \Pbxg33k\MusicInfo\Command\SearchTrackCommand()
new \Pbxg33k\MusicInfo\Command\SearchTrackCommand(),
new \Pbxg33k\MusicInfo\Command\SearchAlbumCommand()
]);
$app->run();
55 changes: 55 additions & 0 deletions src/Command/SearchAlbumCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Created by PhpStorm.
* User: oguzu
* Date: 1-4-2017
* Time: 16:07
*/

namespace Pbxg33k\MusicInfo\Command;


use Pbxg33k\MusicInfo\MusicInfo;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SearchAlbumCommand extends BaseCommand
{
const COMMAND_NAME = 'search:album';

const COMMAND_DESCRIPTION = 'Search an album on one or more services';

/**
* @var MusicInfo
*/
protected $musicInfo;

protected function configure()
{
$this
->addArgument('album', InputArgument::REQUIRED, 'Album name')
->addArgument('service', InputArgument::OPTIONAL, 'Service to use, default to all enabled');

parent::configure();
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$service = ($input->getArgument('service') != '') ? $input->getArgument('service') : null;

$searchResult = $this->musicInfo->doSearch($input->getArgument('album'), 'album', $service);

$resultsTable = $this->generateTableForSearchResult($searchResult, [
'id' => 'ID',
'name' => 'Name',
'type' => 'Type',
'image' => 'Image',
'dataSource' => 'Source'
], new Table($output));

$resultsTable->render();

}
}
2 changes: 1 addition & 1 deletion src/Service/Spotify/Endpoint/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,6 @@ public function getByName($name)
*/
public function getByGuid($guid)
{
return $this->getParent()->getApiClient()->getAlbum($guid);
return $this->transform($this->getParent()->getApiClient()->getAlbum($guid));
}
}
42 changes: 39 additions & 3 deletions src/Service/VocaDB/Endpoint/Album.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@

namespace Pbxg33k\MusicInfo\Service\VocaDB\Endpoint;

use Doctrine\Common\Collections\ArrayCollection;
use Pbxg33k\MusicInfo\Model\Artist as ArtistModel;
use Pbxg33k\MusicInfo\Model\IMusicServiceEndpoint;
use Pbxg33k\VocaDB\Album as AlbumEndpoint;
use Pbxg33k\MusicInfo\Model\Album as AlbumModel;
use Pbxg33k\VocaDB\Models\Collections\AlbumCollection;
use Pbxg33k\VocaDB\Models\Album as VocaDBModel;

class Album extends AlbumEndpoint implements IMusicServiceEndpoint
{
Expand Down Expand Up @@ -50,18 +55,49 @@ public function setParent($apiService)
// TODO: Implement setApiService() method.
}

/**
* @param VocaDBModel $raw
* @return AlbumModel
*/
public function transformSingle($raw)
{
// TODO: Implement transformSingle() method.
$object = new AlbumModel();
$object
->setId($raw->getId())
->setName($raw->getName())
->setArtists(new ArrayCollection([
(new ArtistModel())->setName($raw->getName())
]))
->setType($raw->getDiscType())
->setImage($raw->getMainPicture()->urlThumb)
->setReleaseDate(new \DateTime($raw->getReleaseDate()->formatted))
->setDataSource('vocadb')
->setRawData($raw);

return $object;
}

public function transformCollection($raw)
{
// TODO: Implement transformCollection() method.
$collection = new ArrayCollection();
if($raw instanceof AlbumCollection) {
foreach($raw->collection as $album) {
$collection->add($this->transformSingle($album));
}

return $collection;
}

throw new \Exception('Transform failed. Object is not an instance of AlbumCollection');
}

public function transform($raw)
{
// TODO: Implement transform() method.
return $this->transformCollection($raw);
}

public function getByName($name, $complete = true)
{
return $this->transform(parent::getByName($name, $complete));
}
}

0 comments on commit acb535b

Please sign in to comment.