1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Joomla! Statistics Server |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\StatsServer\Commands; |
10
|
|
|
|
11
|
|
|
use Joomla\Console\Command\AbstractCommand; |
12
|
|
|
use Joomla\StatsServer\Repositories\StatisticsRepository; |
13
|
|
|
use Joomla\StatsServer\Views\Stats\StatsJsonView; |
14
|
|
|
use League\Flysystem\Filesystem; |
15
|
|
|
use Symfony\Component\Console\Exception\InvalidOptionException; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Command to take a record snapshot |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
class SnapshotCommand extends AbstractCommand |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* The default command name |
28
|
|
|
* |
29
|
|
|
* @var string|null |
30
|
|
|
*/ |
31
|
|
|
protected static $defaultName = 'snapshot'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* JSON view for displaying the statistics. |
35
|
|
|
* |
36
|
|
|
* @var StatsJsonView |
37
|
|
|
*/ |
38
|
|
|
private $view; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Filesystem adapter for the snapshots space. |
42
|
|
|
* |
43
|
|
|
* @var Filesystem |
44
|
|
|
*/ |
45
|
|
|
private $filesystem; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor. |
49
|
|
|
* |
50
|
|
|
* @param StatsJsonView $view JSON view for displaying the statistics. |
51
|
|
|
* @param Filesystem $filesystem Filesystem adapter for the snapshots space. |
52
|
|
|
*/ |
53
|
3 |
|
public function __construct(StatsJsonView $view, Filesystem $filesystem) |
54
|
|
|
{ |
55
|
3 |
|
$this->view = $view; |
56
|
3 |
|
$this->filesystem = $filesystem; |
57
|
|
|
|
58
|
3 |
|
parent::__construct(); |
59
|
3 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Internal function to execute the command. |
63
|
|
|
* |
64
|
|
|
* @param InputInterface $input The input to inject into the command. |
65
|
|
|
* @param OutputInterface $output The output to inject into the command. |
66
|
|
|
* |
67
|
|
|
* @return integer The command exit code |
68
|
|
|
*/ |
69
|
3 |
|
protected function doExecute(InputInterface $input, OutputInterface $output): int |
70
|
|
|
{ |
71
|
3 |
|
$symfonyStyle = new SymfonyStyle($input, $output); |
72
|
|
|
|
73
|
3 |
|
$symfonyStyle->title('Creating Statistics Snapshot'); |
74
|
|
|
|
75
|
|
|
// We want the full raw data set for our snapshot |
76
|
3 |
|
$this->view->isAuthorizedRaw(true); |
77
|
|
|
|
78
|
3 |
|
$source = $input->getOption('source'); |
79
|
|
|
|
80
|
3 |
|
$filename = date('YmdHis'); |
81
|
|
|
|
82
|
3 |
|
if ($source) |
83
|
|
|
{ |
84
|
2 |
|
if (!\in_array($source, StatisticsRepository::ALLOWED_SOURCES)) |
85
|
|
|
{ |
86
|
1 |
|
throw new InvalidOptionException( |
87
|
1 |
|
\sprintf( |
88
|
1 |
|
'Invalid source "%s" given, valid options are: %s', |
89
|
1 |
|
$source, |
90
|
1 |
|
implode(', ', StatisticsRepository::ALLOWED_SOURCES) |
91
|
|
|
) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
$this->view->setSource($source); |
96
|
|
|
|
97
|
1 |
|
$filename .= '_' . $source; |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
if (!$this->filesystem->write($filename, $this->view->render())) |
101
|
|
|
{ |
102
|
|
|
$symfonyStyle->error('Failed writing snapshot to the filesystem.'); |
103
|
|
|
|
104
|
|
|
return 1; |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
$symfonyStyle->success('Snapshot recorded.'); |
108
|
|
|
|
109
|
2 |
|
return 0; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Configures the current command. |
114
|
|
|
* |
115
|
|
|
* @return void |
116
|
|
|
*/ |
117
|
3 |
|
protected function configure(): void |
118
|
|
|
{ |
119
|
3 |
|
$this->setDescription('Takes a snapshot of the statistics data.'); |
120
|
3 |
|
$this->addOption( |
121
|
3 |
|
'source', |
122
|
3 |
|
null, |
123
|
3 |
|
InputOption::VALUE_OPTIONAL, |
124
|
3 |
|
'If given, filters the snapshot to a single source.' |
125
|
|
|
); |
126
|
3 |
|
} |
127
|
|
|
} |
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.