Completed
Push — master ( 5bda77...cb2c73 )
by Aleh
10s
created

CompleteCommand::executeAsync()   B

Complexity

Conditions 2
Paths 5

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 46
rs 8.9411
cc 2
eloc 29
nc 5
nop 2
1
<?php
2
3
namespace Padawan\Command;
4
5
use Padawan\Framework\Complete\CompleteEngine;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Padawan\Framework\Application\Socket\SocketOutput;
9
use Padawan\Domain\ProjectRepository;
10
use Padawan\Framework\Project\Persister;
11
12
class CompleteCommand extends AsyncCommand
13
{
14
15
    protected function configure()
16
    {
17
        $this->setName("complete")
18
            ->setDescription("Finds completion")
19
            ->addArgument(
20
                "path",
21
                InputArgument::REQUIRED,
22
                "Path to the project root"
23
            )->addArgument(
24
                "column",
25
                InputArgument::REQUIRED,
26
                "Column number of cursor position"
27
            )->addArgument(
28
                "line",
29
                InputArgument::REQUIRED,
30
                "Line number of cursor position"
31
            )->addArgument(
32
                "data",
33
                InputArgument::REQUIRED,
34
                "File contents"
35
            )->addArgument(
36
                "filepath",
37
                InputArgument::REQUIRED,
38
                "Path to file relative to project root"
39
            );
40
    }
41
    protected function executeAsync(InputInterface $input, SocketOutput $output)
42
    {
43
        $column = $input->getArgument("column");
44
        $file = $input->getArgument("filepath");
45
        $line = $input->getArgument("line");
46
        $content = $input->getArgument("data");
47
        $path = $input->getArgument("path");
48
49
        $projectRepository = $this->getContainer()->get(ProjectRepository::class);
50
        $project = $projectRepository->findByPath($path);
51
52
        $completeEngine = $this->getContainer()->get(CompleteEngine::class);
53
        /** @var Persister */
54
        $persister = $this->getContainer()->get(Persister::class);
55
        try {
56
            $completion = $completeEngine->createCompletion(
57
                $project,
58
                $content,
59
                $line,
60
                $column,
61
                $file
62
            );
63
64
            yield $output->write(
65
                json_encode(
66
                    [
67
                        "completion" => $this->prepareEntries(
68
                            $completion["entries"]
69
                        ),
70
                        "context" => $completion["context"]
71
                    ]
72
                )
73
            );
74
            yield $output->disconnect();
75
            yield $persister->save($project);
76
        } catch (\Exception $e) {
77
            yield $output->write(
78
                json_encode(
79
                    [
80
                        "completion" => [],
81
                        "context" => []
82
                    ]
83
                )
84
            );
85
        }
86
    }
87
    protected function prepareEntries(array $entries) {
88
        $result = [];
89
        foreach ($entries as $entry) {
90
            $result[] = [
91
                "name" => $entry->getName(),
92
                "signature" => $entry->getSignature(),
93
                "description" => $entry->getDesc(),
94
                "menu" => $entry->getMenu()
95
            ];
96
        }
97
        return $result;
98
    }
99
}
100