SearchCommand::filterWith()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace CL\ComposerInit;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use GuzzleHttp\Client;
10
11
/**
12
 * @author    Ivan Kerin <[email protected]>
13
 * @copyright (c) 2014 Clippings Ltd.
14
 * @license   http://spdx.org/licenses/BSD-3-Clause
15
 */
16
class SearchCommand extends Command
17
{
18
    /**
19
     * @var Client
20
     */
21
    private $packagist;
22
23
    /**
24
     * @param Client $packagist
25
     */
26 1
    public function __construct(Client $packagist)
27
    {
28 1
        parent::__construct();
29
30 1
        $this->packagist = $packagist;
31 1
    }
32
33
    /**
34
     * @return Client
35
     */
36 1
    public function getPackagist()
37
    {
38 1
        return $this->packagist;
39
    }
40
41 1
    protected function configure()
42
    {
43 1
        $this
44 1
            ->setName('search')
45 1
            ->setDescription('Search available composer init templates')
46 1
            ->addArgument(
47 1
                'filter',
48 1
                InputArgument::OPTIONAL,
49
                'Filter by name'
50 1
            );
51
        ;
52 1
    }
53
54
    /**
55
     * @return array
56
     */
57 1
    public function getTemplates()
58
    {
59 1
        $response = $this->packagist->get(
60 1
            '/packages/list.json',
61 1
            ['query' => ['type' => 'composer-init-template']]
62 1
        );
63
64 1
        $list = json_decode($response->getBody(), true);
65
66 1
        return (array) $list['packageNames'];
67
    }
68
69
    /**
70
     * @param  array  $array
71
     * @param  string $filter
72
     * @return array
73
     */
74
    public function filterWith(array $array, $filter)
75
    {
76 1
        return array_filter($array, function ($name) use ($filter) {
77 1
            return false !== strpos($name, $filter);
78 1
        });
79
    }
80
81
    /**
82
     * @param  InputInterface  $input
83
     * @param  OutputInterface $output
84
     */
85 1
    protected function execute(InputInterface $input, OutputInterface $output)
86
    {
87 1
        $templates = $this->getTemplates();
88
89 1
        $filter = $input->getArgument('filter');
90
91 1
        if ($filter) {
92 1
            $templates = $this->filterWith($templates, $filter);
93 1
        }
94
95 1
        if (empty($templates)) {
96 1
            $output->writeln("<error>No templates found</error>");
97 1
        } else {
98 1
            $output->writeln('Available Init Templates:');
99
100 1
            foreach ($templates as $package) {
101 1
                $output->writeln("  <info>{$package}</info>");
102 1
            }
103
        }
104 1
    }
105
}
106