Completed
Push — master ( 9a089f...8f0cf9 )
by Alexey
01:27
created

WowAppsPackagistSearchCommand::execute()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 52
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 8.6868
c 0
b 0
f 0
cc 5
eloc 32
nc 5
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the wow-apps/symfony-packagist project
4
 * https://github.com/wow-apps/symfony-packagist
5
 *
6
 * (c) 2017 WoW-Apps
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WowApps\PackagistBundle\Command;
13
14
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use WowApps\PackagistBundle\Exception\PackagistException;
21
use WowApps\PackagistBundle\Service\Packagist;
22
23
/**
24
 * Class WowAppsPackagistSearchCommand
25
 *
26
 * @author Alexey Samara <[email protected]>
27
 * @package wow-apps/symfony-packagist
28
 */
29
class WowAppsPackagistSearchCommand extends ContainerAwareCommand
30
{
31
    /**
32
     * @return void
33
     */
34
    protected function configure()
35
    {
36
        $this
37
            ->setName('wowapps:packagist:search')
38
            ->setDescription('Search for packages')
39
            ->addArgument('search_query', InputArgument::OPTIONAL, 'Search query')
40
            ->addOption(
41
                'tag',
42
                null,
43
                InputOption::VALUE_OPTIONAL,
44
                'You can set package tag to search'
45
            )
46
            ->addOption(
47
                'type',
48
                null,
49
                InputOption::VALUE_OPTIONAL,
50
                'You can set type of packages to search'
51
            )
52
        ;
53
    }
54
55
    /**
56
     * @param InputInterface $input
57
     * @param OutputInterface $output
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        /** @var Packagist $packagist */
62
        $packagist = $this->getContainer()->get('wowapps.packagist.service');
63
        $searchQuery = strtolower($input->getArgument('search_query'));
64
        $symfonyStyle = new SymfonyStyle($input, $output);
65
66
        echo PHP_EOL;
67
        $output->writeln('<bg=green;options=bold;fg=white>                                               </>');
68
        $output->writeln('<bg=green;options=bold;fg=white>         Symfony Packagist API Bundle          </>');
69
        $output->writeln('<bg=green;options=bold;fg=white>                                               </>');
70
        echo PHP_EOL;
71
72
        if (empty($searchQuery)) {
73
            $symfonyStyle->error([
74
                PackagistException::E_EMPTY_SEARCH_QUERY,
75
                PackagistException::E_EMPTY_SEARCH_QUERY_DESCRIPTION,
76
            ]);
77
78
            return;
79
        }
80
81
        $symfonyStyle->title('Search for packages');
82
83
        $packagesList = $packagist->searchPackages(
84
            $searchQuery,
85
            $input->getOption('tag'),
86
            $input->getOption('type')
87
        );
88
89
        if (!$packagesList->count()) {
90
            $symfonyStyle->warning(PackagistException::W_NO_SEARCH_RESULT);
91
            return;
92
        }
93
94
        $showList = $symfonyStyle->confirm(
95
            sprintf('Founded %d packages. Do you want to show them all?', $packagesList->count()),
96
            false
97
        );
98
99
        if ($showList) {
100
            foreach ($packagesList as $package) {
101
                $output->writeln(
102
                    sprintf(
103
                        '<options=bold;fg=yellow>%s</>%s',
104
                        $package->getName(),
105
                        PHP_EOL . $package->getDescription() . PHP_EOL
106
                    )
107
                );
108
            }
109
        }
110
    }
111
}
112