SyncCommand::execute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 2
dl 0
loc 31
ccs 0
cts 19
cp 0
crap 6
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace CompoLab\Application\Cli;
4
5
use CompoLab\Application\GitlabRepositoryManager;
6
use Gitlab\Client as Gitlab;
7
use Gitlab\Model\Project;
8
use Gitlab\ResultPager;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Helper\ProgressBar;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
final class SyncCommand extends Command
15
{
16
    /** @var Gitlab */
17
    private $gitlab;
18
19
    /** @var GitlabRepositoryManager */
20
    private $repositoryManager;
21
22
    public function __construct(Gitlab $gitlab, GitlabRepositoryManager $repositoryManager)
23
    {
24
        $this->gitlab = $gitlab;
25
        $this->repositoryManager = $repositoryManager;
26
27
        parent::__construct();
28
    }
29
30
    public function configure()
31
    {
32
        $this
33
            ->setName('sync')
34
            ->setDescription('Sync the CompoLab cache with GitLab')
35
            ->setHelp('This command will list all GitLab projects (accessible with the specified token), generate a complete packages.json based on this list and download all package archives into the web-accessible cache directory.')
36
        ;
37
    }
38
39
    public function execute(InputInterface $input, OutputInterface $output)
40
    {
41
        $output->write('List all projects...');
42
43
        $pager = new ResultPager($this->gitlab);
44
        $projects = $pager->fetchall($this->gitlab->projects, 'all');
45
46
        $output->writeln(' OK');
47
48
        ProgressBar::setFormatDefinition('custom', "%message% \n%current%/%max% [%bar%] %percent:3s%% \n");
49
        $progress = new ProgressBar($output, count($projects));
50
        $progress->setFormat('custom');
51
52
        foreach ($projects as $project) {
53
            $progress->setMessage(sprintf('Parse project "%s"...', $project['name']));
54
55
            $this->repositoryManager->registerProject(
56
                Project::fromArray($this->gitlab, $project)
57
            );
58
59
            $progress->advance();
60
        }
61
62
        $progress->setMessage('Parse projects... OK');
63
        $progress->finish();
64
65
        $output->write('Persist JSON in cache...');
66
        $this->repositoryManager->save();
67
        $output->writeln(' OK');
68
69
        $output->writeln('Finished');
70
    }
71
}
72