Issues (219)

Branch: 4-cactus

BEdita/Core/src/Shell/Task/CheckApiKeyTask.php (1 issue)

1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2017 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
14
namespace BEdita\Core\Shell\Task;
15
16
use BEdita\Core\Model\Table\ApplicationsTable;
17
use Cake\Console\ConsoleOptionParser;
18
use Cake\Console\Shell;
19
use Cake\Datasource\Exception\RecordNotFoundException;
20
21
/**
22
 * Task to setup API key.
23
 *
24
 * @since 4.0.0
25
 * @property \BEdita\Core\Model\Table\ApplicationsTable $Applications
26
 */
27
class CheckApiKeyTask extends Shell
28
{
29
    /**
30
     * {@inheritDoc}
31
     *
32
     * @codeCoverageIgnore
33
     */
34
    public function getOptionParser(): ConsoleOptionParser
35
    {
36
        $parser = parent::getOptionParser();
37
        $parser
38
            ->setDescription([
39
                'Setup API key.',
40
            ]);
41
42
        return $parser;
43
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48
    public function main()
49
    {
50
        $this->loadModel('Applications');
51
52
        try {
53
            $this->verbose('=====> Loading default application... ', 0);
54
            $application = $this->Applications->get(ApplicationsTable::DEFAULT_APPLICATION);
55
            $this->verbose('<info>DONE</info>');
56
        } catch (RecordNotFoundException $e) {
57
            $this->verbose('<error>FAIL</error>');
58
            $this->abort('Default application is missing, please check your installation');
59
        }
60
61
        if (empty($application->api_key)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $application does not seem to be defined for all execution paths leading up to this point.
Loading history...
62
            $this->out('=====> <warning>Default application has no API key</warning>');
63
64
            return false;
65
        }
66
67
        $this->out(sprintf('=====> Default API key is: <info>%s</info>', $application->api_key));
68
        $this->out('=====> <success>API key is ok. You can now make your requests even more handsome with it!</success>');
69
70
        return true;
71
    }
72
}
73