1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/******************************************************************************* |
4
|
|
|
* This file is part of the Pbxg33k\MusicInfo package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
* |
9
|
|
|
* (c) 2017 Oguzhan uysal. All rights reserved |
10
|
|
|
******************************************************************************/ |
11
|
|
|
namespace Pbxg33k\MusicInfo\Command; |
12
|
|
|
|
13
|
|
|
use Pbxg33k\MusicInfo\Model\Artist; |
14
|
|
|
use Pbxg33k\MusicInfo\Service\Spotify\Service; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Helper\Table; |
17
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
View Code Duplication |
class SearchSpotifyArtistCommand extends Command |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Service |
26
|
|
|
*/ |
27
|
|
|
protected $spotifyService; |
28
|
|
|
|
29
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this |
32
|
|
|
->setName('music-info:search:spotify:artist') |
33
|
|
|
->setDescription('Search an artist on Spotify') |
34
|
|
|
->setHelp("This command will search and return an artist using the Spotify API") |
35
|
|
|
->addOption('save', 'p', InputOption::VALUE_OPTIONAL, false) |
|
|
|
|
36
|
|
|
->addArgument('query', InputArgument::REQUIRED, 'Artist name') |
37
|
|
|
; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
41
|
|
|
{ |
42
|
|
|
$artist = $input->getArgument('query'); |
43
|
|
|
if(!$artist) { |
44
|
|
|
throw new \InvalidArgumentException('Artist missing'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$result = $this->spotifyService->artist()->getByName($artist); |
48
|
|
|
|
49
|
|
|
$table = new Table($output); |
50
|
|
|
$table |
51
|
|
|
->setHeaders(['ID','Artist']); |
52
|
|
|
|
53
|
|
|
if(!$result) { |
54
|
|
|
$output->writeln('No results'); |
55
|
|
|
} else { |
56
|
|
|
/** @var Artist $artist */ |
57
|
|
|
foreach($result as $artist) { |
58
|
|
|
$table |
59
|
|
|
->addRow([ |
60
|
|
|
$artist->getId(), |
61
|
|
|
$artist->getName() |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$table->render(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function setSpotifyService(Service $service) |
70
|
|
|
{ |
71
|
|
|
$this->spotifyService = $service; |
72
|
|
|
} |
73
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.