Completed
Push — feature/test-commands ( c468f4 )
by Oguzhan
03:07
created

SearchSpotifyTrackCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 2
cbo 6
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
B execute() 0 28 4
A setSpotifyService() 0 4 1
1
<?php
2
/*******************************************************************************
3
 * This file is part of the Pbxg33k\MusicInfo package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * (c) 2017 Oguzhan uysal. All rights reserved
9
 ******************************************************************************/
10
namespace Pbxg33k\MusicInfo\Command;
11
12
use Pbxg33k\MusicInfo\Model\Artist;
13
use Pbxg33k\MusicInfo\Service\Spotify\Service;
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Helper\Table;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
class SearchSpotifyTrackCommand extends Command
22
{
23
    /**
24
     * @var Service
25
     */
26
    protected $spotifyService;
27
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('music-info:search:spotify:artist')
32
            ->setDescription('Search an artist on Spotify')
33
            ->setHelp("This command will search and return an artist using the Spotify API")
34
            ->addOption('save', 's', InputOption::VALUE_OPTIONAL)
35
            ->addArgument('query', InputArgument::REQUIRED, 'Artist name', false)
36
            ;
37
    }
38
39
    protected function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $artist = $input->getArgument('artist');
42
        if(!$artist) {
43
            throw new \InvalidArgumentException('Artist missing');
44
        }
45
46
        $result = $this->spotifyService->artist()->getByName($artist);
47
48
        $table = new Table($output);
49
        $table
50
            ->setHeaders(['ID','Artist']);
51
52
        if(!$result) {
53
            $output->writeln('No results');
54
        } else {
55
            /** @var Artist $artist */
56
            foreach($result as $artist) {
57
                $table
58
                    ->addRow([
59
                        $artist->getId(),
60
                        $artist->getName()
61
                    ]);
62
            }
63
64
            $table->render();
65
        }
66
    }
67
68
    public function setSpotifyService(Service $service)
69
    {
70
        $this->spotifyService = $service;
71
    }
72
}