Completed
Push — master ( d055c4...9f7aba )
by timegryd
04:27
created

OpcacheResetCommand::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
1
<?php
2
3
namespace Timegryd\OpcacheResetBundle\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
use GuzzleHttp\Client as HttpClient;
10
use Timegryd\OpcacheResetBundle\Helper\OpcacheResetCommandHelper;
11
12
class OpcacheResetCommand extends ContainerAwareCommand
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $defaultHost;
18
19
    /**
20
     * @var string
21
     */
22
    protected $defaultDir;
23
24
    /**
25
     * @var OpcacheResetCommandHelper
26
     */
27
    protected $helper;
28
29
    public function __construct($defaultHost, $defaultDir, OpcacheResetCommandHelper $opcacheResetCommandHelper, HttpClient $httpClient)
30
    {
31
        $this->defaultHost = $defaultHost;
32
        $this->defaultDir = $defaultDir;
33
        $this->helper = $opcacheResetCommandHelper;
34
        $this->httpClient = $httpClient;
0 ignored issues
show
Bug introduced by
The property httpClient does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
36
        parent::__construct();
37
    }
38
39
    protected function configure()
40
    {
41
        $this
42
            ->setName('opcache:reset')
43
            ->setDescription('Reset opcache')
44
            ->addArgument('host', InputArgument::OPTIONAL, 'Base url to call', $this->defaultHost)
45
            ->addArgument('dir', InputArgument::OPTIONAL, 'Web dir where to create file to call', $this->defaultDir)
46
        ;
47
    }
48
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $output->writeln(['Opcache reset begin', '==================='], OutputInterface::VERBOSITY_VERBOSE);
52
53
        $token = sha1(uniqid());
54
55
        $this->helper->createFile($input->getArgument('dir'), $token);
56
        $output->writeln('File created', OutputInterface::VERBOSITY_VERBOSE);
57
58
        $url = $this->helper->generateUrl($input->getArgument('host'), $token);
59
60
        $response = $this->httpClient->request('GET', $url);
61
62
        $this->helper->clean($input->getArgument('dir'));
63
        $output->writeln('File deleted', OutputInterface::VERBOSITY_VERBOSE);
64
65
        $output->writeln($this->helper->handleResponse($response), OutputInterface::VERBOSITY_VERBOSE);
66
    }
67
}
68