Completed
Push — master ( 09c647...dd7acd )
by Nate
05:04
created

CompileCommand.php$0 ➔ send()   A

Complexity

Conditions 1

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
rs 10
c 0
b 0
f 0
cc 1
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Retrofit\Command;
10
11
use Psr\Http\Message\RequestInterface;
12
use Psr\Http\Message\ResponseInterface;
13
use Symfony\Component\Console\Command\Command;
14
use Symfony\Component\Console\Exception\InvalidArgumentException;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Tebru\Retrofit\HttpClient;
19
use Tebru\Retrofit\Retrofit;
20
21
/**
22
 * Class CompileCommand
23
 *
24
 * @author Nate Brunette <[email protected]>
25
 * @codeCoverageIgnore
26
 */
27
class CompileCommand extends Command
28
{
29
    /**
30
     * Configure command
31
     *
32
     * @throws InvalidArgumentException
33
     */
34
    protected function configure()
35
    {
36
        $this->setName('compile');
37
        $this->setDescription('Compiles and caches all services found in the project');
38
        $this->addArgument('sourceDirectory', InputArgument::REQUIRED, 'Enter the source directory');
39
        $this->addArgument('cacheDirectory', InputArgument::REQUIRED, 'Enter the cache directory');
40
    }
41
42
    /**
43
     * Execute command
44
     *
45
     * @param InputInterface $input
46
     * @param OutputInterface $output
47
     * @return int|null|void
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $srcDir = $input->getArgument('sourceDirectory');
52
        $cacheDir = $input->getArgument('cacheDirectory');
53
54
        $clientStub = new class implements HttpClient {
55
            public function send(RequestInterface $request): ResponseInterface { }
56
            public function sendAsync(RequestInterface $request, callable $onResponse, callable $onFailure): void { }
57
            public function wait(): void { }
58
        };
59
60
        $retrofit = Retrofit::builder()
61
            ->setBaseUrl('')
62
            ->setHttpClient($clientStub)
63
            ->setCacheDir($cacheDir)
64
            ->enableCache()
65
            ->build();
66
        $count = $retrofit->createAll($srcDir);
67
68
        $output->writeln(sprintf('<info>Compiled %s %s successfully</info>', $count, ($count === 1) ? 'class' : 'classes'));
69
    }
70
}
71