LTIRegisterConsumer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 23 1
C execute() 0 48 7
1
<?php
2
3
namespace AppBundle\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use IMSGlobal\LTI\ToolProvider;
10
use IMSGlobal\LTI\ToolProvider\DataConnector;
11
use Symfony\Component\Console\Question\ConfirmationQuestion;
12
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
13
14
15
class LTIRegisterConsumer extends Command
16
{
17
    protected function configure()
18
    {
19
        $this
20
            ->setName('app:lti-register-consumer')
21
            ->setDescription('register a new lti consumer tool.')
22
            ->setHelp('This command registers a new lti consumer tool')
23
            ->addOption(
24
                'consumerKey',
25
                null,
26
                InputOption::VALUE_REQUIRED,
27
                'What is the consumer key (eg: \'testing.edu\')?'
28
            )
29
            ->addOption(
30
                'consumerName',
31
                null,
32
                InputOption::VALUE_REQUIRED,
33
                'What is the consumer name (eg: \Testing\')?'
34
            )
35
            ->addOption(
36
                'consumerSecret',
37
                null,
38
                InputOption::VALUE_REQUIRED,
39
                'What is the consumer secret (eg: \'ThisIsASecret!\')?'
40
            );
41
    }
42
43
    protected function execute(InputInterface $input, OutputInterface $output)
44
    {
45
        $output->writeln([
46
            'Register a lti consumer tool',
47
            '============',
48
            '',
49
        ]);
50
51
        if (!$consumerKey = $input->getOption('consumerKey')) {
52
            throw new MissingMandatoryParametersException('consumerKey');
53
        }
54
        if (!$consumerName = $input->getOption('consumerName')) {
55
            throw new MissingMandatoryParametersException('consumerName');
56
        }
57
        if (!$consumerSecret = $input->getOption('consumerSecret')) {
58
            throw new MissingMandatoryParametersException('consumerSecret');
59
        }
60
        $output->writeln([
61
            'You are about to register a new LTI consumer with the following parameters:',
62
            'consumerKey: ' . $consumerKey,
63
            'consumerName: ' . $consumerName,
64
            'consumerSecret: ' . $consumerSecret,
65
        ]);
66
        $helper = $this->getHelper('question');
67
        $question = new ConfirmationQuestion('It that ok (y/n)?', false);
68
        $output->writeln('');
69
70
        if(!$input->getOption('no-interaction')){
71
            if (!$helper->ask($input, $output, $question)) {
72
                return;
73
            }
74
        }
75
        $dsn = sprintf('%s:host=%s;dbname=%s', getenv('DB_TYPE'), getenv('DB_HOST'), getenv('DB_NAME'));
76
        $db = new \PDO($dsn, getenv('DB_USER'), getenv('DB_PWD'));
77
        $dataConnector = DataConnector\DataConnector::getDataConnector('', $db);
78
        $consumer = new ToolProvider\ToolConsumer($consumerKey, $dataConnector);
79
        $consumer->name = $consumerName;
80
        $consumer->secret = $consumerSecret;
81
        $consumer->enabled = TRUE;
82
        $consumer->save();
83
        $sth = $db->prepare('SELECT * FROM lti2_consumer WHERE consumer_key256=:consumerKey');
84
        $sth->execute(['consumerKey' => $consumerKey]);
85
        $result = $sth->fetchAll();
86
        dump($result);
87
        if (!count($result)) {
88
            throw new \ErrorException('your consumer app has not been created!');
89
        } else {
90
            $output->writeln('Your consumer app has been created!');
91
        }
92
    }
93
}