UseCommand::getTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 CL\ComposerInit\Prompt\Prompts;
10
use GuzzleHttp\Client;
11
12
/**
13
 * @author    Ivan Kerin <[email protected]>
14
 * @copyright (c) 2014 Clippings Ltd.
15
 * @license   http://spdx.org/licenses/BSD-3-Clause
16
 */
17
class UseCommand extends Command
18
{
19
    /**
20
     * @var Client
21
     */
22
    private $packagist;
23
24
    /**
25
     * @var Template
26
     */
27
    private $template;
28
29
    /**
30
     * @var Prompts
31
     */
32
    private $prompts;
33
34
    /**
35
     * @param Template $template
36
     * @param Prompts $prompts
37
     * @param Client $packagist
38
     */
39 1
    public function __construct(Template $template, Prompts $prompts, Client $packagist)
40
    {
41 1
        parent::__construct();
42
43 1
        $this->template = $template;
44 1
        $this->prompts = $prompts;
45 1
        $this->packagist = $packagist;
46 1
    }
47
48
    /**
49
     * @return Client
50
     */
51 1
    public function getPackagist()
52
    {
53 1
        return $this->packagist;
54
    }
55
56
    /**
57
     * @return Template
58
     */
59 1
    public function getTemplate()
60
    {
61 1
        return $this->template;
62
    }
63
64
    /**
65
     * @return Prompts
66
     */
67 1
    public function getPrompts()
68
    {
69 1
        return $this->prompts;
70
    }
71
72 1
    protected function configure()
73
    {
74 1
        $this
75 1
            ->setName('use')
76 1
            ->setDescription('List available composer init templates')
77 1
            ->addArgument(
78 1
                'package',
79 1
                InputArgument::REQUIRED,
80
                'Package Name'
81 1
            );
82 1
    }
83
84 1
    protected function execute(InputInterface $input, OutputInterface $output)
85
    {
86 1
        $packageName = $input->getArgument('package');
87 1
        $dialog = $this->getHelperSet()->get('dialog');
88
89 1
        $this->template->open($this->getPackageZipUrl($packageName));
90
91 1
        $output->writeln('Enter Template variables (Press enter for default):');
92
93 1
        $this->template->setValues(
94 1
            $this->prompts->getValues(
95 1
                $this->template->getPromptNames(),
96 1
                $output,
97
                $dialog
98 1
            )
99 1
        );
100
101 1
        $valuesDisplay = "Use These Variables:\n";
102
103 1
        foreach ($this->template->getValues() as $key => $value) {
104 1
            $valuesDisplay .= "  <info>$key</info>: $value\n";
105 1
        }
106
107 1
        $valuesDisplay .= "Confirm? <comment>(Y/n)</comment>:";
108
109 1
        if ($dialog->askConfirmation($output, $valuesDisplay, 'y')) {
110 1
            $this->template->putInto(getcwd());
111 1
            $output->writeln('Done');
112 1
        } else {
113 1
            $output->writeln('<error>Aborted</error>');
114
        }
115 1
    }
116
117
    /**
118
     * @param string $packageName
119
     */
120 1
    public function getPackageZipUrl($packageName)
121
    {
122 1
        $response = $this->packagist->get("/packages/{$packageName}.json");
123 1
        $package = json_decode($response->getBody(), true);
124
125 1
        return $package['package']['versions']['dev-master']['dist']['url'];
126
    }
127
}
128