Completed
Push — master ( fe0371...1946fd )
by Simon
04:01
created

Command::loadApiCredentials()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 5
Bugs 1 Features 0
Metric Value
cc 1
eloc 7
c 5
b 1
f 0
nc 1
nop 2
dl 0
loc 11
rs 9.4285
ccs 8
cts 8
cp 1
crap 1
1
<?php namespace Simondubois\UnsplashDownloader;
2
3
use Phar;
4
use Symfony\Component\Console\Command\Command as SymfonyCommand;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
/**
10
 * A command to check parameters validity and call a download task. Steps are :
11
 *  - check option validity (destination, count, history, featured, categories, category).
12
 *  - create a task (to deal with Unsplash API).
13
 *  - execute the task.
14
 */
15
class Command extends SymfonyCommand
16
{
17
18
    //
19
    // Constants & Attributes
20
    //
21
    const DESCRIPTION_DESTINATION = 'Directory where to download photos.';
22
    const DESCRIPTION_QUANTITY = 'Number of photos to download.';
23
    const DESCRIPTION_HISTORY = 'Filename to use as download history.
24
                When photos are downloaded, their IDs will be stored into the file.
25
                Then any further download is going to ignore photos that have their ID in the history.
26
                Usefull to delete unwanted pictures and prevent the CLI to download them again.';
27
    const DESCRIPTION_FEATURED = 'Download only featured photos (incompatible with --category).';
28
    const DESCRIPTION_CATEGORIES = 'Print out categories and quit (no download).';
29
    const DESCRIPTION_CATEGORY = 'Only download photos for the given category ID (incompatible with the --featured).';
30
    /**
31
     * Output instance.
32
     * Stored to simplify method calls and to be used in callbacks.
33
     * @var OutputInterface
34
     */
35
    public $output;
36
37
38
39
    //
40
    // Helpers
41
    //
42
43
    /**
44
     * Output text only if run with verbose attribute
45
     * @param  string  $message Text to output
46
     * @param  string|null $context Context of the message
47
     * @param  int $type Symfony output type
48
     */
49 2
    public function verboseOutput($message, $context = null, $type = OutputInterface::OUTPUT_NORMAL) {
50 2
        if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_VERBOSE) {
51 1
            return;
52
        }
53
54 2
        if (is_string($context)) {
55 1
            $append = '';
56
57 1
            if (substr($message, -1) === PHP_EOL) {
58 1
                $message = substr($message, 0, -1);
59 1
                $append = PHP_EOL;
60 1
            }
61
62 1
            $message = sprintf('<%s>%s</%s>%s', $context, $message, $context, $append);
63 1
        }
64
65 2
        $this->output->write($message, false, $type);
66 2
    }
67
68
69
70
    /**
71
     * Output text only if run with verbose attribute
72
     * @param  string  $message Text to output
73
     * @param  string|null $context Context of the message
74
     * @param  int $type Symfony output type
75
     */
76 1
    public function output($message, $context = null, $type = OutputInterface::OUTPUT_NORMAL) {
77 1
        if ($this->output->getVerbosity() < OutputInterface::VERBOSITY_NORMAL) {
78 1
            return;
79
        }
80
81 1
        if (is_string($context)) {
82 1
            $append = '';
83
84 1
            if (substr($message, -1) === PHP_EOL) {
85 1
                $message = substr($message, 0, -1);
86 1
                $append = PHP_EOL;
87 1
            }
88
89 1
            $message = sprintf('<%s>%s</%s>%s', $context, $message, $context, $append);
90 1
        }
91
92 1
        $this->output->write($message, false, $type);
93 1
    }
94
95
96
97
    //
98
    // Handle command setup
99
    //
100
101
    /**
102
     * Configure the Symfony command
103
     */
104 6
    protected function configure()
105
    {
106 6
        $this->setName('unsplash-downloader');
107 6
        $this->setDescription('Download unsplash photos');
108 6
        $this->configureOptions();
109 6
    }
110
111
    /**
112
     * Set command options
113
     */
114 6
    private function configureOptions()
115
    {
116 6
        $this->addOption('destination', null, InputOption::VALUE_REQUIRED, self::DESCRIPTION_DESTINATION, getcwd());
117 6
        $this->addOption('quantity', null, InputOption::VALUE_REQUIRED, self::DESCRIPTION_QUANTITY, '10');
118 6
        $this->addOption('history', null, InputOption::VALUE_REQUIRED, self::DESCRIPTION_HISTORY);
119 6
        $this->addOption('featured', null, InputOption::VALUE_NONE, self::DESCRIPTION_FEATURED);
120 6
        $this->addOption('categories', null, InputOption::VALUE_NONE, self::DESCRIPTION_CATEGORIES);
121 6
        $this->addOption('category', null, InputOption::VALUE_REQUIRED, self::DESCRIPTION_CATEGORY);
122 6
    }
