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\Snapshot; |
10
|
|
|
|
11
|
|
|
use Joomla\Controller\AbstractController; |
12
|
|
|
use Joomla\StatsServer\CommandInterface; |
13
|
|
|
use Joomla\StatsServer\Views\Stats\StatsJsonView; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Command to take a record snapshot for recently updated records |
17
|
|
|
* |
18
|
|
|
* @method \Joomla\StatsServer\CliApplication getApplication() Get the application object. |
19
|
|
|
* @property-read \Joomla\StatsServer\CliApplication $app Application object |
20
|
|
|
*/ |
21
|
|
View Code Duplication |
class RecentCommand extends AbstractController implements CommandInterface |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* JSON view for displaying the statistics. |
25
|
|
|
* |
26
|
|
|
* @var StatsJsonView |
27
|
|
|
*/ |
28
|
|
|
private $view; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Constructor. |
32
|
|
|
* |
33
|
|
|
* @param StatsJsonView $view JSON view for displaying the statistics. |
34
|
|
|
*/ |
35
|
|
|
public function __construct(StatsJsonView $view) |
36
|
|
|
{ |
37
|
|
|
$this->view = $view; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Execute the controller. |
42
|
|
|
* |
43
|
|
|
* @return boolean |
44
|
|
|
*/ |
45
|
|
|
public function execute() |
46
|
|
|
{ |
47
|
|
|
$this->getApplication()->outputTitle('Creating Statistics Snapshot'); |
48
|
|
|
|
49
|
|
|
// We want the full raw data set for our snapshot |
50
|
|
|
$this->view->isAuthorizedRaw(true); |
51
|
|
|
$this->view->isRecent(true); |
52
|
|
|
|
53
|
|
|
$file = APPROOT . '/snapshots/' . date('YmdHis') . '_recent'; |
54
|
|
|
|
55
|
|
|
if (!file_put_contents($file, $this->view->render())) |
56
|
|
|
{ |
57
|
|
|
throw new \RuntimeException('Failed writing snapshot to the filesystem at ' . $file); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->getApplication()->out('<info>Snapshot successfully recorded.</info>'); |
61
|
|
|
|
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the command's description |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getDescription() : string |
71
|
|
|
{ |
72
|
|
|
return 'Takes a snapshot of the recently updated statistics data.'; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the command's title |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getTitle() : string |
81
|
|
|
{ |
82
|
|
|
return 'Recent Stats Snapshot'; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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.