ImportCommand::execute()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
cc 5
eloc 23
nc 6
nop 2
1
<?php
2
3
namespace MS\Sentry\Monitor\Command;
4
5
use MS\Sentry\Monitor\Application;
6
use MS\Sentry\Monitor\Model\SentryRequest;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Helper\ProgressBar;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
class ImportCommand extends Command
16
{
17
    /**
18
     * @var Application
19
     */
20
    private $application;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
21
22
    /**
23
     * @param Application $application
24
     * @param string      $name
25
     */
26
    public function __construct(Application $application, $name = null)
27
    {
28
        parent::__construct($name);
29
30
        $this->application = $application;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    protected function configure()
37
    {
38
        $this
39
            ->setName('import')
40
            ->setDescription('import sentry events by organisation/project')
41
            ->addArgument('organisation', InputArgument::REQUIRED, 'slug of the organisation')
42
            ->addArgument('project', InputArgument::OPTIONAL, 'slug of the project, if empty, all projects will be imported')
43
            ->addOption('sentry-url', 's', InputOption::VALUE_REQUIRED, 'base sentry url')
44
            ->addOption('sentry-api-key', 'a', InputOption::VALUE_REQUIRED, 'sentry api key')
45
            ->addOption('project-blacklist', 'b', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'blacklist specific projects', [])
46
        ;
47
    }
48
49
    /**
50
     * @param InputInterface  $input
51
     * @param OutputInterface $output
52
     *
53
     * @return void
54
     */
55
    protected function execute(InputInterface $input, OutputInterface $output)
56
    {
57
        $io = new SymfonyStyle($input, $output);
58
        $io->title('Import sentry events');
59
60
        $organisation = $input->getArgument('organisation');
61
        $project = $input->getArgument('project');
62
        $projectBlacklist = $input->getOption('project-blacklist');
63
        $sentryRequest = new SentryRequest($input->getOption('sentry-url'), $input->getOption('sentry-api-key'));
64
65
        $projects = [$project];
66
67
        if (null === $project) {
68
            $projects = $this->application['project.collector']->getSlugs($sentryRequest, $organisation);
69
        }
70
71
        $progress = new ProgressBar($output);
72
        $progress->start();
73
74
        foreach ($projects as $project) {
75
            if (in_array($project, $projectBlacklist)) {
76
                continue;
77
            }
78
79
            $simplifiedEvents = $this->application['event.collector']->getSimplifiedEvents($sentryRequest, $organisation, $project);
80
81
            foreach ($simplifiedEvents as $simplifiedEvent) {
82
                $this->application['importer']->import($organisation, $project, $simplifiedEvent);
83
                $progress->advance();
84
            }
85
        }
86
87
        $progress->finish();
88
89
        $io->newLine(2);
90
        $eventCount = $this->application['db']->fetchColumn('SELECT COUNT(*) FROM events');
91
        $io->success(sprintf('%s events are available.', $eventCount));
92
    }
93
}
94