123
124
125
126
    //
127
    // Handle command operations
128
    //
129
130
    /**
131
     * Process the download based on provided options
132
     * @param  InputInterface  $input  Command input
133
     * @param  OutputInterface $output Command output
134
     */
135 2
    public function execute(InputInterface $input, OutputInterface $output)
136
    {
137 2
        $this->output = $output;
138
139 2
        $validate = new Validate();
140
141 2
        $task = $this->task();
142
143 2
        if ($input->getOption('categories')) {
144 1
            $task->categories();
145 1
            return;
146
        }
147
148 1
        $this->parameters($validate, $task, $input->getOptions());
149 1
        $task->download();
150 1
    }
151
152
    /**
153
     * Instantiate a new task
154
     * @return Task Task instance
155
     */
156 1
    public function task() {
157 1
        $task = new Task();
158 1
        $task->setNotificationCallback([$this, 'output']);
159
160 1
        return $task;
161
    }
162
163
    /**
164
     * Check & validate the parameters
165
     * @param  Validate $validate Validate instance
166
     * @param  Task $task Download task
167
     * @param  array $options Command options
168
     */
169 1
    public function parameters(Validate $validate, Task $task, $options)
170
    {
171 1
        $this->destinationParameter($validate, $task, $options['destination']);
172 1
        $this->quantityParameter($validate, $task, $options['quantity']);
173 1
        $this->historyParameter($validate, $task, $options['history']);
174 1
        $this->featuredParameter($task, $options['featured']);
175 1
        $this->categoryParameter($validate, $task, $options['category']);
176 1
    }
177
178
    /**
179
     * Check & validate the destination parameter
180
     * @param  Validate $validate Validate instance
181
     * @param  Task $task Download task
182
     * @param  string $option Option value
183
     */
184 1
    public function destinationParameter(Validate $validate, Task $task, $option)
185
    {
186 1
        $destination = $validate->destination($option);
187
188 1
        $task->setDestination($destination);
189
190 1
        $this->verboseOutput('Download photos to '.$destination.'.'.PHP_EOL);
191 1
    }
192
193
    /**
194
     * Check & validate the quantity parameter
195
     * @param  Validate $validate Validate instance
196
     * @param  Task $task Download task
197
     * @param  string $option Option value
198
     */
199 1
    public function quantityParameter(Validate $validate, Task $task, $option)
200
    {
201 1
        $quantity = $validate->quantity($option);
202
203 1
        $task->setQuantity($quantity);
204
205 1
        $this->verboseOutput('Download the last '.$quantity.' photos.'.PHP_EOL);
206 1
    }
207
208
    /**
209
     * Check & validate the history parameter
210
     * @param  Validate $validate Validate instance
211
     * @param  Task $task Download task
212
     * @param  string $option Option value
213
     */
214 1
    public function historyParameter(Validate $validate, Task $task, $option)
215
    {
216 1
        $history = $validate->history($option);
217
218 1
        $task->setHistory($history);
219
220 1
        if (is_null($history)) {
221 1
            $this->verboseOutput('Do not use history.'.PHP_EOL);
222 1
            return;
223
        }
224
225 1
        $this->verboseOutput('Use '.$history.' as history.'.PHP_EOL);
226 1
    }
227
228
    /**
229
     * Check & validate the featured parameter
230
     * @param  Task $task Download task
231
     * @param  bool $option Option value
232
     */
233 1
    public function featuredParameter(Task $task, $option)
234
    {
235 1
        $task->setFeatured($option);
236
237 1
        if ($option === true) {
238 1
            $this->verboseOutput('Download only featured photos.');
239 1
        }
240
241 1
        $this->verboseOutput('Download featured and not featured photos.');
242 1
    }
243
244
    /**
245
     * Check & validate the category parameter
246
     * @param  Validate $validate Validate instance
247
     * @param  Task $task Download task
248
     * @param  string $option Option value
249
     */
250 1
    public function categoryParameter(Validate $validate, Task $task, $option)
251
    {
252 1
        $category = $validate->category($option);
253
254 1
        $task->setCategory($category);
255
256 1
        if (is_int($category) && $task->getFeatured() !== true) {
257 1
            $this->verboseOutput('Download only photos for category ID '.$option.'.'.PHP_EOL);
258 1
        }
259 1
    }
260
261
}
262